快速排序
来源:互联网 发布:想兼职淘宝客服 编辑:程序博客网 时间:2024/06/15 16:07
/*快速排序的核心思想是分治策略,即先分解再递归求解,最后再合并。
具体来说就是在待排序记录序列中选取一个记录(通常先选取第一个记录)为驱轴,其关键字设为K1,然后将其余关键字小于K1的记录移到前面,
而将关键字大于K1的记录移到后面,这样K1就将记录分成了两部分,这有点类似于二分查找。在一次划分之后,对分割后的子表继续按上述原则
进行分割,直到所有的子表长不超过1为止,此时待排序记录序列就变成了一个有序表。*/
//快速排序性能:
//快速排序的性能取决于PARTITION操作,它是否是平衡操作,即能否将数组分为两个大小差不多的数组。如果他分配不均的话,就变成了插入排序。
int Partition(int *pData,int left,int right){ int pivotKey = pData[left]; int low = left; int high = right; while(low < high){ while(low < high&&pData[high] >= pivotKey) high--; //high从右向左找小于pivotKey的元素 if(low < high){ pData[low] = pData[high]; low++; } while((low < high)&&pData[low] < pivotKey) low++; if(low < high){ pData[high] = pData[low]; high--; } } pData[low] = pivotKey; return low;}void QuickSort(int *pData,int left,int right){ if(left < right){ int pivotLoc = Partition(pData,left,right); QuickSort(pData,left,pivotLoc - 1); QuickSort(pData,pivotLoc+1,right); } return;}
阅读全文
0 0
- 快速排序
- 快速排序
- 快速排序
- 快速排序!
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- transwarp Slipstream 简介之事件驱动流处理
- idea中实现struts2框架下国际化
- CY6936模型遥控2.4GHz系统的设计与实现
- Footable的简单用法
- web网站获取客户端服务端信息
- 快速排序
- Xml解析并计算物体坐标数值
- Pjsip笔记之pjsip使用示例
- 从unity获取资源的小套路
- Python TensorFlow 安装
- SparkStreaming转SparkSql
- ceph bluestore相关配置
- 312. Burst Balloons
- adb install 指定设备安装