冒泡 快速 堆排序 归并排序示例
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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | // SortTest.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> using namespace std; void swap( int & a, int & b) { int c = a; a = b; b = c; } void BubbleSort( int arr[], int length) { for ( int i = 0; i < length-1; i++) { for ( int j = 0; j < length - i-1; j++) { if (arr[j] > arr[j + 1]) { swap(arr[j], arr[j+ 1]); //此处可增加是否跳出标记 标记为真 } } //此处检测跳出标记 若标记为假 则说明此次循环数据已无变动 //排序结束 可跳出循环 } } void PrintArray( int arr[], int length) { for ( int i = 0; i < length; i++) { std::cout << arr[i] << " " ; } std::cout << std::endl; } void TestBubbleSort() { int testArr1[] = { 12,76,12,18,99,12,12,35,12,12 }; int testArr2[] = { 1,2,3,8,7,4,5,6,7,8,9,0 }; int testArr3[] = { 0,9,3,4,8,7,99,6,7,88,5,33,66,4,3,2,1 }; #define TESTARR(ARR) \ do { \ BubbleSort(ARR, sizeof (ARR) / sizeof (ARR[0])); \ PrintArray(ARR, sizeof (ARR) / sizeof (ARR[0])); \ } while (0) TESTARR(testArr1); TESTARR(testArr2); TESTARR(testArr3); std::cout << std::endl; } //============================================================= void QuickSort( int arr[], int left, int right) { int i = left; int j = right; if (left >= right) return ; int temp = arr[left]; while (i != j) { while (j != i && arr[j] >= temp) j--; while (j != i && arr[i] <= temp) i++; if (i < j) { swap(arr[i],arr[j]); } } arr[left] = arr[i]; arr[i] = temp; QuickSort(arr,left,i-1); QuickSort(arr, i+1,right); } void TestQuickSort() { int testArr1[] = { 12,76,12,18,99,12,12,35,12,12 }; int testArr2[] = { 1,2,3,8,7,4,5,6,7,8,9,0 }; int testArr3[] = { 0,9,3,4,8,7,99,6,7,88,5,33,66,4,3,2,1 }; #define TESTQUICKARR(ARR) \ do { \ QuickSort(ARR, 0, sizeof (ARR) / sizeof (ARR[0])-1); \ PrintArray(ARR, sizeof (ARR) / sizeof (ARR[0])); \ } while (0) TESTQUICKARR(testArr1); TESTQUICKARR(testArr2); TESTQUICKARR(testArr3); std::cout << std::endl; } //======================================= struct HeapArray { int heapArr[100]; int heapSize; HeapArray() { heapSize = 0; for ( int i = 0; i < 100; i++) { heapArr[i] = -1; } } }; HeapArray heapA; void HeadpAdjustTopDown( int index) { while (index*2+1 <= heapA.heapSize-1){ if (index * 2 + 2 <= heapA.heapSize - 1) { if (heapA.heapArr[index] > heapA.heapArr[index * 2 + 1] && heapA.heapArr[index * 2 + 1] <= heapA.heapArr[index * 2 + 2]) { swap(heapA.heapArr[index], heapA.heapArr[index * 2 + 1]); index = index * 2 + 1; continue ; } else if (heapA.heapArr[index] > heapA.heapArr[index * 2 + 2] && heapA.heapArr[index * 2 + 2] <= heapA.heapArr[index * 2 + 1]) { swap(heapA.heapArr[index], heapA.heapArr[index * 2 + 2]); index = index * 2 + 2; continue ; } } else { // 仅有左儿子情况 if (heapA.heapArr[index] > heapA.heapArr[index * 2 + 1]) { swap(heapA.heapArr[index], heapA.heapArr[index * 2 + 1]); index = index * 2 + 1; continue ; } } break ; } } void HeadpAdjustDownTop( int index) { int i = index; while (i != 0) { if (heapA.heapArr[i] < heapA.heapArr[(i - 1) / 2]) { swap(heapA.heapArr[i], heapA.heapArr[(i - 1) / 2]); i = (i-1) / 2; } else { break ; } } } void HeapInsert( int in ) { heapA.heapArr[heapA.heapSize] = in ; HeadpAdjustDownTop(heapA.heapSize); heapA.heapSize++; } void HeapSort( int arr[], int length) { heapA.heapSize = 0; for ( int i = 0; i < 100; i++) { heapA.heapArr[i] = -1; } for ( int i = 0; i < length; i++) { HeapInsert(arr[i]); } } void PrintHeap() { int j = heapA.heapSize; for ( int i = 0; i < j; i++) { std::cout << heapA.heapArr[0] << " " ; heapA.heapArr[0] = heapA.heapArr[heapA.heapSize - 1]; heapA.heapSize--; HeadpAdjustTopDown(0); } std::cout << std::endl; } void TestHeapSort() { int testArr1[] = { 12,76,12,18,99,12,12,35,12,12 }; int testArr2[] = { 1,2,3,8,7,4,5,6,7,8,9,0 }; int testArr3[] = { 0,9,3,4,8,7,99,6,7,88,5,33,66,4,3,2,1 }; #define TESTHEAPARR(ARR) \ do { \ HeapSort(ARR, sizeof (ARR) / sizeof (ARR[0])); \ PrintHeap(); \ } while (0) TESTHEAPARR(testArr1); TESTHEAPARR(testArr2); TESTHEAPARR(testArr3); std::cout << std::endl; } //================================================= void MergeArr( int arr[], int lbegin, int lend, int rbegin, int rend, int tmp[]) { int i = lbegin,j = lend,m = rbegin, n = rend,k = 0; while (i <= j && m <= n) { if (arr[i] <= arr[m]) { tmp[k++] = arr[i++]; } else { tmp[k++] = arr[m++]; } } while (i <= j) tmp[k++] = arr[i++]; while (m <= n) tmp[k++] = arr[m++]; for ( int i = 0; i < k; i++) { arr[lbegin + i] = tmp[i]; } } void MergeSort( int a[], int first, int last, int temp[]) { if (first < last) { int mid = (first + last) / 2; MergeSort(a, first, mid, temp); MergeSort(a, mid + 1, last, temp); MergeArr(a,first,mid, mid + 1,last,temp); } } bool MergeSort( int arr[], int n) { int *tmp = new int [n]; MergeSort(arr, 0, n - 1, tmp); delete[] tmp; return true ; } void TestMergeSort() { int testArr1[] = { 12,76,12,18,99,12,12,35,12,12 }; int testArr2[] = { 1,2,3,8,7,4,5,6,7,8,9,0 }; int testArr3[] = { 0,9,3,4,8,7,99,6,7,88,5,33,66,4,3,2,1 }; #define TESTMERGEARR(ARR) \ do { \ MergeSort(ARR, sizeof (ARR) / sizeof (ARR[0])); \ PrintArray(ARR, sizeof (ARR) / sizeof (ARR[0])); \ } while (0) TESTMERGEARR(testArr1); TESTMERGEARR(testArr2); TESTMERGEARR(testArr3); std::cout << std::endl; } //============================================= int main() { TestQuickSort(); TestBubbleSort(); TestHeapSort(); TestMergeSort(); return 0; } |
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
2015-02-08 linux 基本工具相关