冒泡 快速 堆排序 归并排序学习示例
2017-02-14 15:06
218 查看
// 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 ; 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; }
相关文章推荐
- 七大排序算法(冒泡,选择,插入,二分法排序,希尔,快速,合并,堆排序)的java实现
- 冒泡、归并、快速、桶、堆排序
- java---插入排序,冒泡,归并,快速,希尔,堆排序
- 第四篇、C_快速、冒泡、选择、插入排序、二分查找排序、归并、堆排序
- 六、内部排序综合(九种)—插入类排序(直接插入、折半插入、希尔排序);交换类排序(冒泡、快速);选择类排序(简单选择、堆排序);二路归并排序;基数排序
- 各排序算法总结(简单选择,冒泡,快速,希尔排序,堆排序)
- c# 自定义排序类(冒泡、选择、插入、希尔、快速、归并、堆排序等)
- 排序总结:插入(简单和改进)、希尔、选择、冒泡、快速、堆排序、归并排序
- 基础排序算法,java实现(快速,冒泡,选择,堆排序,插入)
- 22.冒泡,选择,插入,希尔,快速,堆排序
- 插入 希尔 排序 堆排序 冒泡 快速 归并
- c语言归并、选择、直接插入、冒泡、快速、堆排序与顺序、二分查找
- c语言排序算法总结 选择 插入 冒泡 希尔 快速 堆排序 稳定性 时间复杂度 空间复杂度
- 冒泡,快速,和堆排序
- 九大排序算法及其实现- 插入.冒泡.选择.归并.快速.堆排序.计数.基数.桶排序.堆排序
- 六种排序 冒泡 选择 插入 归并 快速 堆排序
- C语言实现 排序源程序(包括直接插入、希尔、冒泡、快速、简单选择、堆排序)
- 关于几种排序算法(简易冒泡,正宗冒泡,选择,插入,快速,希尔,堆排序)
- C语言实现 排序源程序(包括直接插入、希尔、冒泡、快速、简单选择、堆排序)
- c排序算法:选择、冒泡、插入、快速、归并、堆排序