datastruct_6

datastruct_6

Charles Lv7

排序相关函数

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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 100007
void swap(int* pa, int* pb);
void InsertSort(int A[], int n);
void Mid_InsertSort(int A[], int n);
void ShellSort(int A[], int n);
void BubbleSort(int A[], int n);
int Partition(int A[], int low, int high);
void QuickSort(int A[], int low, int high);
void SelectSort(int A[], int n);
void HeadAdjust(int A[], int k, int len);
void BuildMaxHeap(int A[], int len);
void HeapSort(int A[], int len);
void Merge(int A[], int low, int mid, int high);
void MergeSort(int A[], int low, int high);
int a[M], n;
int main() {
int i, n;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
return 0;
}
void InsertSort(int A[], int n) {
int i, j, temp;
for (i = 1; i < n; i++) {
if (A[i] < A[i - 1]) {
temp = A[i];
for (j = i - 1; j >= 0 && A[j] > temp; --j) {
A[j + 1] = A[j];
}
A[j + 1] = temp;
}
}
}
void Mid_InsertSort(int A[], int n) {
int i, j, high, low, mid;
for (i = 2; i <= n; i++) {
A[0] = A[i];
low = 1, high = i - 1;
while (low <= high) {
mid = low + (high - low) / 2;
if (A[mid] > A[0]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
for (j = i - 1; j >= high + 1; --j) {
A[j + 1] = A[j];
}
A[high + 1] = A[0];
}
}
void ShellSort(int A[], int n) {
int d, i, j;
for (d = n / 2; d >= 1; d = d / 2) {
for (i = d + 1; i <= n; i++) {
if (A[i] < A[i - d]) {
A[0] = A[i];
for (j = i - d; j > 0 && A[0] < A[j]; j -= d) {
A[j + d] = A[j];
}
A[j + d] = A[0];
}
}
}
}
void swap(int* pa, int* pb) {
int* temp = pa;
pa = pb;
pb = temp;
}
void BubbleSort(int A[], int n) {
int i, j;
for (i = 0; i < n - 1; i++) {
bool flag = false;
for (j = n - 1; j > i; j--) {
if (A[j - 1] > A[j]) {
swap(&A[j - 1], &A[j]);
flag = true;
}
}
if (flag == false) {
return;
}
}
}
int Partition(int A[], int low, int high) {
int pivot = A[low];
while (low < high) {
while (low < high && A[high] >= pivot) {
--high;
}
A[low] = A[high];
while (low < high && A[low] <= pivot) {
++low;
}
A[high] = A[low];
}
A[low] = pivot;
return low;
}
void QuickSort(int A[], int low, int high) {
if (low < high) {
int pivotpos = Partition(A, low, high);
QuickSort(A, low, pivotpos - 1);
QuickSort(A, pivotpos + 1, high);
}
}
void SelectSort(int A[], int n) {
int i, j;
for (i = 0; i < n - 1; i++) {
int min = i;
for (j = i + 1; j < n; j++) {
if (A[j] < A[min]) {
min = j;
}
}
if (min != i) {
swap(&A[i], &A[min]);
}
}
}
void HeadAdjust(int A[], int k, int len) {
A[0] = A[k];
int i;
for (i = 2 * k; i <= len; i *= 2) {
if (i < len && A[i] < A[i + 1]) {
i++;
}
if (A[0] >= A[i]) {
break;
} else {
A[k] = A[i];
k = i;
}
}
A[k] = A[0];
}
int i;
for (i = len / 2; i > 0; i--) {
HeadAdjust(A, i, len);
}
}
void HeapSort(int A[], int len) {
BuildMaxHeap(A, len);
int i;
for (i = len; i > 1; i--) {
swap(&A[i], &A[1]);
HeadAdjust(A, 1, i - 1);
}
}
int b[M];
void Merge(int a[], int low, int mid, int high) {
int i, j, k;
for (i = low, j = mid + 1, k = 0; i <= mid && j <= high; k++) {
cnt++;
if (a[i] <= a[j]) {
b[k] = a[i++];
} else {
b[k] = a[j++];
}
}
if (i == mid + 1) {
while (j <= high)
b[k++] = a[j++];
}
if (j == high + 1) {
while (i <= mid)
b[k++] = a[i++];
}
for (j = 0, i = low; j < k; i++, j++) {
a[i] = b[j];
}
}
void MergeSort(int a[], int low, int high) {
if (low < high) {
int mid = low + (high - low) / 2;
MergeSort(a, low, mid);
MergeSort(a, mid + 1, high);
Merge(a, low, mid, high);
}
}

  • Title: datastruct_6
  • Author: Charles
  • Created at : 2022-12-29 11:01:56
  • Updated at : 2023-07-18 21:14:54
  • Link: https://charles2530.github.io/2022/12/29/datastruct-6/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
datastruct_6