各种内部排序方法及效率分析。
一、 实验内容
1、对起泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、堆排序这六种常用排序算法进行比较。
2、待排序表的表长不超过100;其中数据用伪随机数产生程序产生。
3、至少要用6组不同的输入数据做比较。
4、要对实验结果做简单分析。
一、 源程序
1 #include<iostream> 2 #include<string> 3 #include<cstdlib> 4 using namespace std; 5 #define OK 1 6 #define OVERFLOW -1 7 #define ERROR 0 8 #define MAXSIZE 100 9 int typedef Status; 10 #define MAXSIZE 20 11 typedef int KeyType; 12 typedef struct{ 13 KeyType key; 14 int otherinfo; 15 }RedType; 16 typedef struct{ 17 RedType r[MAXSIZE + 1]; 18 int length; 19 }SqList; 20 void InsertSort(SqList &L){//直接插入排序 21 int i, j; 22 for ( i = 2; i <= L.length; ++i) 23 if (L.r[i].key < L.r[i - 1].key){ 24 L.r[0] = L.r[i]; 25 L.r[i] = L.r[i - 1]; 26 for (j = i - 2; L.r[0].key < L.r[j].key; --j) 27 L.r[j + 1] = L.r[j]; 28 L.r[j + 1] = L.r[0]; 29 30 } 31 32 } 33 void ShellInsert(SqList &L, int dk){//希尔排序 34 int i, j; 35 for (i = dk + 1; i <= L.length;++i) 36 if (L.r[i].key < L.r[i - dk].key){ 37 L.r[0] = L.r[i]; 38 for (j = i - dk; j>0 && L.r[0].key < L.r[j].key; j -= dk) 39 L.r[j + dk] = L.r[j]; 40 L.r[j + dk] = L.r[0]; 41 } 42 } 43 void ShellSort(SqList &L, int dt[], int t){ 44 int k; 45 for (k = 0; k < t; ++k) 46 ShellInsert(L, dt[k]); 47 } 48 void BubbleSort(SqList &L){//冒泡排序 49 int m = L.length - 1; int flag = 1; 50 int j; RedType t; 51 while ((m>0) && (flag == 1)){ 52 flag = 0; 53 for (j = 1; j <= m; j++) 54 if (L.r[j].key > L.r[j + 1].key){ 55 flag = 1; 56 t = L.r[j]; L.r[j] = L.r[j + 1]; 57 L.r[j + 1] = t; 58 } 59 --m; 60 } 61 } 62 void SelectSort(SqList &L){//简单选择排序 63 int i; 64 for (i = 1; i < L.length; ++i){ 65 int k; 66 k = i; 67 for (int j = i + 1; j <= L.length;++j) 68 if (L.r[j].key < L.r[k].key)k = j; 69 if (k != i) 70 { 71 RedType t; 72 t = L.r[i]; L.r[i] = L.r[k]; L.r[k] = t; 73 } 74 } 75 } 76 int Partition(SqList &L, int low, int high){ 77 L.r[0] = L.r[low]; 78 int pivotkey; 79 pivotkey = L.r[low].key; 80 while (low < high){ 81 while (low < high&&L.r[high].key >= pivotkey)--high; 82 L.r[low] = L.r[high]; 83 while (low < high&&L.r[low].key <= pivotkey)++low; 84 L.r[high] = L.r[low]; 85 } 86 L.r[low] = L.r[0]; 87 return low; 88 } 89 void QSort(SqList &L, int low, int high){ 90 if (low < high){ 91 int pivotloc; 92 pivotloc = Partition(L, low, high); 93 QSort(L, low, pivotloc - 1); 94 QSort(L, pivotloc + 1, high); 95 } 96 } 97 void QuickSort(SqList &L){//快速排序 98 QSort(L, 1, L.length); 99 } 100 void HeapAdjust(SqList &L, int s, int m){ 101 RedType rc; 102 rc = L.r[s]; 103 for (int j = 2 * s; j <= m; j *= 2){ 104 if (j < m&&L.r[j].key < L.r[j + 1].key)++j; 105 if (rc.key >= L.r[j].key)break; 106 L.r[s] = L.r[j]; s = j; 107 108 } 109 L.r[s] = rc; 110 } 111 void CreatHeap(SqList &L){ 112 int n = L.length; 113 for (int i = n / 2; i>0; --i) 114 HeapAdjust(L, i, n); 115 } 116 void HeapSort(SqList &L){//堆排序 117 CreatHeap(L); 118 for (int i = L.length; i > 1; --i){ 119 RedType x = L.r[1]; 120 L.r[1] = L.r[i]; 121 L.r[i] = x; 122 HeapAdjust(L, 1, i - 1); 123 } 124 } 125 int main(){ 126 SqList L; 127 cout << "请输入 长度:" << endl; 128 cin >> L.length; 129 for (int i = 1; i <= L.length; i++){ 130 cout << "请输入 第" << (i ) << "个值:" << endl; 131 cin >> L.r[i].key; 132 } 133 /*cout << "直接插入排序" << endl; 134 InsertSort(L);*/ 135 136 137 /*cout << "希尔排序" << endl; 138 cout << "请输入有几个增量选取" << endl; 139 int t; 140 int dt[10]; 141 cin >> t; 142 cout << "请输入每一趟的值" << endl; 143 for (int i = 0; i < t; i++){ 144 cin >> dt[i]; 145 } 146 ShellSort(L, dt, t);*/ 147 148 /*cout << "冒泡排序" << endl; 149 BubbleSort(L);*/ 150 151 152 153 /*cout << "选择排序" << endl; 154 SelectSort(L); 155 */ 156 157 /*cout << "快速排序" << endl; 158 QuickSort(L);*/ 159 160 161 cout << "堆排序" << endl; 162 HeapSort(L); 163 164 for (int i = 1; i <= L.length; i++){ 165 cout << "第" << (i ) << "个值:" << L.r[i].key << endl; 166 } 167 168 }