Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * C Program To Sort array in ascending order using Shell Sort.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #define SIZE 100
- static inline
- void swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; }
- void shell(int array[], int n)
- {
- for (int i = n / 2; i > 0; i /= 2) {
- for (int j = i; j < n; j++) {
- for (int k = j - i; k >= 0; k = k - i) {
- if (array[k + i] > array[k]) break;
- else swap(&array[k], &array[k + i]);
- }
- }
- }
- }
- int main(int argc, char *argv[])
- {
- int array[SIZE];
- printf("%s - %i\n", argv[0], SIZE);
- // create an array of random ints
- srand(time(NULL));
- for (int i = 0; i < SIZE; i++) array[i] = rand();
- shell(array, SIZE);
- // output array
- for (int i = 0; i < SIZE; i++) printf("%i\n", array[i]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement