Get Email Subscription

Enter your email address:

Delivered by FeedBurner

Wednesday, April 21, 2010

This program is uesd for Shell Sort. (C++)


 
#include < stdlib.h>
#include < stdio.h>
#include< conio.h>
#include < iostream.h>
#define NUM_ITEMS 15
 
void shellSort(int numbers[], int array_size);
int numbers[NUM_ITEMS];
int main()
{
clrscr();
int i;
//fill array with random integers
for (i = 0; i < NUM_ITEMS; i++)
numbers[i] = rand() %100;
cout< < "\nUnsorted list is as follows\n";
for (i = 0; i < NUM_ITEMS; i++)
cout< < " "< < numbers[i];
//perform shell sort on array
shellSort(numbers, NUM_ITEMS);
cout< < "\n\nSorted list is as follows:\n";
for (i = 0; i < NUM_ITEMS; i++)
cout< < " "< < numbers[i];
}
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;
}
}
 

Related Posts by Categories



0 comments:

About This Blog

Lorem Ipsum

  © Blogger templates Newspaper III by Ourblogtemplates.com 2008

Back to TOP