Advertisement
rjcostales

shell.c

Jun 9th, 2021
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. /*
  2.  * C Program To Sort array in ascending order using Shell Sort.
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <time.h>
  8.  
  9. #define SIZE 100
  10.  
  11. static inline
  12. void swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; }
  13.  
  14. void shell(int array[], int n)
  15. {
  16.     for (int i = n / 2; i > 0; i /= 2) {
  17.         for (int j = i; j < n; j++) {
  18.             for (int k = j - i; k >= 0; k = k - i) {
  19.                 if (array[k + i] > array[k]) break;
  20.                 else swap(&array[k], &array[k + i]);
  21.             }
  22.         }
  23.     }
  24. }
  25.  
  26. int main(int argc, char *argv[])
  27. {
  28.     int array[SIZE];
  29.  
  30.     printf("%s - %i\n", argv[0], SIZE);
  31.  
  32.     // create an array of random ints
  33.     srand(time(NULL));
  34.     for (int i = 0; i < SIZE; i++) array[i] = rand();
  35.  
  36.     shell(array, SIZE);
  37.  
  38.     // output array
  39.     for (int i = 0; i < SIZE; i++) printf("%i\n", array[i]);
  40.  
  41.     return 0;
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement