简单排序算法实现

简单排序算法实现

闲来无事,简单回顾了一下排序算法

  • 简单插入排序,哈希排序
  • 冒泡排序,快速排序
  • 简单选择排序,堆排序
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <stdio.h>
#include<stdlib.h>

void insertSortForward(int orig[], int size);
void insertSortBackward(int orig[], int size);
void bubbleSort(int orig[], int size);
void selectSort(int orig[], int size);
void shellSort(int orig[], int size);
void HeapSort(int orig[], int size);
void QuickSort(int[], int, int);
void swap(int *, int *);
int Partition(int orig[], int left, int right);
void BuildHeap(int orig[],int size);
void HeapAdjust(int[], int, int);

int main() {
int a[] = { 1,3,2,4,5,7,0,8,8 };
//bubbleSort(a, 7);
//insertSortForward(a, 8);
//insertSortBackward(a, 8);
//selectSort(a, 8);
//shellSort(a, 8);
//HeapSort(a, 9);
QuickSort(a, 0, sizeof(a)/sizeof(a[0])-1);
for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
printf("%d ", a[i]);
printf("\n");
system("pause");
return 0;
}

void shellSort(int orig [], int size) //Shell排序
{
int i, j, k, t;
k = (size / 2) % 2 == 0 ? size / 2 + 1 : size / 2;
while (k > 0)
{
for (int j = k; j < size; j++) {
t = orig[j];
i = j - k;
while (i >= 0 && t < orig[i]) {
orig[i+k] = orig[i];
orig[i] = t;
i = i - k;
}
}
k /= 2;
if (k==0)
{
break;

}
}
for (int i = 0; i < size; i++)
printf("%d ", orig[i]);
printf("\n");

}
void HeapSort(int orig[], int size)
{
BuildHeap(orig, size);
for (int i = 0; i < size; i++)
printf("%d ", orig[i]);
printf("\n");
while(size > 0)
{
printf("%d ", orig[0]);
orig[0] = orig[ --size];
HeapAdjust(orig, 0,size);
}
}
void QuickSort(int orig[], int left, int right)
{
if (left < right) {

int point = Partition(orig, left, right);
QuickSort(orig, left, point - 1);
QuickSort(orig, point + 1, right);
}
}
void swap(int *a, int*b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int Partition(int orig[], int left, int right)
{
int prikey = orig[right];
while (left<right)
{
while (left<right&&prikey >= orig[left]) left++;
swap(&orig[left], &orig[right]);
while (left<right&& prikey <= orig[right]) right--;
swap(&orig[left], &orig[right]);
}
return left;
}
void BuildHeap(int orig[], int size)
{
int k;
for (k=size/2-1; k>=0; k--)
{
HeapAdjust(orig, k, size);
}


}
void HeapAdjust(int orig[], int adjustPort, int size) {

int min; //记录左右节点中最小的
int nextPoint; // 下一步
while (adjustPort*2+1<size-1) // 还有孩子
{
int leftChild = adjustPort * 2+1;
int rightChild = adjustPort * 2 + 2;
min = orig[leftChild];
nextPoint = leftChild;
if (rightChild<=size-1&&orig[rightChild]<orig[leftChild])
{
min = orig[rightChild];
nextPoint = rightChild;
}
if (orig[adjustPort]>min)
{
orig[nextPoint] = orig[adjustPort];
orig[adjustPort] = min;
adjustPort = nextPoint;
}
else
{
break;
}
}
}
void insertSortForward(int orig[], int size)
{
int i, j;
for (i = 1; i < size; i++) {
int temp = orig[i];
int index = i;
j = i - 1;
while (j >= 0 && orig[j] > temp)
{
orig[index] = orig[j];
index = j;
j--;
}
orig[j + 1] = temp;
}
for (int i = 0; i < size; i++)
printf("%d ", orig[i]);
printf("\n");
}

void insertSortBackward(int orig[], int size) // 从后往前
{
int i, j;
int left, right;
for (i = 1; i < size; i++) {
for (j = 0; j < i; j++) {
if (orig[i] <= orig[j])
{
left = orig[i];
right = i;
while (i - j)
{
orig[i] = orig[i - 1];
i--;
}
i = right;
orig[j] = left;
}
}

}
for (int i = 0; i < size; i++)
printf("%d ", orig[i]);
printf("\n");
}

void bubbleSort(int orig[], int size) {
int i = 0;
int j = 0;
for (i = 0; i < size; i++) {
for (j = 0; j < size - i; j++) {
if (orig[j] > orig[j + 1]) {
int temp = 0;
temp = orig[j];
orig[j] = orig[j + 1];
orig[j + 1] = temp;
}
}
}
for (int i = 0; i < size; i++)
printf("%d ", orig[i]);
printf("\n");
}

void selectSort(int orig[], int size)
{
for (int i = 0; i < size; i++) {
int min = orig[i];
int index = i;
for (int j = i+1; j < size; j++)
{
if (min > orig[j]) {
min = orig[j];
index = j;
}
}
orig[index] = orig[i];
orig[i] = min;
}
for (int i = 0; i < size; i++)
printf("%d ", orig[i]);
printf("\n");
}