-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.cpp
More file actions
173 lines (163 loc) · 2.88 KB
/
Copy pathsort.cpp
File metadata and controls
173 lines (163 loc) · 2.88 KB
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
// sort.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void insert_sort(int a[], int len)
{
//1.find pos 2.get in
int watch,pos;
for (int i = 1; i < len; i++)
{
watch=a[i];
//1.find the pos,j is the pos
pos = 0;
while (watch > a[pos])
{
pos++;
}
//2.get in(move the array [j,i),then get in)
for (int k = i-1; k >= pos; k--)
{
a[k + 1] = a[k];
}
a[pos] = watch;
//this is debug
for (int q = 0; q < len; q++)
cout << a[q] <<' ';
cout << endl;
}
}
void half_sort(int a[], int len)
{
int watch, j = 0, pos, low, high, mid;
for (int i = 1; i < len; i++)
{
watch = a[i];
low = 0;
high = i - 1;
//find the pos
while (low <= high)
{
mid = (low + high / 2);
if (a[mid] > watch)
high = mid - 1;
else
low = mid + 1;
}
//high is the pos
for (int k = i - 1; k >= high; k--)
{
a[k + 1] = a[k];
}
a[high + 1] = watch;
}
}
void bub_sort(int a[], int len)
{
for (int j=len-1;j>0;j--)
for (int i = 0; i < j; i++)
{
if (a[i + 1] < a[i])
swap(a[i + 1], a[i]);
}
}
int * shellpass(int *sqList, int h, int len)
{
int watch,pos;
for (int i = 0; i < h; i++)
{
//插入排序
for (int j = i; j < len; j += h)
{
//1.find pos
watch=sqList[j];
pos = i;
while (watch > sqList[pos]) pos+=h;
//2.get in
for (int k = j - 1; k >= pos; k--)
{
sqList[k + h] = sqList[k];
}
sqList[pos] = watch;
}
}
return sqList;
}
void shell_sort(int a[],int len,int h[],int hlen)
{
int * temList = a;
for (int i = hlen - 1; i >= 0; i--)
{
cout << h[i] << endl;
temList = shellpass(temList, h[i], len);
for (int q = 0; q < len; q++)
cout << a[q] << ' ';
cout << endl;
}
}
void quick_sort(int a[], int low, int high)
{
if (low >= high)
{
return;
}
int first = low;
int last = high;
int key = a[first];/*用字表的第一个记录作为枢轴*/
while (first < last)
{
while (first < last && a[last] >= key)
{
--last;
}
a[first] = a[last];/*将比第一个小的移到低端*/
while (first < last && a[first] <= key)
{
++first;
}
a[last] = a[first];
/*将比第一个大的移到高端*/
}
a[first] = key;/*枢轴记录到位*/
quick_sort(a, low, first - 1);
quick_sort(a, first + 1, high);
}
void choose_sort(int a[], int len) {
int min=1000,pos;
for (int i = 0; i < len; i++)
{
//choose the min,record pos,
for (int j = i; j < len; j++)
{
if (a[j] < min)
{
min = a[j];
pos = j;
}
}
//swap
swap(a[pos], a[i]);
min = 1000;
}
}
int main()
{
int len,hlen;
int a[] = {0,4,3,2,1,5,6,7,8,9};
int h[] = { 1,3,6 };
hlen = sizeof(h) / sizeof(h[0]);
len = sizeof(a) / sizeof(a[0]);
//bub_sort(a, len);
//insert_sort(a, len);
//half_sort(a, len);
//shell_sort(a, len, h, hlen);
//quick_sort(a, 0, len - 1);
//choose_sort(a, len);
//print array
for (int i = 0; i < len; i++) {
cout << a[i] << ' ';
}
system("pause");
return 0;
}