SHELL SORT
Implemented using C Language
C Language Code:
SHELL.C
// SHELL SORTING using C Language
#include <stdlib.h>
#include <stdio.h>
#include <process.h>
#define NUM_ITEMS 10
void shellSort(int numbers[], int array_size);
void prnArray(int numbers[], int array_size) ;
int numbers[NUM_ITEMS];
int main()
{
int i;
clrscr() ;
//seed random number generator
srand(getpid());
//fill array with random integers
for (i = 0; i < NUM_ITEMS; i++)
numbers[i] = rand();
printf("\n Before SHELL Sort.\n");
prnArray(numbers, NUM_ITEMS) ;
//perform shell sort on array
shellSort(numbers, NUM_ITEMS);
printf("\n After SHELL Sort.\n");
prnArray(numbers, NUM_ITEMS) ;
}
void shellSort(int numbers[], int array_size)
{
int i, j, increment, temp;
increment = 3;
while (increment > 0)
{
for (i=0; i < array_size; i++)
{
j = i;
temp = numbers[i];
while ((j >= increment) && (numbers[j-increment] > temp))
{
numbers[j] = numbers[j - increment];
j = j - increment;
}
numbers[j] = temp;
}
if (increment/2 != 0)
increment = increment/2;
else if (increment == 1)
increment = 0;
else
increment = 1;
}
}
{
int x ;
for(x=0; x < array_size; x++)
printf("%d\t", numbers[x]) ;
}
No comments:
Post a Comment