-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathShellSort.cpp
More file actions
41 lines (33 loc) · 827 Bytes
/
ShellSort.cpp
File metadata and controls
41 lines (33 loc) · 827 Bytes
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
#include <iostream>
using namespace std;
// shell sort implementation
int shellSort(int arr[], int N)
{
for (int gap = N/2; gap > 0; gap /= 2)
{
for (int i = gap; i < N; i += 1)
{
//sort sub lists created by applying gap
int temp = arr[i];
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];
arr[j] = temp;
}
}
return 0;
}
int main()
{
int arr[] = {45,23,53,43,18,24,8,95,101}, i;
//Calculate size of array
int N = sizeof(arr)/sizeof(arr[0]);
cout << "Array to be sorted: \n";
for (int i=0; i<N; i++)
cout << arr[i] << " ";
shellSort(arr, N);
cout << "\nArray after shell sort: \n";
for (int i=0; i<N; i++)
cout << arr[i] << " ";
return 0;
}