快速排序

来源:互联网 发布:怎么设计软件 编辑:程序博客网 时间:2024/05/15 03:52
快速排序、堆排序和归并排序理论上时间复杂度为O(nlogn ),是内存占用较少的情况下,速度最快的排序算法。
当然桶排序的时间复杂度为O(n),但是需要牺牲巨大内存。
#include<iostream>using namespace std;void Quick_sort(int a[],int left,int right){    int key=a[left];    int low=left;    int heigh=right;    if(left<right)    {        while(low<heigh)        {            while(low<heigh&&a[heigh]>key)                heigh--;            a[low]=a[heigh];            while(low<heigh&&a[low]<key)                low++;            a[heigh]=a[low];        }        a[low]=key;        Quick_sort(a,left,low-1);        Quick_sort(a,low+1,right);    }}int main(){    int a[5]={5,2,1,3,4};    Quick_sort(a,0,4);    for(int i=0;i<5;i++)        cout<<a[i]<<endl;    return 0;}

0 0