944,082 Members | Top Members by Rank

Ad:
  • C Code Snippet
  • Views: 4226
  • C RSS
0

Comparing Sorting Routines

by on Sep 4th, 2005
I have joined the thousands who have done it before, and have compared a number of sorting routines. The sorting is done on the same random-integer arrays. No surprises, quicksort wins this simple comparison hands down. There are clever combinations of sorting routines that are faster, like the snippet by Narue shown in:
http://www.daniweb.com/code/snippet61.html
There is also an expert treatise on sorting at:
http://www.eternallyconfuzzled.com/tuts/sorting.html
C Code Snippet (Toggle Plain Text)
  1. // taking a comparative look at several sort routines
  2. // typical results: qsort() = 31ms shellsort = 922ms insertionsort = 2672ms bubblesort = 12032ms
  3. // a Windows Console Application tested with Pelles C vegaseat 04sep2005
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h> // qsort(), srand(), rand()
  7. #include <string.h> // strcmp()
  8. #include <windows.h> // GetTickCount() ms since WIN has started
  9.  
  10. #define NUM_ITEMS 50000 // might tax memory capacity
  11.  
  12. int compare(const void *a, const void *b);
  13. void shellSort(int numbers[], int array_size);
  14. void insertionSort(int numbers[], int array_size);
  15. void bubbleSort(int numbers[], int array_size);
  16.  
  17. int main(void)
  18. {
  19. int k;
  20. long te, ts;
  21. int numbers1[NUM_ITEMS], numbers2[NUM_ITEMS], numbers3[NUM_ITEMS], numbers4[NUM_ITEMS];
  22.  
  23. //seed random number generator
  24. srand(GetTickCount());
  25.  
  26. //fill arrays with random integers 0 to 49999
  27. for (k = 0; k < NUM_ITEMS; k++)
  28. numbers1[k] = numbers2[k] = numbers3[k] = numbers4[k] = rand() % NUM_ITEMS;
  29.  
  30. //perform quicksort on array and time it ...
  31. ts = GetTickCount();
  32. qsort(numbers1, NUM_ITEMS, sizeof(int *), compare);
  33. te = GetTickCount();
  34. printf("Quicksort of %d random integers took %d ms\n", NUM_ITEMS, te - ts);
  35.  
  36. //perform shellsort on array and time it ...
  37. ts = GetTickCount();
  38. shellSort(numbers2, NUM_ITEMS);
  39. te = GetTickCount();
  40. printf("Shellsort of %d random integers took %d ms\n", NUM_ITEMS, te - ts);
  41.  
  42. //perform insertion sort on array and time it ...
  43. ts = GetTickCount();
  44. insertionSort(numbers3, NUM_ITEMS);
  45. te = GetTickCount();
  46. printf("Insertionsort of %d random integers took %d ms\n", NUM_ITEMS, te - ts);
  47.  
  48. //perform bubble sort on array and time it ...
  49. printf("Bubbling away ...\n");
  50. ts = GetTickCount();
  51. bubbleSort(numbers4, NUM_ITEMS);
  52. te = GetTickCount();
  53. printf("Bubble sort of %d random integers took %d ms\n", NUM_ITEMS, te - ts);
  54.  
  55. /*
  56.   // test: show first 100 sorted numbers of each sort
  57.   printf("\n---------------------\n");
  58.   for (k = 0; k < 100; k++)
  59.   printf("%8d ", numbers1[k]);
  60.   printf("\n---------------------\n");
  61.   for (k = 0; k < 100; k++)
  62.   printf("%8d ", numbers2[k]);
  63.   printf("\n---------------------\n");
  64.   for (k = 0; k < 100; k++)
  65.   printf("%8d ", numbers3[k]);
  66.   printf("\n---------------------\n");
  67.   for (k = 0; k < 100; k++)
  68.   printf("%8d ", numbers4[k]);
  69.   */
  70.  
  71. getchar(); // wait
  72. return 0;
  73. }
  74.  
  75. // this odd compare function using generic pointers is required by qsort()
  76. int compare(const void *a, const void *b)
  77. {
  78. // cast generic pointer to appropriate pointer
  79. // for strings use return( strcmp((char *)a,(char *)b) );
  80. return ( *(int*)a - *(int*)b );
  81. }
  82.  
  83. void shellSort(int numbers[], int array_size)
  84. {
  85. int i, j, increment, temp;
  86.  
  87. increment = 3;
  88. while (increment > 0) {
  89. for (i=0; i < array_size; i++) {
  90. j = i;
  91. temp = numbers[i];
  92. while ((j >= increment) && (numbers[j-increment] > temp)) {
  93. numbers[j] = numbers[j - increment];
  94. j = j - increment;
  95. }
  96. numbers[j] = temp;
  97. }
  98. if (increment/2 != 0)
  99. increment = increment/2;
  100. else if (increment == 1)
  101. increment = 0;
  102. else
  103. increment = 1;
  104. } // while
  105. }
  106.  
  107. void insertionSort(int numbers[], int array_size)
  108. {
  109. int i, j, temp;
  110.  
  111. for (i = 1; i < array_size; i++) {
  112. temp = numbers[i];
  113. j = i;
  114. while ((j > 0) && (numbers[j-1] > temp)) {
  115. numbers[j] = numbers[j-1];
  116. j = j - 1;
  117. }
  118. numbers[j] = temp;
  119. }
  120. }
  121.  
  122. void bubbleSort(int numbers[], int array_size)
  123. {
  124. int finished = 0, i, j, temp;
  125.  
  126. for (i = (array_size - 1); i >= 0; i--) {
  127. if (finished)
  128. break;
  129. finished = 1;
  130. for (j = 1; j <= i; j++) {
  131. if (numbers[j-1] > numbers[j]) {
  132. finished = 0;
  133. // swap
  134. temp = numbers[j-1];
  135. numbers[j-1] = numbers[j];
  136. numbers[j] = temp;
  137. }
  138. }
  139. }
  140. }
Comments on this Code Snippet
Sep 10th, 2005
0

Re: Comparing Sorting Routines

I always wondered how qsort was correctly implimented.
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Message:
Previous Thread in C Forum Timeline: Really simple thing...
Next Thread in C Forum Timeline: Rundll





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC