您的位置:首页 > 其它

基本排序算法--快速排序

2015-08-25 21:53 323 查看
#include<iostream>
using namespace std;

void QuickSort(int a[], int low,int high)
{
if (low < high)
{
int i = low - 1;
int key = a[high];
for (int j = low; j < high; j++)
{
if (a[j] < key)
{
i++;
int temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
int temp = a[high];
a[high] = a[i + 1];
a[i + 1] = temp;
int q = i + 1;
QuickSort(a, low, q - 1);
QuickSort(a, q + 1, high);

}
}

int main()
{
int array[] = { 34, 65, 12, 43, 67, 5, 78, 10, 3, 70 };
int len = sizeof(array) / sizeof(int);
cout << "the original array are:" << endl;
for (int k = 0; k < len; k++)
cout << array[k] << " ";
cout << endl;
QuickSort(array, 0,len-1);
cout << "The sorted array are:" << endl;
for (int k = 0; k<len; k++)
cout << array[k] << ",";
cout << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: