-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell_sort1.cpp
More file actions
44 lines (40 loc) · 987 Bytes
/
Shell_sort1.cpp
File metadata and controls
44 lines (40 loc) · 987 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
42
43
44
#include <iostream>
using namespace std;
// Shell sort
void shellSort(int array[], int n) {
// Rearrange elements at each n/2, n/4, n/8, ... intervals
for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i += 1) {
int temp = array[i];
int j;
for (j = i; j >= gap && array[j - gap] > temp; j -= gap) {
array[j] = array[j - gap];
}
array[j] = temp;
}
}
}
// Print an array
void printArray(int array[], int size) {
int i;
for (i = 0; i < size; i++)
cout << array[i] << " ";
cout << endl;
}
// Driver code
int main() {
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int data[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>data[i];
}
// int data[] = {9, 8, 3, 7, 5, 6, 4, 1};
//int size = sizeof(data) / sizeof(data[0]);
shellSort(data, n);
cout << "Sorted array: \n";
printArray(data, n);
}