News
  • Submission of Theory Assignments 6,7,8 of OS on 1st April
  • Case Study Presentation of OS on 4st April
  • CN and PNS Journal Submission on 6th April

DS (RM) Program - Shell Sort



 DS (RM) Program - Shell Sort







 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream.h>
#include<conio.h>
#define SIZE 25
void main()
{
 clrscr();
 int list[SIZE];
 int n,i;
 cout<<"\nEnter no of elements in list:";
 cin>>n;
 cout<<"\nEnter elements:";
 for(i=0;i<n;i++)
 {
  cin>>list[i];
 }
 cout<<"\nOriginal list:\n";
 for(i=0;i<n;i++)
 {
  cout<<list[i]<<"\t";
 }
 cout<<"\nSorted list using Shell Sort:\n";
 int last=n-1,incr,walker,hold,current;
incr=last/2;
 while(incr!=0)
 {
  current=0+incr;
  while(current<=last)
  {
   hold=list[current];
   walker=current-incr;
   while(walker>=0 && hold<list[walker])
   {
    list[walker+incr]=list[walker];
    walker=walker-incr;
   }
   list[walker+incr]=hold;
   current++;
  }
  incr=incr/2;
 }
 for(i=0;i<n;i++)
 {
  cout<<list[i]<<"\t";
 }
 getch();
}
Output:

Enter no of elements in list:5  

Enter elements:12               

45                              

86                              

77                              

90                                



Original list:                  

12      45      86      77      90

Sorted list using Shell Sort:   
12      45      77      86      90
© Copyright NMITD MCA Notes Published.. Blogger Templates
Back To Top