New Member FAQ | Forums | Earn Revenue


Resources Entrance Ask Experts Exam Papers Jobs English Projects Universities Colleges Courses Schools Training My India



My Profile
Active Members
TodayLast 7 Days more...



Awards & Gifts
Online Exams

Fresher Jobs


Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian cities including Bangalore, Chennai, Hyderabad, Pune or Kochi

Resources


Find educational articles, blogs, discussion threads and other resources.

Colleges


Find details about any college in India or search for courses.

website counter



Resources » Articles/Knowledge Sharing » Computer & Technology »

Program to sort list of elements using shell sort.


Posted Date: 03 Jun 2008    Resource Type: Articles/Knowledge Sharing    Category: Computer & Technology
Author: VidyaMember Level: Diamond    
Rating: 3 out of 53 out of 53 out of 5Points: 1



Program to sort list of elements using shell sort.



#include
#include
#define SIZE 20

/**************Function Declaration Begin**********/
void get_data(int A[],int n);
void shell_sort(int A[],int n);
void show_data(int A[],int n);
/**************Function Declaration End**********/

void main()
{
int n,A[SIZE];
clrscr();
printf(“\n\t\t Program for selection sort”);
printf(“\n\t\t How many numbers do you want to store in the array:”);
scanf(“%d”,&n);
get_data(A,n);
shell_sort(A,n);
show_data(A,n);
getch();
}


/********** shell sort technique **********/
/********** Function Definition begins **********/
void shell_sort(int A[],int n)
{
int i,j,k,inc,temp;
inc = 1;
while (inc <= n)
{
inc = inc * 3 + 1;
}
do
{
inc = inc / 3;
i = inc + 1;
printf(“\nincrement = %d\n”,inc);
printf(“\nElements at %d are swapped : \n”,inc);
while (i <= n)
{
temp = A[i];
j = i;
while ((A[j - inc]) > temp)
{
printf(“swapping %d and %d\n”,temp,A[j-inc]);
A[j] = A[j - inc];
j = j - inc;
if (j <= inc)
break;
}
A[j] = temp;
i++;
}
printf(“\n after pass\n”);
for (k = 1; k <= n; k++)
printf(“%5d”,A[k]);
printf(“\n”);
}
while (inc != 1);

}
/********** Function Definition ends **********/


/********** inputting elements **********/
/********** Function Definition begins **********/
void get_data( int A[],int n)
{
int i;
printf(“\nEnter %d elements in the array:\n”,n);
for (i=1;i<=n;i++)
scanf(“%d”,&A[i]);
printf(“\nArray before sorting: “);
for(i=1;i<=n;i++)
printf(“%d “,A[i]);
printf(“\n”);
}
/********** Function Definition ends **********/

/********** outputting elements **********/
/********** Function Definition begins **********/
void show_data(int A[],int n)
{
int i;
printf(“\nArray after sorting: “);
for(i=1;i<=n;i++)
{
printf(“%d “,A[i]);
}
printf(“\n”);
}
/********** Function Definition ends **********/
? OUTPUT
Program for selection sort
How many numbers do you want to store in the array:6
Enter 6 elements in the array:
66
55
44
33
22
11
Array before sorting: 66 55 44 33 22 11
increment = 4
Elements at 4 are swapped :
swapping 22 and 66
swapping 11 and 55
after pass
22 11 44 33 66 55
increment = 1
Elements at 1 are swapped :
swapping 11 and 22
swapping 33 and 44
swapping 55 and 66
after pass
11 22 33 44 55 66
Array after sorting: 11 22 33 44 55 66




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Interview Questions Answers On Bug Tracking
Previous Resource: some facts about computers and IT
Return to Discussion Resource Index
Post New Resource
Category: Computer & Technology


Post resources and earn money!
 
More Resources



Advertise Here





Contact Us   Advertise   Editors    Privacy Policy    Terms Of Use   

ISC Technologies.
2006 - 2009 All Rights Reserved.