Advertisement
szubert

lab 2 08.04

Apr 8th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. /*void mergeOutOfPlace(int *arr, int first, int mid, int last) {
  8.     // Merges two contigous sub-arrays and sorts them out-of-place
  9.     // Condition Required: Sub-arrays must be sorted individually
  10.     int *l = new int [mid-first];
  11.     int *r = new int [last-mid];
  12.     int *tempArr = new int [last-first];
  13.  
  14.     // copying into new arrays
  15.     for (int i=0, j=first; i<mid-first; ++i, ++j) {
  16.         l[i] = arr[j];
  17.     }
  18.     for (int i=0, j=mid; i<last-mid; ++i, ++j) {
  19.         r[i] = arr[j];
  20.     }
  21.  
  22.     // merge
  23.     for(int i=0, j=0, k=0; k<last-first; ++k) {
  24.         if (i == mid-first) {
  25.             tempArr[k] = r[j++];
  26.         }
  27.         else if (j == last-mid) {
  28.             tempArr[k] = l[i++];
  29.         }
  30.         else {
  31.             //(l[i] < r[j]) ? (tempArr[k] = l[i++]) : (temp[k] = r[j++]);
  32.         }
  33.     }
  34.  
  35.     // copy into original array
  36.     for(int i=first, j=0; j<last-first; ++i, ++j) {
  37.         arr[i] = tempArr[j];
  38.     }
  39.  
  40.     delete[] l;
  41.     delete[] r;
  42.     delete[] tempArr;
  43. }
  44.  
  45. void mergeSortBottomUp(int *arr, int first, int last, int size) {
  46.     if (size == 0) return;
  47.     // width determines the length of the 2 arrays, the contiguous
  48.     // arrays which are sent to the mergeOutOfPlace function.
  49.     int width=2;
  50.     // we select arrays with length = power of 2.
  51.     for ( ; width<size ; width*=2) {
  52.         // iterating backwards as iterating forward
  53.         // does not work. mergeOutOfPlace is common
  54.         // for different merge algorithms. When iterating
  55.         // forward the left array has a bug
  56.         int next=size-width, curr=size;
  57.         for ( ; next>=0; curr=next, next-=width) {
  58.             int mid = (curr+next)/2;
  59.             mergeOutOfPlace(arr, next, mid, curr);
  60.         }
  61.         // whenever array of length = pow2 is not selectable
  62.         // we select varied length array, which is always near
  63.         // the end of iteration
  64.         if (curr>=2) {
  65.             mergeOutOfPlace(arr, 0, (size%(width>>1)), curr);
  66.         }
  67.     }
  68.     // if array not power of 2
  69.     if ( (size%(width>>1)) != 0 )
  70.         mergeOutOfPlace(arr, 0, size%(width>>1), size);
  71.         // if array power of 2
  72.     else mergeOutOfPlace(arr, 0, size/2, size);
  73. }
  74. */
  75. void selectionSort(int arr[], int n) {
  76.     int i, j, minIndex, tmp;
  77.     for (i = 0; i < n - 1; i++) {
  78.         minIndex = i;
  79.         for (j = i + 1; j < n; j++)
  80.             if (arr[j] < arr[minIndex])
  81.                 minIndex = j;
  82.         if (minIndex != i) {
  83.             tmp = arr[i];
  84.             arr[i] = arr[minIndex];
  85.             arr[minIndex] = tmp;
  86.         }
  87.     }
  88. }
  89.  
  90. void fillArray(int array[], int table_size){
  91.     for (int i = 0; i < table_size; i++) {
  92.         array[i] = (rand() % 1000000) + 0;
  93.     }
  94. }
  95.  
  96. int main() {
  97.     srand(time(NULL));
  98.     int size_powers[3] = { 14, 18, 22 };
  99.     int sorting_type;
  100.  
  101.     FILE * fp;
  102.     fp = fopen ("times.csv", "w+");
  103.     fprintf(fp, "table size,sorting type,sorting time\n");
  104.  
  105.     for(int n = 0; n < 3; n++) {
  106.  
  107.         float t = 0;
  108.         clock_t start;
  109.         int table_size = 2 << size_powers[n];
  110.         int tab[table_size];
  111.  
  112.         switch(n){
  113.             case 0:
  114.  
  115.                 for(int i=0; i<3; i++){
  116.                     switch(i){
  117.                         case 0:
  118.                             sorting_type = i + 1;
  119.                             fillArray(tab, table_size);
  120.                             start=clock();
  121.  
  122.                             t=(clock()-start)/ CLOCKS_PER_SEC;
  123.                             printf ("%d -%d (%f seconds)\n",n,i, t);
  124.                             fprintf(fp, "%d,%d,%f\n", table_size, sorting_type, t);
  125.                             break;
  126.  
  127.                         case 1:
  128.  
  129.                             break;
  130.  
  131.                         case 2:
  132.  
  133.                             break;
  134.                     }
  135.                 }
  136.                 break;
  137.  
  138.             case 1:
  139.  
  140.                 for(int i=0; i<3; i++){
  141.                     switch(i){
  142.                         case 0:
  143.  
  144.                             break;
  145.  
  146.                         case 1:
  147.  
  148.                             break;
  149.  
  150.                         case 2:
  151.  
  152.                             break;
  153.                     }
  154.                 }
  155.                 break;
  156.  
  157.             case 2:
  158.  
  159.                 for(int i=0; i<3; i++){
  160.                     switch(i){
  161.                         case 0:
  162.  
  163.                             break;
  164.  
  165.                         case 1:
  166.  
  167.                             break;
  168.  
  169.                         case 2:
  170.  
  171.                             break;
  172.                     }
  173.                 }
  174.                 break;
  175.         }
  176.  
  177.     }
  178.     fclose(fp);
  179.  
  180.     return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement