快速排序
2015-07-02 17:42
330 查看
选择一个基准元素,通常选择第一个元素,过一轮排序将数据分为两部分,左边小于基准右边大于基准元素,然后分别对这两部分记录用递归的方法继续进行排序,直到整个序列有序。
1.第一趟排序
![](http://my.csdn.net/uploads/201207/20/1342782317_4426.jpg)
2.排序全过程
![](http://my.csdn.net/uploads/201207/20/1342782329_8314.jpg)
排序实现过程
快速排序的改进方法
在本改进算法中,只对长度大于k的子序列递归调用快速排序,让原序列基本有序,然后再对整个基本有序序列用插入排序算法排序。实践证明,改进后的算法时间复杂度有所降低,且当k取值为 8 左右时,改进算法的性能最佳。算法思想如下:
1.第一趟排序
![](http://my.csdn.net/uploads/201207/20/1342782317_4426.jpg)
2.排序全过程
![](http://my.csdn.net/uploads/201207/20/1342782329_8314.jpg)
排序实现过程
#include <stdio.h> #include <stdlib.h> #define SIZE 10 void swap(int arr[], int a, int b) { int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } //选择首元素作为基准元素,通过一轮排序将数据分为两部分,左边小于基准右边大于基准元素 int partition(int arr[], int low, int high) { int pivot = arr[low]; //基准元素 while(low < high) { while(low < high && arr[high] >= pivot) --high; swap(arr, low, high); while(low < high && arr[low] <= pivot) low++; swap(arr, low, high); } return low; } void QuickSort(int arr[], int low, int high) { if(low < high) { int pivot = partition(arr, low, high);//把首元素做基准,找到合适位置 QuickSort(arr, low, pivot-1);//用递归的方法对基准左边的序列排序 QuickSort(arr, pivot+1, high); } } int main(int argc, char *argv[]) { int i = 0; int arr[SIZE] = {3,2,8,9,5,6,88,0,99,44}; for(i = 0; i < SIZE; i++) { printf("%d ", arr[i]); } printf("\n"); QuickSort(arr, 0, SIZE-1); for(i = 0; i < SIZE; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; }
快速排序的改进方法
在本改进算法中,只对长度大于k的子序列递归调用快速排序,让原序列基本有序,然后再对整个基本有序序列用插入排序算法排序。实践证明,改进后的算法时间复杂度有所降低,且当k取值为 8 左右时,改进算法的性能最佳。算法思想如下:
#include <stdio.h> #include <stdlib.h> #define SIZE 10 void swap(int arr[], int a, int b) { int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } //选择首元素作为基准元素,通过一轮排序将数据分为两部分,左边小于基准右边大于基准元素 int partition(int arr[], int low, int high) { int pivot = arr[low]; //基准元素 while(low < high) { while(low < high && arr[high] >= pivot) --high; swap(arr, low, high); while(low < high && arr[low] <= pivot) low++; swap(arr, low, high); } return low; } void QsortImprove(int arr[], int low, int high, int k) { if(low - high > k) { int pivot = partition(arr, low, high);//把首元素做基准,找到合适位置 QsortImprove(arr, low, pivot-1, k);//用递归的方法对基准左边的序列排序 QsortImprove(arr, pivot+1, high, k); } } void QuickSort(int arr[], int low, int high, int k) { int i, j, m; QsortImprove(arr, low, high, k); for(i = 1; i < SIZE; i++) { int temp = arr[i]; m = i; for(j = i-1; j >= 0 && arr[j] > temp; j--) { arr[j+1] = arr[j]; m = j; } arr[m] = temp; } } int main(int argc, char *argv[]) { int i = 0; int arr[SIZE] = {3,2,8,9,5,6,88,0,99,44}; for(i = 0; i < SIZE; i++) { printf("%d ", arr[i]); } printf("\n"); QuickSort(arr, 0, SIZE-1, 9); for(i = 0; i < SIZE; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; }
相关文章推荐
- 快速排序
- 我是运营,我没有假期
- DB2数据库的安装
- C#实现把指定数据写入串口
- “传奇”图象数据存储方式
- 修复mysql数据库
- SQLServer 数据导入导出的几种方法小结
- MySQL数据备份之mysqldump的使用详解
- C#快速排序算法实例分析
- C#实现窗体间传递数据实例
- 给你的数据库文件减肥
- Oracle数据更改后出错的解决方法
- C#将Sql数据保存到Excel文件中的方法
- C++快速排序的分析与优化详解
- 把excel表格里的数据导入sql数据库的两种方法
- 用文本作数据处理
- 桌面中心(一)创建数据库
- 桌面中心(四)数据显示
- PHP+JS实现大规模数据提交的方法
- SQL Server Management Studio Express管理器 没有导入导出数据的向导的解决方法