-
Notifications
You must be signed in to change notification settings - Fork 0
/
shellsort.cpp
43 lines (42 loc) · 928 Bytes
/
shellsort.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
#include<iostream>
using namespace std;
void shellSort(int arr[], int size)
{
for(int gap = size / 2;gap > 0; gap /= 2)
{
for(int j = gap;j < size; 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 size;
cout<<"\nEnter the size of the array: ";
cin>>size;
int arr[size];
cout<<"\nEnter "<<size<<" elements in random order: ";
for(int i = 0;i < size; i++)
{
cin>>arr[i];
}
cout<<"\nBefore sorting: ";
for(int i = 0;i < size; i++)
{
cout<<arr[i]<<" ";
}
shellSort(arr, size);
cout<<"\nAfter sorting: ";
for(int i = 0;i < size; i++)
{
cout<<arr[i]<<" ";
}
return 0;
}