Advertisement
agul

20130517 :: Sorting

May 17th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7.  
  8. #define N 1000
  9.  
  10. #ifdef ASC
  11.     #define less compAsc
  12. #elif DESC_ABS
  13.     #define less compDescAbs
  14. #else
  15.     #error "Use -DASC or -DDESC_ABS to define sorting type"
  16. #endif
  17.  
  18. #ifdef WIN32
  19.     #define LLD "%I64d"
  20. #else
  21.     #define LLD "%lld"
  22. #endif
  23.  
  24.  
  25. ull timestamp(void);
  26. void swap(ll * a, ll * b);
  27. int compAsc(ll a, ll b);
  28. int compDescAbs(ll a, ll b);
  29.  
  30. ull start, compareCount, swapCount, ticksCount;
  31. ll a[N], tmp[N], heap[N];
  32.  
  33. ll random() {
  34.     return ((ll)rand() << 49) ^ ((ll)rand() << 32) ^ (rand() << 16) ^ rand();
  35. }
  36.  
  37. void generateRandom(void) {
  38.     for (int i = 0; i < N; ++i)
  39.         a[i] = random();
  40. }
  41.  
  42. void generateSorted(void) {
  43.     a[0] = random();
  44.     for (int i = 1; i < N; ++i)
  45.         a[i] = a[i - 1] + rand();
  46.     if (less(1, 0))
  47.         for (int i = 0; i < N; ++i)
  48.             if (rand() & 1) a[i] = -a[i];
  49. }
  50.  
  51. void reverse(void) {
  52.     int l = 0, r = N - 1;
  53.     while (l < r) swap(a + (l++), a + (r--));
  54. }
  55.  
  56. void selectionSort(void) {
  57.     int min;
  58.     for (int i = 0; i < N; ++i) {
  59.         min = i;
  60.         for (int j = i + 1; j < N; ++j)
  61.             if (less(a[j], a[min])) min = j;
  62.         swap(a + i, a + min);
  63.     }
  64. }
  65.  
  66. void heapSort(void) {
  67.     int cur, count = 0;
  68.     for (int i = 0; i < N; ++i) {
  69.         heap[cur = ++count] = a[i];
  70.         while (cur > 1)
  71.             if (less(heap[cur], heap[cur >> 1])) swap(heap + cur, heap + (cur >> 1)), cur >>= 1; else break;
  72.     }
  73.     for (int i = 0; i < N; ++i) {
  74.         a[i] = heap[1], heap[cur = 1] = heap[count--];
  75.         while ((cur << 1) < count) {
  76.             int tmp = cur;
  77.             if (less(heap[cur << 1], heap[tmp])) tmp = cur << 1;
  78.             if (less(heap[(cur << 1) ^ 1], heap[tmp])) tmp = (cur << 1) ^ 1;
  79.             if (tmp == cur) break;
  80.             swap(heap + cur, heap + tmp), cur = tmp;
  81.         }
  82.     }
  83. }
  84.  
  85. int main() {
  86.     srand(time(NULL));
  87.     printf("\nAlexander Agulenko (c) 2013\n\nNumber of elements in array is %d\n\nSelect type of sequence:\n\t> 1: Elements are in a sorted order\n\t> 2: Elements are in a reversed order\n\t> 3: Elements are in a random order\n\n", N);
  88.     int type;
  89.     scanf("%d", &type);
  90.     if (type == 3) generateRandom(); else {
  91.         generateSorted();
  92.         if (type == 2 && less(0, 1) || type == 1 && less(1, 0)) reverse();
  93.     }
  94.     printf("\nInitial sequence:\n");
  95.     for (int i = 0; i < N; ++i)
  96.         printf(LLD" ", a[i]);
  97.     memcpy(tmp, a, sizeof(a));
  98.     printf("\n\n\n");
  99.     compareCount = swapCount = 0, ticksCount = timestamp(), selectionSort();
  100.     printf("SELECTION SORT\n\nFinal sequence:\n");
  101.     for (int i = 0; i < N; ++i)
  102.         printf(LLD" ", a[i]);
  103.     ticksCount = timestamp() - ticksCount, printf("\n\n> Comparisons count: "LLD"\n> Swaps count: "LLD"\n> CPU ticks count: "LLD"\n\n\n", compareCount, swapCount, ticksCount);
  104.     memcpy(a, tmp, sizeof(tmp));
  105.     compareCount = swapCount = 0, ticksCount = timestamp(), heapSort();
  106.     printf("HEAP SORT\n\nFinal sequence:\n");
  107.     for (int i = 0; i < N; ++i)
  108.         printf(LLD" ", a[i]);
  109.     ticksCount = timestamp() - ticksCount, printf("\n\n> Comparisons count: "LLD"\n> Swaps count: "LLD"\n> CPU ticks count: "LLD"\n\n\n", compareCount, swapCount, ticksCount);
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement