快速排序
来源:互联网 发布:linux ping不通 trace 编辑:程序博客网 时间:2024/05/16 11:27
#include<iostream>
using namespace std;
int Partition(int A[], int l, int h)
{
int low=l,high=h;
int pivotkey = A[l];
while (low < high)
{
while (A[high] >= pivotkey&&high>l)
--high;
A[low] = A[high];
while (A[low] <= pivotkey&&low<high)
++low;
A[high] = A[low];
}
A[low] = pivotkey;
return (low);
}
void QSort(int A[], int low, int high)
{
if (low < high)
{
int pivotloc = Partition(A, low, high);
QSort(A, low, pivotloc - 1);
QSort(A, pivotloc + 1, high);
}
}
int main()
{
int a[10] = { 9 ,3 ,4 ,6,12,15,6,7,9,4};
QSort(a, 0, 9);
for(int i=0;i<sizeof(a)/sizeof(a[0]);++i)
{
cout<<a[i]<<endl;
}
return 0;
}
加粗部分为快速排序要点,其中如
while (A[high] >=pivotkey&&high>l)--high;
作为判断循环时一定要对数组下标进行控制。比如数组A[4]={5,5,6,6}且pivotkey=5,若不对数组下标进行控制,
则为
while (A[high] >=pivotkey)--high;
则会发生high一直将减,直到找到A[high]>=pivotkey。然而此时的high必然小于0,A[high]等效于*(A+high)。
- 快速排序
- 快速排序
- 快速排序
- 快速排序!
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- 快速排序
- Xcode4.2中将Three20开源库导入到工程项目中
- android res
- .Net TCP/UDP 编程
- 用get函数读入一个字符
- c++头文件
- 快速排序
- UI控件篇——UIPageControl及其自定义
- oracle索引详解
- xp安装bugfree
- 语言之争可以休矣
- 当风轻轻吹起我会沉默
- camera的分辨率
- UIPopoverController的使用
- Shell脚本:用于OpenVox GSM(Asterisk Card)卡发送长短信