C++实现快速排序算法

来源:互联网 发布:田岛7.0绣花软件 编辑:程序博客网 时间:2024/06/05 10:56
#include<iostream>using namespace std;void swap(int& num1,int& num2){int temp = num1;num1 = num2;num2 = temp;}int quickSort(int a[],int beg ,int end){int temp = a[beg];int firstIndex = beg;int lastIndex = end;while (firstIndex<lastIndex){while (firstIndex<lastIndex&&a[lastIndex]>=temp){lastIndex--;}swap(a[firstIndex],a[lastIndex]);while (firstIndex<lastIndex&&a[firstIndex] <= temp){firstIndex++;}swap(a[firstIndex], a[lastIndex]);}a[firstIndex] = temp;return firstIndex;}void quick(int a[], int beg, int end){if (beg > end){return;}int index = quickSort(a, beg, end);quick(a, index + 1, end);quick(a, beg, index - 1);}int main(){int a[12] = {10,8,6,7,9,2,1,90,67,67,9,90};quick(a,0,11);for (int i = 0; i < 12;i++){cout << a[i] << " ";}cout << endl;getchar();return 0;}


原创粉丝点击