-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.cpp
155 lines (136 loc) · 3.71 KB
/
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
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> InitArr(int size) {
srand((int) time(0));
vector<int> arr;
for (int i = 0; i < size; ++i) {
arr.push_back(rand() % size);
}
return arr;
}
void PrintArr(vector<int> arr) {
for (auto i : arr)
cout << i << "\t";
cout << endl;
return;
}
//选择排序
void SelectSort(vector<int> &arr) {
for (int i = 0; i < arr.size(); ++i) {
for (int j = i; j < arr.size(); ++j) {
if (arr[i] > arr[j]) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
}
//冒泡排序(交换排序)
void BubbleSort(vector<int> &arr) {
for (int i = 0; i < arr.size(); ++i) {
for (int j = 0; j < arr.size() - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
int tmp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = tmp;
}
}
}
}
//插入排序
void InsertSort(vector<int> &arr) {
for (int i = 1; i < arr.size(); ++i) {
int next = arr[i]; // 右手抓到一张扑克牌
int j = i - 1; // 拿在左手上的牌总是排序好的
while (j >= 0 && arr[j] > next) // 将抓到的牌与手牌从右向左进行比较
{
arr[j + 1] = arr[j]; // 如果该手牌比抓到的牌大,就将其右移
j--;
}
arr[j + 1] = next;
}
}
//归并排序
void Merge(vector<int> &arr, int left, int mid, int right) {
vector<int> a1;
vector<int> a2;
int i, j;
// 注意这个循环终止条件....
for (i = left; i <= mid; ++i) {
a1.push_back(arr[i]);
}
for (j = mid + 1; j <= right; j++) {
a2.push_back(arr[j]);
}
a1.push_back(INT32_MAX);
a2.push_back(INT32_MAX);
i = 0;
j = 0;
for (int k = left; k <= right; ++k) {
if (a1[i] > a2[j]) {
arr[k] = a2[j];
++j;
} else {
arr[k] = a1[i];
++i;
}
}
}
void MergeSort(vector<int> &arr, int left, int right) {
if (left < right) {
int mid = (right + left) / 2;
MergeSort(arr, left, mid);
MergeSort(arr, mid + 1, right);
Merge(arr, left, mid, right);
}
return;
}
//快速排序
void swap(vector<int> &arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
//每一趟选一个基准数,找出比它大的和比它小的个数,就能确定它的位置!
int partition(vector<int> &arr, int low, int high) {
// if (low >= high) return
int base = arr[high];
int tail = low - 1;
for (int i = low; i < high; i++) // 遍历基准以外的其他元素
{
if (arr[i] <= base) // 把小于等于基准的元素放到前一个子数组中
{
tail++;
if (tail == i) continue;
else swap(arr, tail, i);
}
}
swap(arr, tail + 1, high);
return tail + 1;
}
void QuickSort(vector<int> &arr, int low, int high) {
if (high < low) return;
int Index = partition(arr, low, high); //将表一分为二
QuickSort(arr, low, Index - 1); //递归对低子表递归排序
QuickSort(arr, Index + 1, high);
}
//堆排序
void HeapSortbystl(vector<int> &arr) {
make_heap(arr.begin(), arr.end());
sort_heap(arr.begin(), arr.end());
}
int main() {
vector<int> arr = InitArr(20);
PrintArr(arr);
// InsertSort(arr);
// SelectSort(arr);
// BubbleSort(arr);
// MergeSort(arr, 0, arr.size());
// QuickSort(arr, 0, arr.size() - 1);
HeapSortbystl(arr);
PrintArr(arr);
return 0;
}