Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
shell_sort2.c File Reference

Shell sort algorithm implementation. More...

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Include dependency graph for shell_sort2.c:

Functions

void show_data (int *arr, long len)
 Helper function to print array values.
 
void swap (int *a, int *b)
 Swap two integer variables.
 
void shell_sort (int *array, long LEN)
 Shell sort algorithm.
 
int main (int argc, char *argv[])
 Main function.
 

Detailed Description

Shell sort algorithm implementation.

Author
Krishna Vedala

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Main function.

67{
68 int i;
69 long size = 500;
70 if (argc == 2)
71 size = atol(argv[1]);
72 else if (argc > 2)
73 fprintf(stderr, "Usage: ./shell_sort [number of values]\n");
74
75 int *array = (int *)malloc(size * sizeof(int));
76 int range = 500; // range of array values
77 double time_spent;
78
79 srand(time(NULL)); // initialize random number generator
80 for (i = 0; i < size; i++)
81 // fill array with random integers
82 array[i] = rand() % range + 1;
83
84 show_data(array, size); // show array before sorting
85 clock_t t1 = clock(); // start timer
86 shell_sort(array, size); // sort the array
87 clock_t t2 = clock(); // end timer
88
89 printf("Data Sorted\n");
90 show_data(array, size); // display array after sorting
91
92 printf("Time spent sorting: %.4g s\n", (t2 - t1) / CLOCKS_PER_SEC);
93
94 free(array);
95 return 0;
96}
void shell_sort(int *array, long LEN)
Shell sort algorithm.
Definition shell_sort2.c:41
void show_data(int *arr, long len)
Helper function to print array values.
Definition shell_sort2.c:16
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition malloc_dbg.h:18
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26
Definition prime_factoriziation.c:25
Here is the call graph for this function: