-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.cpp
More file actions
38 lines (26 loc) · 784 Bytes
/
ShellSort.cpp
File metadata and controls
38 lines (26 loc) · 784 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
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main(){
int array[] = {56,-1,5,43,67,99,4};
int arrayLength = sizeof(array)/sizeof(array[0]);
cout << "Array before ShellSort: ";
for(auto x : array) cout << x << " ";
cout << endl;
for(int gap = arrayLength/2; gap>0 ; gap /= 2){
for(int i = gap; i<arrayLength ; i++){
int newElement = array[i];
int j = i;
while (j >= gap && array[j-gap] > newElement)
{
array[j] = array[j-gap];
j = j - gap;
}
array[j] = newElement;
}
}
cout << "Array after ShellSort: ";
for(auto x : array) cout << x << " ";
cout << endl;
}