-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellSort.cpp
More file actions
48 lines (48 loc) · 1.5 KB
/
shellSort.cpp
File metadata and controls
48 lines (48 loc) · 1.5 KB
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
47
48
#include<iostream>
using namespace std;
void display(int *a, int n){
int i;
for(i=0;i<n;i++){
cout<<a[i]<<" ";
}
}
void swap(int &a, int &b){
int temp;
temp=a;
a=b;
b=temp;
}
void shellSort(int *a, int n){
int i,gap=n/2, j; //initially Gap is considered half of size of array
//function will compare every value with the value present at a particular gap meaning ith element will be compared with (i+gap)th element
while(gap>0)
{
for(j=gap;j<n;j++) //j will be use to access every single element
{
for(i=j-gap;i>=0;i=i-gap)//i will be use to compare elements with their gap element
{ //if 0th element is compared with 3rd element and later 3rd element is swaped with 7th element
if(a[i]<=a[i+gap]) // then swapped element at 3rd will be again compared with 0th element using statement i=i-gap
break;
else
swap(a[i], a[i+gap]) ;
}
}
gap=gap/2; //Afrer Each cycle the gap is reduced to half
}
}
int main(){
int i,size;
cout<<"\nEnter Size of Array ";
cin>>size;
int arr[size];
cout<<"\nEnter elements ";
for(i=0;i<size;i++) {
cin>>arr[i];
}
cout<<"\nBefore Sorting : ";
display(arr, size);
shellSort(arr, size);
cout<<"\nAfter Sorting : ";
display(arr, size);
return 0;
}