常用排序代码模板

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <bits/stdc++.h>
using namespace std;

int nums[1000];
int n;

void Swap(int i, int j)
{
int val = nums[j];
nums[j] = nums[i];
nums[i] = nums[j];
}

/******************** 快排 ********************/
int Partition(int first, int last)
{
int pval = nums[first];
while (first < last)
{
while (first < last && pval <= nums[last])
--last;
nums[first] = nums[last];
while (first < last && nums[first] <= pval)
++first;
nums[last] = nums[first];
}
nums[first] = pval;
return first;
}

void QSort(int beg, int end)
{
if (beg + 1 < end)
{
int pivot = Partition(beg, end - 1);
QSort(beg, pivot);
QSort(pivot + 1, end);
}
}

/******************** 归并 ********************/
void Merge(int beg, int mid, int end)
{
const int len = end - beg;
int arr[len];
int i = beg, j = mid, k = 0;
while (i < mid && j < end)
{
if (nums[i] <= nums[j])
arr[k++] = nums[i++];
else
arr[k++] = nums[j++];
}
while (i < mid)
arr[k++] = nums[i++];
while (j < end)
arr[k++] = nums[j++];
for (i = 0, j = beg; i < len; ++i, ++j)
nums[j] = arr[i];
}

void MergeSort(int beg, int end)
{
if (beg + 1 < end)
{
int mid = (beg + end) / 2;
MergeSort(beg, mid);
MergeSort(mid, end);
Merge(beg, mid, end);
}
}

/******************** 堆排序 ********************/

int Parent(int i)
{
return (i + 1) / 2 - 1;
}

int Left(int i)
{
return (i + 1) * 2 - 1;
}

void AdjustTop(int top, int len)
{
int topVal = nums[top];
for (int child; Left(top) < len; top = child)
{
child = Left(top);
if (child + 1 < len && nums[child + 1] > nums[child])
++child;
if (topVal < nums[child])
nums[top] = nums[child];
else
break;
}
nums[top] = topVal;
}

void HeapSort(int len)
{
int last = len - 1;
for (int i = Parent(last); i >= 0; --i)
AdjustTop(i, len);
for (int last = len - 1; last > 0; --last)
{
Swap(0, last);
AdjustTop(0, last);
}
}


/******************** 希尔 ********************/
void ShellSort(int len)
{
for (int gap = len / 2; gap > 0; gap /= 2)
{
for (int i = gap; i < len; ++i)
{
int lastV = nums[i], j;
for (j = i; j >= gap && lastV < nums[j-gap]; j -= gap)
nums[j] = nums[j-gap];
nums[j] = lastV;
}
}
}

/******************** 插入 ********************/
void InsertionSort(int len)
{
for (int i = 1; i < len; ++i)
{
int lastV = nums[i], j;
for (j = i; j > 0 && lastV < nums[j-1]; --j)
nums[j] = nums[j-1];
nums[j] = lastV;
}
}

int main()
{
//while (cin >> n)
{
// for (int i = 0; i < n; ++i)
// cin >> nums[i];
n = 9;
int arr[9] = {1, 3, 4, 2, 0, 8, 4, 2, 7};
for (int i = 0; i < n; ++i)
nums[i] = arr[i];
//QSort(0, n);
//MergeSort(0, n);
HeapSort(n);
//InsertionSort(n);
//ShellSort(n);
cout << nums[n-1] << endl;
for (int i = 0; i < (n - 1); ++i)
{
if (i != 0)
cout << " ";
cout << nums[i];
}
if (n == 1)
cout << -1;
cout << endl;
}
return 0;
}