快速排序

来源:互联网 发布:数据归一化后的值 编辑:程序博客网 时间:2024/06/07 14:40

在最优的情况下,快速排序算法的时间复杂度为O(nlongn)

在最坏的情况下,快速排序算法的时间复杂度为O(n*n)

平均的时间复杂度为O(nlongn)

在最优的情况下,快速排序算法的时间复杂度为O(longn)

在最坏的情况下,快速排序算法的时间复杂度为O(n)

平均的时间复杂度为O(longn)

快速排序是一种不稳定的排序算法

#include<iostream>using namespace std;int Partition(int *num,int low,int high){int pivot=num[low];while(low<high){while(low<high && num[high]>=pivot)high--;num[low]=num[high];while(low<high && num[low]<=pivot)low++;num[high]=num[low];}num[low]=pivot;return low;}//快速排序void QuitSort(int *num, int low, int high){if(low<high){int pivot=Partition(num,low,high);//划分 QuitSort(num, low, pivot-1);QuitSort(num, pivot+1, high);}} int main(){int num[10]={4,0,2,3,1,8,5,4,12,6};QuitSort(num,0,10);for(int i=0; i<10; i++)cout<<num[i]<<" ";cout<<endl;return 0;} 


0 0