From 1abb8b20bbdffbfa1f585df06a28e439c92ef9b4 Mon Sep 17 00:00:00 2001 From: zeroway Date: Tue, 19 Feb 2019 16:31:26 +0000 Subject: [PATCH] BugFix : loop out of range will cause stack smashing Data Sorted Shell Sort Big O Notation: --> Best Case: O(n log(n)) --> Average Case: depends on gap sequence --> Worst Case: O(n) Time spent sorting: 0.000006 *** stack smashing detected ***: terminated Aborted --- sorting/shellSort.c | 98 +++++++++++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 39 deletions(-) diff --git a/sorting/shellSort.c b/sorting/shellSort.c index e81cad95..ac392a90 100644 --- a/sorting/shellSort.c +++ b/sorting/shellSort.c @@ -2,47 +2,67 @@ #include #include -void shellSort(int array[], int value){ - int i = value; - int j, k, tmp; - for (i = value / 2; i > 0; i = i / 2){ - for (j = i; j < value; j++){ - for(k = j - i; k >= 0; k = k - i){ - if (array[k+i] >= array[k]){ - break; - } - else{ - tmp = array[k]; - array[k] = array[k+i]; - array[k+i] = tmp; - } - } - } - } +#define ELEMENT_NR 20 +#define ARRAY_LEN(x) (sizeof(x)/sizeof((x)[0])) +const char *notation = "Shell Sort Big O Notation:\ + \n--> Best Case: O(n log(n)) \ + \n--> Average Case: depends on gap sequence \ + \n--> Worst Case: O(n)"; + +void show_data(int arr[], int len) +{ + int i; + + for (i = 0; i < len; i++) + printf("%3d ", arr[i]); + printf("\n"); } +void swap(int *a, int *b) +{ + int tmp; -int main(){ - - int array[20]; - int range = 500; - for(int i = 0; i < 100; i++){ - array[i] = rand() % range + 1; - } - int size = sizeof array / sizeof array[0]; - - - - clock_t start = clock(); - shellSort(array,size); - clock_t end = clock(); - double time_spent = (double)(end - start) / CLOCKS_PER_SEC; - - - printf("Data Sorted\n"); - printf("%s\n", "Shell Sort Big O Notation:\n--> Best Case: O(n log(n))\n--> Average Case: depends on gap sequence\n--> Worst Case: O(n)\n"); - printf("Time spent sorting: %f\n", time_spent); - - return 0; + tmp = *a; + *a = *b; + *b = tmp; } +void shellSort(int array[], int len) +{ + int i, j, gap; + + for (gap = len / 2; gap > 0; gap = gap / 2) + for (i = gap; i < len; i++) + for (j = i - gap; j >= 0 && array[j] > array[j + gap]; j = j - gap) + swap(&array[j], &array[j + gap]); +} + +int main(int argc, char *argv[]) +{ + int i; + int array[ELEMENT_NR]; + int range = 500; + int size; + clock_t start, end; + double time_spent; + + srand(time(NULL)); + for (i= 0; i < ELEMENT_NR; i++) + array[i] = rand() % range + 1; + + size = ARRAY_LEN(array); + + show_data(array, size); + start = clock(); + shellSort(array, size); + end = clock(); + time_spent = (double)(end - start) / CLOCKS_PER_SEC; + + printf("Data Sorted\n"); + show_data(array, size); + + printf("%s\n", notation); + printf("Time spent sorting: %f\n", time_spent); + + return 0; +}