现在的位置: 首页 > 综合 > 正文

排序算法

2012年02月11日 ⁄ 综合 ⁄ 共 6139字 ⁄ 字号 评论关闭

a. 排序算法
b. 归并排序
c. 快速排序
d. 堆排序
e. 选择排序
f. 希尔排序

Sortable_list.h

Sortable.h

1 #include"Key.h"
2 #include"List.h"
3 #include"Timer.h"
4 #include"Random.h"
5 typedef Key Record;
6
7 template <class Record>
8  class Sortable_list:public List<Record>
9 {
10  public:
11 Sortable_list();
12 Sortable_list(Sortable_list<Record> &s);
13 void insertion_sort();
14 void selection_sort();
15 void shell_sort();
16 void merge_sort();
17 void quick_sort();
18 void heap_sort();
19  private:
20 int max_key(int low, int high);
21 void swap(int low, int high);
22 void sort_interval(int start, int increment);
23 void merge(int low, int high);
24 void recursive_merge_sort(int low, int high);
25 void recursive_quick_sort(int low, int high);
26 int partition(int low, int high);
27 void insert_heap(const Record &current, int low, int high);
28 void build_heap();
29 };
30
31 template <class Record>
32 Sortable_list<Record>::Sortable_list()
33 {
34 count = 0;
35 }
36
37 template <class Record>
38 Sortable_list<Record>::Sortable_list(Sortable_list<Record> &s)
39 {
40 count = s.count;
41 for (int i = 0; i < count; i++)
42 entry[i] = s.entry[i];
43 }
44
45 template <class Record>
46 void Sortable_list<Record>::insertion_sort()
47 {
48 int first_unsorted;
49 int position;
50 Record current;
51
52 for (first_unsorted = 1; first_unsorted < count; first_unsorted++) // < count 因为max entry[count-1]
53 {
54 if (entry[first_unsorted] < entry[first_unsorted-1]) //if true 互换
55 {
56 position = first_unsorted;
57 current = entry[first_unsorted];
58 do
59 {
60 entry[position] = entry[position-1];
61 position--;
62 } while(position > 0 && entry[position-1] > current);
63 entry[position] = current;
64 }
65 }
66 }
67
68 template <class Record>
69 void Sortable_list<Record>::selection_sort()
70 {
71 for (int position = count - 1; position > 0; position--)
72 {
73 int max = max_key(0, position); //先找出最大的,然后放在后面
74 swap(max, position);
75 }
76 }
77
78 template <class Record>
79 int Sortable_list<Record>::max_key(int low, int high)
80 {
81 int largest, current;
82 largest = low;
83 for (current = low + 1; current <= high; current++)
84 {
85 if (entry[largest] < entry[current])
86 largest = current; //下标操作
87 }
88 return largest;
89 }
90
91 template <class Record>
92 void Sortable_list<Record>::swap(int low, int high)
93 {
94 Record temp;
95 temp = entry[low];
96 entry[low] = entry[high];
97 entry[high] = temp;
98 }
99
100 template <class Record>
101 void Sortable_list<Record>::shell_sort()
102 {
103 int increment, // spacing of entries in sublist
104 start; // starting point of sublist
105 increment = count;
106 do
107 {
108 increment = increment / 3 + 1;
109 for (start = 0; start < increment; start++)
110 sort_interval(start, increment); //modified insertion sort
111 } while (increment > 1);
112 }
113
114 template <class Record>
115 void Sortable_list<Record>::sort_interval(int start, int increment)
116 {
117 int first_unsorted = 0; // position of first unsorted entry
118 int place = 0; // searches sorted part of list
119 Record current; // used to swap entries
120 for (first_unsorted = start +increment; first_unsorted < count; first_unsorted += increment)
121 {
122 if (entry[first_unsorted] < entry[first_unsorted - increment])
123 {
124 place = first_unsorted;
125 current = entry[first_unsorted];
126 // Pull entry[first_unsorted] out of the list
127 do
128 {
129 // Shift all previous entries one place down the list
130 entry[place] = entry[place - increment];
131 // until the proper place is found
132 place -= increment;
133 // Position place is now availabe for insertion
134 } while (place > start && entry[place - increment] > current);
135 entry[place] = current;
136 }
137 }
138 }
139
140 template <class Record>
141 void Sortable_list<Record>::merge_sort()
142 {
143 recursive_merge_sort(0, count-1);
144 }
145
146 template <class Record>
147 void Sortable_list<Record>::recursive_merge_sort(int low, int high)
148 {
149 if (high > low)
150 {
151 recursive_merge_sort(low, (high + low) / 2);
152 recursive_merge_sort( (high + low) / 2 + 1, high);
153 merge(low, high);
154 }
155 }
156
157 template <class Record>
158 void Sortable_list<Record>::merge(int low, int high)
159 {
160 Record* temp = new Record[high - low + 1];
161 int index = 0;
162 int index1 = low, mid = (low + high) / 2, index2 = mid + 1;
163 while (index1 <= mid && index2 <= high)
164 {
165 if (entry[index] < entry[index2])
166 temp[index++] = entry[index1++];
167 else
168 temp[index++] = entry[index2++];
169 }
170 while (index1 <= mid)
171 temp[index++] = entry[index1++];
172 while (index2 <= high)
173 temp[index++] = entry[index2++];
174 for (index = low; index <= high; index++)
175 entry[index] = temp[index - low];
176 delete []temp;
177 }
178
179 template <class Record>
180 void Sortable_list<Record>::quick_sort()
181 {
182 recursive_quick_sort(0, count - 1);
183 }
184
185 template <class Record>
186 void Sortable_list<Record>::recursive_quick_sort(int low, int high)
187 {
188 int pivot_position;
189 if (low < high)
190 {
191 pivot_position = partition(low, high);
192 recursive_quick_sort(low, pivot_position - 1);
193 recursive_quick_sort(pivot_position + 1, high);
194 }
195 }
196
197 template <class Record>
198 int Sortable_list<Record>::partition(int low, int high)
199 {
200 Record pivot;
201 int i, // used to scan through the list
202 last_small; // position of th last key less than pivot
203 swap(low, (low + high) / 2);
204 pivot = entry[low]; // First entry is now pivot
205 last_small = low;
206
207 for (i = low + 1; i < high; i++)
208 /* At the beginning of each iteration of this loop, we have the following condition:
209 If low < j <= last_small then entry[j].key < pivot.
210 If last_small < j < i then entry[j].key >= pivot */
211 {
212 if (entry[i] < pivot)
213 {
214 last_small = last_small + 1;
215 swap(last_small, i); // Move large entry to right and small to left
216 }
217 }
218
219 swap(low, last_small); // Put the pivot into its proper position
220 return last_small;
221 }
222
223 template <class Record>
224 void Sortable_list<Record>::heap_sort()
225 {
226 Record current;
227 int last_unsorted;
228 build_heap();
229 for (last_unsorted = count + 1; last_unsorted > 0; last_unsorted--)
230 {
231 current = entry[last_unsorted];
232 entry[last_unsorted] = entry[0];
233 insert_heap(current, 0, last_unsorted - 1);
234 }
235 }
236
237 template <class Record>
238 void Sortable_list<Record>::build_heap()
239 {
240 int low;
241 for (low = count / 2 - 1; low >= 0; low--)
242 {
243 Record current = entry[low];
244 insert_heap(current, low, count - 1);
245 }
246 }
247 template <class Record>
248 void Sortable_list<Record>::insert_heap(const Record &current, int low, int high)
249 {
250 int large;
251 large = 2 * low + 1;
252 while (large <= high)
253 {
254 if (large < high && entry[large] < entry[large+1])
255 large++;
256 if (current >= entry[large])
257 break;
258 else
259 {
260 entry[low] = entry[large];
261 low = large;
262 large = 2 * low + 1;
263 }
264 }
265 entry[low] = current;
266 }

main.cpp
注意对fstream的使用
 fstream f("a.txt");  =  fstream f;  f.open("a.txt");
操作完必须 f.close();
该文件中 两个do{} while(); 用的比较好,第二个指令[q]uit时,可自动跳到上一层

main.cpp

1 #include"Sortable_list.h"
2 #include<fstream>
3
4 void show(char *comment, double t, int comparisions)
5 {
6 cout

抱歉!评论已关闭.