几种排序
来源:互联网 发布:js调用android的方法 编辑:程序博客网 时间:2024/06/10 04:39
//冒泡排序#include <iostream>using namespace std;#include <assert.h>void BubbleSort(int* a, size_t size){bool sign = false;for (int i = size-1; i > 0; --i){for (int j = 0; j < i; ++j){if (a[j] > a[j + 1]){swap(a[j], a[j + 1]);sign = true;}}if (sign == false)break;}}void TestBubbleSort(){int a[10] = { 2, 8, 5, 0, 7, 1, 3, 9, 4, 6 };int b[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };BubbleSort(b, 10);}int PartSort(int *a, int left, int right){int begin = left;int end = right - 1;while (begin < end){while (begin < end&&a[begin] <= a[right])begin++;while (begin < end&&a[end] >= a[right])end--;swap(a[begin], a[end]);}return begin;}void QuickSort(int* a, int left, int right){if (left >= right)return;int div = PartSort(a, left, right);if (a[div] > a[right]){swap(a[div], a[right]);}QuickSort(a, left, div - 1);QuickSort(a, div + 1, right);}void TestQuickSort(){int a[10] = { 2, 8, 5, 0, 7, 1, 3, 9, 4, 6 };int b[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };int c[2] = { 3,4};QuickSort(c, 0, 1);}//堆排序void _AdjustDown(int* a, int parent, int size)//大堆{int child = parent * 2 + 1;while (child < size){if (child + 1 < size && a[child] < a[child + 1]){++child;}if (a[child]>a[parent]){swap(a[child], a[parent]);parent = child;child = parent * 2 + 1;}elsebreak;}}void HeapSort(int* a, size_t size){assert(a);for (int i = (size - 2) / 2; i >= 0; i--){_AdjustDown(a, i, size);}int end = size - 1;while (end > 0){swap(a[end], a[0]);_AdjustDown(a, 0, end);end--;}}void TestHeapSort(){int a[10] = { 2, 8, 5, 0, 7, 1, 3, 9, 4, 6 };int b[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };HeapSort(a, 10);}//选择排序void SelectSort(int* a, int size){assert(a);for (int end = size - 1; end > 0; --end){int MaxIndex = 0;for (int i = 1; i < end; ++i){if (a[MaxIndex] < a[i]){MaxIndex = i;}}swap(a[MaxIndex], a[end]);}}void TestSelectSort(){int a[10] = { 2, 8, 5, 0, 7, 1, 3, 9, 4, 6 };SelectSort(a, 10);}//选择排序的优化void SelectSort_OP(int* a, int size){assert(a);int left = 0;int right = size - 1;while (left < right){int MaxIndex = 0;int MinIndex = right;for (int i = left; i < right; i++){if (a[MaxIndex] < a[i])MaxIndex = i;if (a[MinIndex]>a[i])MinIndex = i;}swap(a[MaxIndex], a[right]);if (MinIndex == right)//注意当MinIndex==right时,在上一个语句把MaxIndex和right的值交换了,所以要把Maxdex赋值给MinIndex{MinIndex = MaxIndex;}swap(a[MinIndex], a[left]);left++;right--;}}void TestSelectSort_OP(){int a[10] = { 2, 8, 5, 0, 7, 1, 3, 9, 4, 6 };SelectSort_OP(a, 10);}//希尔排序void ShellSort(int* a, int size){assert(a);int gap = size;while (gap > 1){gap = gap / 3 + 1;for (int i = 0; i < size; ++i){int end = i;int tmp = a[end + gap];while (end >= 0 && a[end]>tmp){a[end + gap] = a[end];end -= gap;}a[end + gap] = tmp;}}}int main(){//TestBubbleSort();//TestQuickSort();//TestHeapSort();//TestSelectSort();TestSelectSort_OP();system("pause");return 0;}
0 0
- 几种简单排序
- 几种排序算法
- 几种排序算法!
- 几种常用排序
- 几种排序
- 几种排序算法
- 几种排序算法
- 几种排序实现
- 几种排序
- 几种排序_java
- 几种排序方案
- 几种排序
- 几种排序方法
- 几种排序算法
- 几种排序算法
- 几种常用排序
- 几种插入排序
- 几种排序算法
- Python中@property的粗略理解
- 阿里面试题总结
- 【天梯赛-练习题】L2-007 家庭房产
- Objective C类方法load和initialize的区别
- 使用jutil测试程序出现错误java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=classestest]
- 几种排序
- 开平方算法
- 第一次
- 8、覆盖equas时请遵守约定
- c++string自学
- <Sicily>Rails
- iOS开发中的本地数据存储(持久化)
- 端午杂谈
- KVC基本使用