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.
|
Resources » Articles/Knowledge Sharing » Computer & Technology »
Program to sort list of elements using shell sort.
|
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.
|
|
Advertise Here
|