Advertisement
Guest User

ovo sa casa

a guest
Mar 13th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <limits.h>
  5.  
  6. #define MAXL 100000
  7. #define MAXE 1000000
  8.  
  9. void copyArray(int[], int[], int);
  10. void printArray(int[], int);
  11. void randArray(int[], int);
  12.  
  13. int cmp(const void *, const void *);
  14. void quickSort(int[], int);
  15. void bubbleSort(int[], int);
  16. void selectionSort(int[], int);
  17. void insertionSort(int[], int);
  18. void shellSort(int[], int);
  19. void mergeSort(int[], int);
  20.  
  21. void test_func(void (*f)(int[], int), int[], int);
  22.  
  23. int main()
  24. {
  25.     int a[MAXL], b[MAXL], i, j;
  26.     void (*pf[])(int[], int) = {bubbleSort, selectionSort, quickSort,
  27.                                 insertionSort, shellSort, mergeSort};
  28.     char algs[][MAXL] = {"Bubble sort:", "Selection sort:", "Quicksort:",
  29.                          "Insertion sort:", "Shell sort:", "Merge sort:"};
  30.  
  31.     for (i = 10; i <= MAXL; i *= 10)
  32.     {
  33.         randArray(a, i);
  34.         printf("\ni : %d\n", i);
  35.  
  36.         for (j = 0; j < 6; j++)
  37.         {
  38.             puts(algs[j]);
  39.             copyArray(a, b, i);
  40.             test_func(pf[j], b, i);
  41.         }
  42.     }
  43.  
  44.     return 0;
  45. }
  46.  
  47. int cmp(const void *a, const void *b)
  48. {
  49.     return *(int *)a > *(int *)b;
  50. }
  51.  
  52. void copyArray(int a[], int b[], int n)
  53. {
  54.     int i;
  55.  
  56.     for (i = 0; i < n; i++)
  57.     {
  58.         b[i] = a[i];
  59.     }
  60. }
  61.  
  62. int partition(int a[], int lo, int hi)
  63. {
  64.     // TODO : quicksort parition procedure
  65.     return 0;
  66.     swap(a[j], a[iMin])
  67. }
  68.  
  69. void quicksort(int a[], int lo, int hi)
  70. {
  71.     // TODO : quicksort for a[lo..hi]
  72. }
  73.  
  74. void quickSort(int a[], int n)
  75. {
  76.     quicksort(a, 0, n - 1);
  77. }
  78.  
  79. void selectionSort(int a[], int n)
  80. {
  81.     // TODO
  82. }
  83.  
  84. void bubbleSort(int a[], int n)
  85. {
  86.     int i,j,tmp=0;
  87.     for(i=0;i<n-1;i++)
  88.         {
  89.             for(j=0;j<n-i-1;j++)
  90.                 if(a[i]<a[j])
  91.                 {
  92.                     temp=a[j];
  93.                     a[j]=a[i];
  94.                     a[i]=temp;  
  95.                 }
  96.         }
  97. }
  98.  
  99. void insertionSort(int a[], int n)
  100. {
  101.     // TODO
  102. }
  103.  
  104. void shellSort(int a[], int n)
  105. {
  106.     // TODO
  107. }
  108.  
  109. void merge(int a[], int p, int q, int r)
  110. {
  111.    n1=
  112.    
  113. }
  114.  
  115. void mergesort(int a[], int p, int r)
  116. {
  117.     if(p<r)
  118.     {
  119.         int m = p+(l-p)/2;
  120.         mergesort(a,p,m);
  121.         mergesort(a,m+1,r);
  122.         merge(a,p,m,r);
  123.     }
  124. }
  125.  
  126. void mergeSort(int a[], int n)
  127. {
  128.     mergesort(a, 0, n - 1);
  129. }
  130.  
  131. void randArray(int a[], int n)
  132. {
  133.     int i;
  134.     srand(time(0));
  135.     for (i = 0; i < n; i++)
  136.     {
  137.         a[i] = rand() % MAXE;
  138.     }
  139. }
  140.  
  141. void printArray(int a[], int n)
  142. {
  143.     int i = 0;
  144.     for (i = 0; i < n; i++)
  145.     {
  146.         printf("%d ", a[i]);
  147.     }
  148.     puts("");
  149. }
  150.  
  151. void test_func(void (*f)(int[], int), int a[], int n)
  152. {
  153.     clock_t start_t, end_t;
  154.  
  155.     start_t = clock();
  156.     f(a, n);
  157.     end_t = clock();
  158.  
  159.     double total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
  160.     printf("Total time: %lf\n", total_t);
  161.  
  162.     //printArray(a, n);
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement