快速排序
来源:互联网 发布:windows toolkit 2.5.3 编辑:程序博客网 时间:2024/06/06 01:02
基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
最差时间:O(n2);平均时间复杂度:O(n*log2n);空间复杂度为:O(log2n)~O(n);
快速排序的稳定度为不稳定;
程序实现如下:
void QSort(int arNums[], int nLowIndex, int nHighIndex)
{
if(nLowIndex < 0 || nHighIndex < 0)
return;
if(nLowIndex >= nHighIndex)
return;
int nFirst = nLowIndex;
int nLast = nHighIndex;
int nKey = arNums[nFirst];
while(nFirst < nLast)
{
while(nFirst < nLast && nKey <= arNums[nLast])
--nLast;
arNums[nFirst] = arNums[nLast];
while(nFirst < nLast && arNums[nFirst] <= nKey)
++nFirst;
arNums[nLast] = arNums[nFirst];
}
arNums[nFirst] = nKey;
QSort(arNums, nLowIndex, nFirst - 1);
QSort(arNums, nFirst + 1, nHighIndex);
}
阅读全文
0 0
- 快速排序
- 快速排序
- 快速排序
- 快速排序!
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 破解微信数据库 并查询数据上传服务器
- 进程
- CSS属性提升
- 实践
- <jsp:include page="/header.jsp"></jsp:include> 引发的bug
- 快速排序
- Python浅拷贝
- Python-WSGI协议
- 【LeetCode】628. Maximum Product of Three Numbers
- java项目写yml配置文件时候需要注意的几个地方
- 联想拯救者安装固态硬盘重做系统
- 炉石兄弟免费版/破解版 0.3.1406.392
- 静态双向链表的多数组实现
- 二叉树