-
Notifications
You must be signed in to change notification settings - Fork 209
/
Shell-Sort.cpp
53 lines (45 loc) · 840 Bytes
/
Shell-Sort.cpp
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
49
50
51
52
53
// C++ implementation of Shell Sort
#include<iostream>
using namespace std;
void ShellSort(int arr[], int n)
{
for(int gap=n/2;gap>0;gap /= 2)
{
for(int j = gap;j<n;j+=1)
{
int temp = arr[j];
int i=0;
for(i=j;(i>=gap)&&(arr[i-gap]>temp);i-=gap)
{
arr[i] = arr[i-gap];
}
arr[i] = temp;
}
}
}
int main()
{
int n;
cout<<"Enter the size of the array: "<<endl;
cin>>n;
int arr1[n],arr2[n];
cout<<"Enter "<<n<<" integers in any order"<<endl;
for(int i=0;i<n;i++)
{
cin>>arr1[i];
arr2[i]=arr1[i];
}
cout<<endl<<"Before Sorting: "<<endl;
for(int i=0;i<n;i++)
{
cout<<arr1[i]<<" ";
}
cout<<endl<<endl<<"SHELL SORT "<<endl;
ShellSort(arr1, n); // SHELL SORT CALLED HERE
cout<<endl<<"After Sorting: "<<endl;
for(int i=0;i<n;i++)
{
cout<<arr1[i]<<" ";
}
return 0;
}