Advertisement
Guest User

Untitled

a guest
May 4th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.34 KB | None | 0 0
  1. /*Matlab
  2. figure; n=[100 1000 2000 3000 4000 5000 6000]; t=[0 0.04 0.15 0.32 0.56 0.87 1.26]; plot(n,t);
  3.  
  4. */
  5.  
  6. #include <iostream>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <ctime>
  10. #include <assert.h>
  11. #include <iomanip>
  12. #include <vector>
  13. #include <sstream>
  14. #include <algorithm>
  15. using namespace std;
  16.  
  17. // Aufgabe 2.1
  18.  
  19. vector <double> GenerateNumbers(string Selection, size_t n){
  20.     vector<double> feld;
  21.  
  22.     for (size_t i = 0; i < n; i++){
  23.         if (!string(Selection).compare("up")){ feld.push_back(i); }
  24.         else if (!string(Selection).compare("down")){ feld.push_back(i*(-1)); }
  25.         else if (!string(Selection).compare("constant")){ feld.push_back(17); }
  26.         else if (!string(Selection).compare("random")){ feld.push_back((double)rand() / (RAND_MAX)); }
  27.         else{ throw "Error: Das Programm braucht einen Modus! [random|up|down|constant]"; }
  28.     }
  29.  
  30.     return feld;
  31. }
  32.  
  33. //Aufgabe 2.2
  34.  
  35. template < class T > bool IsSortedAndNothingIsLost
  36. (vector <T> &Before, vector <T> &After){
  37.     bool SomethingIsLost = false;
  38.     size_t n = Before.size();
  39.  
  40.     //überprüfung des sortierten vektors
  41.     for (size_t i = 0; i<(n - 1); i++)
  42.     {
  43.         if (After[i]>After[i + 1])
  44.         {
  45.             return false;
  46.         }
  47.     }
  48.  
  49.     //Wir überprüfen jede einzelne Stelle und haken ab
  50.     bool *hilfsarray = new bool[n];
  51.     if (*hilfsarray == NULL){ throw "Das Hilfsarray hatte nicht genug speicher um erstellt zu werden."; }
  52.     for (size_t i = 0; i<n; i++)
  53.     {
  54.         hilfsarray[i] = false;
  55.     }
  56.  
  57.     // wir haken ab
  58.     for (size_t i = 0; i<n; i++)
  59.     {
  60.         SomethingIsLost = true;
  61.         for (size_t j = 0; j < n; j++){
  62.             if (Before[i] == After[j] && false == hilfsarray[j]){
  63.                 hilfsarray[j] = true;
  64.                 SomethingIsLost = false;
  65.                 break;
  66.             }
  67.         }
  68.         if (true == SomethingIsLost){
  69.             delete[] hilfsarray;
  70.             return false;
  71.         }
  72.     }
  73.     delete[] hilfsarray;
  74.     return true;
  75.  
  76. }
  77.  
  78. //Aufgabe 2.3
  79.  
  80. template <class T> void InsertionSort(vector<T> &a){
  81.     const int len = (int)a.size();
  82.     int i, j;
  83.     T temp;
  84.     for (i = 1; i < len; i++){
  85.         temp = a[i];
  86.         for (j = i - 1; j >= 0 && a[j] > temp; j--){
  87.             a[j + 1] = a[j];
  88.         }
  89.         a[j + 1] = temp;
  90.     }
  91. }
  92.  
  93. //template <class T> void MergeSort(vector<T> &a){
  94.     /*int curr_size;  // For current size of subarrays to be merged
  95.     // curr_size varies from 1 to n/2
  96.     int left_start; // For picking starting index of left subarray
  97.     // to be merged
  98.     int n = a.size();
  99.  
  100.     // Merge subarrays in bottom up manner.  First merge subarrays of
  101.     // size 1 to create sorted subarrays of size 2, then merge subarrays
  102.     // of size 2 to create sorted subarrays of size 4, and so on.
  103.     for (curr_size = 1; curr_size <= n - 1; curr_size = 2 * curr_size)
  104.     {
  105.         // Pick starting point of different subarrays of current size
  106.         for (left_start = 0; left_start<n - 1; left_start += 2 * curr_size)
  107.         {
  108.             // Find ending point of left subarray. mid+1 is starting
  109.             // point of right
  110.             int mid = left_start + curr_size - 1;
  111.  
  112.             int right_end = min(left_start + 2 * curr_size - 1, n - 1);
  113.  
  114.             // Merge Subarrays arr[left_start...mid] & arr[mid+1...right_end]
  115.             merge(a, left_start, mid, right_end);
  116.         }
  117.     }
  118. }
  119.  
  120. /* Function to merge the two haves arr[l..m] and arr[m+1..r] of array arr[] */
  121. //template <class T> void merge(vector<T> &a, int l, int m, int r)
  122. /*{
  123.     int i, j, k;
  124.     int n1 = m - l + 1;
  125.     int n2 = r - m;
  126.  
  127.     // create temp vectors
  128.     vector<T> L (n1), R(n2);
  129.  
  130.     // Copy data to temp vectors L[] and R[]
  131.     for (i = 0; i < n1; i++)
  132.         L[i] = a[l + i];
  133.     for (j = 0; j < n2; j++)
  134.         R[j] = a[m + 1 + j];
  135.  
  136.     // Merge the temp arrays back into arr[l..r]
  137.     i = 0;
  138.     j = 0;
  139.     k = l;
  140.     while (i < n1 && j < n2)
  141.     {
  142.         if (L[i] <= R[j])
  143.         {
  144.             a[k] = L[i];
  145.             i++;
  146.         }
  147.         else
  148.         {
  149.             a[k] = R[j];
  150.             j++;
  151.         }
  152.         k++;
  153.     }
  154.  
  155.     // Copy the remaining elements of L[], if there are any
  156.     while (i < n1)
  157.     {
  158.         a[k] = L[i];
  159.         i++;
  160.         k++;
  161.     }
  162.  
  163.     // Copy the remaining elements of R[], if there are any
  164.     while (j < n2)
  165.     {
  166.         a[k] = R[j];
  167.         j++;
  168.         k++;
  169.     }
  170. }
  171. /*
  172. //Aufgabe 2.4
  173. /*
  174.  
  175. template <class T> void MergeSort(vector<T> &a){
  176. const int len = (int)a.size();
  177. T* x = a;
  178. T* y = new T[len];
  179. for (int seg = 1; seg < len; seg += seg){
  180. for (int start = 0; start < len; start += seg + seg){
  181. int low = start;
  182. int mid = (start + seg) < len ? (start + seg) : len;
  183. int high = (start + seg + seg) < len ? (start + seg + seg) : len;
  184. int k = low;
  185. int start1 = low, end1 = mid;
  186. int start2 = mid, end2 = high;
  187. while (start1 < end1 && start2 < end2){
  188. y[k++] = x[start1] < x[start2] ? a[start1++] : a[start2++];
  189. }
  190. while(start1 < end1){
  191. y[k++] = x[start1++];
  192. }
  193. while (start2 < end2){
  194. y[k++] = x[start2++];
  195. }
  196. }
  197. T* temp = x;
  198. x = y;
  199. y = temp;
  200. }
  201. if (x != a){
  202. for (int i = 0; i < len; i++){
  203. y[i] = x[i];
  204. }
  205. y = x;
  206. }
  207. delete y[];
  208. }
  209. */
  210. //template <class T> void MergeSort(vector<T> &a){}
  211.  
  212.  
  213.  
  214. template <class T> void merge_sort(vector<T> &V, int size){
  215.     vector<T> temp[size];
  216.     int mid, i;
  217.     if (size < 2) {
  218.         return;
  219.     }
  220.     else {
  221.         mid = size / 2;
  222.         merge_sort(V, mid);
  223.         merge_sort(V + mid, size - mid);
  224.         merge(V, mid, V + mid, size - mid, temp);
  225.         for (i = 0; i < size; i++) {
  226.             V[i] = temp[i];
  227.         }
  228.     }
  229. }
  230.  
  231. template <class T> int  merge(vector<T> &List1, int size1, vector<T> &List2, int size2, vector<T> &List3)
  232. {
  233.     int i1, i2, i3;
  234.     if (size1 + size2 > size) {
  235.         return false;
  236.     }
  237.     i1 = 0; i2 = 0; i3 = 0;
  238.     /* while both lists are non-empty */
  239.     while (i1 < size1 && i2 < size2) {
  240.         if (list1[i1] < list2[i2]) {
  241.             list3[i3++] = list1[i1++];
  242.         }
  243.         else {
  244.             list3[i3++] = list2[i2++];
  245.         }
  246.     }
  247.     while (i1 < size1) {
  248.         /* copy remainder of list1 */
  249.         list3[i3++] = list1[i1++];
  250.     }
  251.     while (i2 < size2) {
  252.         /* copy remainder of list2 */
  253.         list3[i3++] = list2[i2++];
  254.     }
  255.     return true;
  256. }
  257.  
  258.  
  259. int main(int argc, char *argv[]) {
  260.  
  261.     char *Selection = NULL;
  262.  
  263.     // Flags
  264.     bool Vergleich = false;
  265.     bool CheckResult = false;
  266.     bool Zeitmessung = false;
  267.     bool UseMergeSort = false;
  268.  
  269.     int SizeOfArray = 0;
  270.     vector<double> Original;
  271.     double MeasuredTime;
  272.  
  273.     // Zuerst fragen wir die Paramter ab:
  274.     try{
  275.         if (argv[1] == NULL){
  276.             throw "mysort n Direction [ -o ] [ -c ] [ -t ] [ -m | -i ]";
  277.         }
  278.  
  279.         if (!(istringstream(argv[1]) >> dec >> SizeOfArray)){
  280.             throw "Der erste Wert muss ein Integer sein!";
  281.         }
  282.  
  283.         if (SizeOfArray < 1){
  284.             throw "keine negativen Zahlen erlaubt!";
  285.         }
  286.    
  287.  
  288.         // Selection
  289.         if (!string(argv[2]).compare("up") ||
  290.             !string(argv[2]).compare("down") ||
  291.             !string(argv[2]).compare("constant") ||
  292.             !string(argv[2]).compare("random")) {
  293.             Selection = argv[2];
  294.         }
  295.  
  296.         // checken für flags
  297.         for (int i = 3; i < argc; i++){
  298.             if (!string(argv[i]).compare("-c")) {
  299.                 CheckResult = true;
  300.                 continue;
  301.             }
  302.             if (!string(argv[i]).compare("-o")) {
  303.                 Vergleich = true;
  304.                 continue;
  305.             }
  306.             if (!string(argv[i]).compare("-t")) {
  307.                 Zeitmessung = true;
  308.                 continue;
  309.             }
  310.             if (!string(argv[i]).compare("-m") && !string(argv[i]).compare("-i")){
  311.                 throw "-m und -i darf nur einmal benutzt werden!";
  312.             }
  313.             if (!string(argv[i]).compare("-m")) {
  314.                 UseMergeSort = true;
  315.                 continue;
  316.             }
  317.             if (!string(argv[i]).compare("-i")) {
  318.                 UseMergeSort = false;
  319.                 continue;
  320.             }
  321.             else{
  322.                 throw "Wrong Switch, use only one of - o | -c | -t | -m | -i ";
  323.             }
  324.  
  325.         }
  326.  
  327.         if (Selection == NULL){
  328.             throw "Error: Das Programm braucht einen Modus! [random|up|down|constant]";
  329.         }
  330.  
  331.         //Aufgabe 2.5
  332.  
  333.         Original.reserve(SizeOfArray);
  334.  
  335.         Original = GenerateNumbers(Selection, SizeOfArray);
  336.         vector<double>Sorted(Original);
  337.  
  338.         MeasuredTime = double(clock());
  339.  
  340.         // Welchen Algorithmus ?
  341.         if (UseMergeSort == true){
  342.             merge_sort(Sorted, SizeOfArray);
  343.         }
  344.         else{
  345.             InsertionSort(Sorted);
  346.         }
  347.  
  348.  
  349.         if (Zeitmessung == true){ MeasuredTime = (double(clock()) - MeasuredTime) / CLOCKS_PER_SEC; }
  350.  
  351.         // Erfolgsüberprüfung
  352.         if (CheckResult == true){
  353.             if (true == IsSortedAndNothingIsLost(Original, Sorted)){
  354.                 cout << "Success" << endl;
  355.             }
  356.             else{
  357.                 throw "Check failed";
  358.             }
  359.         }
  360.  
  361.         /*
  362.         if (Vergleich == true){
  363.             cout << "Ergebnisse:" << endl << " Original : Sorted" << endl;
  364.             for (int i = 0; i < SizeOfArray; i++){
  365.                 cout << setprecision(5) << setw(9) << Original[i] << " : " << setprecision(5) << Sorted[i] << endl;
  366.             }
  367.         }
  368.         */
  369.         if (Zeitmessung == true){
  370.             cout << "--- It took " << setprecision(32) << MeasuredTime << " seconds to compute---" << endl;
  371.         }
  372.  
  373.  
  374.     }
  375.     catch (const char *Error){
  376.         cerr << Error;
  377.         exit(1);
  378.     }
  379.  
  380.     return 0;
  381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement