TheAlgorithms-C/sorting/shell_sort2.c

87 lines
2.1 KiB
C
Raw Normal View History

2020-06-06 21:51:49 +03:00
/**
* \file
* \brief [Shell sort algorithm](https://en.wikipedia.org/wiki/Shell_sort)
* implementation.
* \author [Krishna Vedala](https://github.com/kvedala)
*/
2020-04-20 23:26:35 +03:00
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARRAY_LEN(x) (sizeof(x) / sizeof((x)[0]))
2020-06-06 21:51:49 +03:00
/** Helper function to print array values */
void show_data(int *arr, long len)
2020-04-20 23:26:35 +03:00
{
for (long i = 0; i < len; i++) printf("%3d ", arr[i]);
2020-04-20 23:26:35 +03:00
printf("\n");
}
2020-06-06 21:51:49 +03:00
/** Function to swap values of two integers */
inline void swap(int *a, int *b)
2020-04-20 23:26:35 +03:00
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
/**
* Optimized algorithm - takes half the time as other
**/
2020-06-06 21:51:49 +03:00
void shell_sort(int *array, long LEN)
2020-04-20 23:26:35 +03:00
{
const int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
const int gap_len = 8;
2020-06-06 21:51:49 +03:00
long i, j, g;
2020-04-20 23:26:35 +03:00
for (g = 0; g < gap_len; g++)
{ // for each gap
2020-04-20 23:26:35 +03:00
int gap = gaps[g];
for (i = gap; i < LEN; i++)
{ // from gap position to the end
2020-04-20 23:26:35 +03:00
int tmp = array[i];
for (j = i; j >= gap && (array[j - gap] - tmp) > 0; j -= gap)
array[j] = array[j - gap];
array[j] = tmp;
}
}
#ifdef DEBUG
for (i = 0; i < LEN; i++) printf("%s\t", data[i]);
2020-04-20 23:26:35 +03:00
#endif
}
2020-06-06 21:51:49 +03:00
/** Main function */
2020-04-20 23:26:35 +03:00
int main(int argc, char *argv[])
{
int i;
2020-06-06 21:51:49 +03:00
long size = 500;
if (argc == 2)
size = atol(argv[1]);
else if (argc > 2)
fprintf(stderr, "Usage: ./shell_sort [number of values]\n");
int *array = (int *)malloc(size * sizeof(int));
int range = 500; // range of array values
2020-04-20 23:26:35 +03:00
double time_spent;
srand(time(NULL)); // initialize random number generator
2020-06-06 21:51:49 +03:00
for (i = 0; i < size; i++)
// fill array with random integers
2020-04-20 23:26:35 +03:00
array[i] = rand() % range + 1;
show_data(array, size); // show array before sorting
clock_t t1 = clock(); // start timer
shell_sort(array, size); // sort the array
clock_t t2 = clock(); // end timer
2020-04-20 23:26:35 +03:00
printf("Data Sorted\n");
show_data(array, size); // display array after sorting
2020-04-20 23:26:35 +03:00
printf("Time spent sorting: %.4g s\n", (t2 - t1) / CLOCKS_PER_SEC);
return 0;
}