-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
shell_sort.cpp
37 lines (33 loc) · 935 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
#include <iostream>
int main() {
int size = 10;
int* array = new int[size];
// Input
std::cout << "\nHow many numbers do want to enter in unsorted array : ";
std::cin >> size;
std::cout << "\nEnter the numbers for unsorted array : ";
for (int i = 0; i < size; i++) {
std::cin >> array[i];
}
// Sorting
for (int i = size / 2; i > 0; i = i / 2) {
for (int j = i; j < size; j++) {
for (int k = j - i; k >= 0; k = k - i) {
if (array[k] < array[k + i]) {
break;
} else {
int temp = array[k + i];
array[k + i] = array[k];
array[k] = temp;
}
}
}
}
// Output
std::cout << "\nSorted array : ";
for (int i = 0; i < size; ++i) {
std::cout << array[i] << "\t";
}
delete[] array;
return 0;
}