-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShellSort.cpp
47 lines (39 loc) · 859 Bytes
/
ShellSort.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
38
39
40
41
42
43
44
45
46
47
#include <vector>
#include <iostream>
using namespace std;
void ShellSort(int A[], int n)
{
int d, i, j;
for (d = n / 2; d >= 1; d /= 2)
{
for (i = d; i < n; i++)
{
if (A[i] < A[i - d])
{
int temp = A[i];
for (j = i - d; j >= 0 && A[j] > temp; j -= d)
{
A[j + d] = A[j];
}
A[j + d] = temp;
}
}
}
}
void printNums(int nums[], int n)
{
for (int i = 0; i < n; i++)
cout << nums[i] << " ";
cout << endl;
}
int main()
{
int test[12] = {4, 2, 7, 10, 3, 9, 100, 64, 77, 92, 101, 50};
int n = sizeof(test) / sizeof(test[0]);
// 排序前打印
printNums(test, n);
ShellSort(test, n);
// 排序后打印
printNums(test, n);
return 0;
}