Ladies_Man

#SORT__ sHELL Sort

Dec 15th, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.44 KB | None | 0 0
  1. //http://sorting.at/
  2. //
  3.  
  4.  
  5. void shellsort(unsigned long nel, int (*compare)(unsigned long, unsigned long), void (*swap)(unsigned long, unsigned long))
  6. {
  7.     int i = 2, max_fib_index = 2;
  8.     unsigned long f[nel];
  9.     f[0] = 1;
  10.     f[1] = 1;
  11.     for (i; ;i++) {
  12.         f[i] = f[i-1] + f[i-2];
  13.         if (f[i] > nel) break;
  14.         max_fib_index++;
  15.     }
  16.     int gap = f[max_fib_index], ready = 0;
  17.  
  18.     int j;
  19.     do {
  20.            gap = f[max_fib_index];
  21.        i = 0;
  22.        ready = 0;
  23.  
  24.        while ((i + gap) < nel){
  25.                 j = i + gap;
  26.         while((j >= gap) && (compare(j, j - gap) < 0)) {
  27.                         swap(j, j - gap);
  28.                         j = j - gap;
  29.         }
  30.         i++;
  31.        }
  32.        max_fib_index--;
  33.      } while (gap > 1);
  34. }
  35.  
  36.  
  37. //============================================================================
  38.  
  39.  
  40. int compare(unsigned long i, unsigned long j)
  41. {
  42.         if (i <= j) {
  43.                 printf("COMPARE␣%d␣%d\n", i, j);
  44.         } else {
  45.                 printf("COMPARE␣%d␣%d\n", j, i);
  46.         }
  47.  
  48.         if (array[i] == array[j]) return 0;
  49.         return array[i] < array[j] ? -1 : 1;
  50. }
  51.  
  52. void swap(unsigned long i, unsigned long j)
  53. {
  54.         if (i <= j) {
  55.                 printf("SWAP␣%d␣%d\n", i, j);
  56.         } else {
  57.                 printf("SWAP␣%d␣%d\n", j, i);
  58.         }
  59.  
  60.         int t = array[i];
  61.         array[i] = array[j];
  62.         array[j] = t;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment