标签:iostream 时间 快速 ios index amp == style space
快速排序,平均时间复杂度O(nlogn),
#include<stdio.h> #include<iostream> using namespace std; int partition(int a[],int left,int right){ int x=a[left]; while(left<right){ while(left<right&&a[right]>=x) --right; a[left]=a[right]; while(left<right&&a[left]<=x) ++left; a[right]=a[left]; } a[left]=x; return left; } void quick_sort(int a[],int left,int right){ if(left==right) return ; int index=partition(a,left,right); if(index>left) quick_sort(a,left,index-1); if(index<right) quick_sort(a,index+1,right); } int main(){ int a[]={1,2,4,3,2}; quick_sort(a,0,4); for(int i=0;i<=4;i++) printf("%d ",a[i]); return 0; }
标签:iostream 时间 快速 ios index amp == style space
原文地址:http://www.cnblogs.com/L-King/p/7403591.html