-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathShell_Sort
More file actions
46 lines (35 loc) · 1.06 KB
/
Shell_Sort
File metadata and controls
46 lines (35 loc) · 1.06 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
#include <iostream>
using namespace std;
// function to sort array using shellSort
int shellSort(int arr[], int n) {
// Start with a considerable gap, then reduce the gap
for (int interval = n/2; interval > 0; interval /= 2) {
// Do a gapped insertion sort for this gap size.
for (int i = interval; i < n; i += 1) {
int temp = arr[i];
int j;
for (j = i; j >= interval && arr[j - interval] > temp; j -= interval)
arr[j] = arr[j - interval];
// put temp (the original a[i]) in its correct location
arr[j] = temp;
}
}
return 0;
}
void printArray(int arr[], int n) {
for (int i=0; i<n; i++)
cout << arr[i] << " ";
}
int main() {
int n;
int arr[1000];
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout << "Array before"<<endl;
printArray(arr, n);
shellSort(arr, n);
cout << "Array after :"<<endl;
printArray(arr, n);
return 0;
}