快速排序是对冒泡排序的一种改进。其基本思想是基于分治法的:在带排序L[1..n]中任选一个元素pivot作为基准,通过一趟快速排序将待排序表分为独立的两个部分L[1..k-1]和L[k+1..n],使得L[1..k-1]中的所有元素小于等于pivot,L[k+1..n]中的所有元素大于等于pivot,则pivot放在了其最终的位置L[k]上,这一个过程称作一次快速排序。而后分别递归地对两个子表重复上述过程,直至每部分内只有一个元素或为空为止,即所有元素放在了其最终的位置上。
qsort()是编译器函数库自带的快速排序函数。
qsort 的函数原型:
void qsort(void*base,size_t num,size_t width,int(__cdecl*compare)(const void*,const void*));
各参数:1 待排序数组首地址 2 数组中待排序元素数量 3 各元素的占用空间大小 4 指向函数的指针
Partition函数有两种写法,如下
第一种写法:
#include<iostream>
using namespace std;
//一次划分函数
int Partition(int A[],int low,int high)
{
int pivot=A[low];
while(low<high)
{
while(low<high&&pivot<=A[high]) high--;
A[low]=A[high];
while(low<high&&A[low]<=pivot) low++;
A[high]=A[low];
}
A[low]=pivot;
return low;
}
//快速排序函数
void QuickSort(int A[],int low,int high)
{
if(low<high)
{
int pivotpos=Partition(A,low,high);
QuickSort(A,low,pivotpos-1);
QuickSort(A,pivotpos+1,high);
}
}
int main()
{
int a[]={2,3,0,9,1,8,6,4,7,5};
QuickSort(a,0,9);
for(int i=0;i<=9;i++)
cout<<a[i]<<" ";
return 0;
}
第一种写法的应用:将数组中的大小写字母分开,让小写字母排在大写字母的前面。代码如下。
#include<iostream>
#include<string>
using namespace std;
//判断一个字母是是否大写字母
bool isUpper(char c)
{
if(c>='A'&&c<='Z')
return true;
else
return false;
}
//判断一个字母是否是小写字母
bool isLower(char c)
{
if(c>='a'&&c<='z')
return true;
else
return false;
}
//划分函数
void partition(string& s,int low,int high)
{
while(low<high)
{
while(low<high&&isUpper(s[high])) --high;
while(low<high&&isLower(s[low])) ++low;
char temp=s[high];
s[high]=s[low];
s[low]=temp;
}
return;
}
int main()
{
string str;
cin>>str;
partition(str,0,str.length()-1);
cout<<str<<endl;
return 0;
}
Partition函数的第二种写法,代码如下。
#include<iostream>
using namespace std;
//一次划分函数
int Partition(int A[],int low,int high)
{
int x=A[high];
int i=low-1;
for(int j=low;j<=high-1;j++)
{
if(A[j]<=x)
{
i++;
swap(A[i],A[j]);
}
}
swap(A[i+1],A[high]);
return i+1;
}
//快速排序函数
void QuickSort(int A[],int low,int high)
{
if(low<high)
{
int pivotpos=Partition(A,low,high);
QuickSort(A,low,pivotpos-1);
QuickSort(A,pivotpos+1,high);
}
return;
}
int main()
{
int a[]={2,3,0,9,1,8,6,4,7,5};
QuickSort(a,0,9);
for(int i=0;i<=9;i++)
cout<<a[i]<<" ";
return 0;
}
第二种写法的应用举例:给定含有n个元素的整型数组a,其中包括0元素和非0元素,对数组进行排序,排序后所有0元素在前,所有非0元素在后,且非0元素排序后前后相对位置不变,要求不使用额外存储空间。
#include<iostream>
using namespace std;
//划分函数
void partition(int A[],int low,int high)
{
int i=high+1;//非零元素部分第一个非零元素的下标
int j=high;
while(j>=low)
{
if(A[j]!=0)
{
i--;
int temp=A[i];
A[i]=A[j];
A[j]=temp;
j--;
}
else
j--;
}
return;
}
int main()
{
int a[]={0,2,0,0,9,4,0,3,2};
partition(a,0,8);
for(int i=0;i<9;i++)
cout<<a[i]<<" ";
return 0;
}
荷兰国旗问题。若令0为红色,1为蓝色,2为白色,将含有0,1,2三种元素的数组排序,使所有的0排在前面,所有的1排在中间,所有的2排在后面。代码如下。
#include<iostream>
using namespace std;
//划分函数
void partition(int A[],int low,int high)
{
int begin=low;
int current=low;
int end=high;
while(current<=end)
{
if(A[current]==0)
{
int temp=A[current];
A[current]=A[begin];
A[begin]=temp;
begin++;
current++;
}
else if(A[current]==1)
current++;
else if(A[current]==2)
{
int temp=A[current];
A[current]=A[end];
A[end]=temp;
end--;
}
}
}
int main()
{
int a[]={0,2,0,1,2,0,1,0,1,1,2};
partition(a,0,10);
for(int i=0;i<10;i++)
cout<<a[i]<<" ";
return 0;
}
最小的k个数问题:输入n个整数,输出其中最小的k个。例如输入1,2,3,4,5,6,7,8这八个数,则最小的4个数字为1,2,3,4.代码如下。
/*
输入n个整数,输出其中最小的k个数
*/
#include<iostream>
using namespace std;
//一次划分函数
int Partition(int A[],int low,int high)
{
int pivot=A[low];
while(low<high)
{
while(low<high&&pivot<=A[high]) high--;
A[low]=A[high];
while(low<high&&A[low]<=pivot) low++;
A[high]=A[low];
}
A[low]=pivot;
return low;//返回index
}
//输出其中最小的k个数
void getLeastKNum(int *input,int n,int *output,int k)
{
if(input==NULL||output==NULL||k>n||k<=0||n<=0)
return ;
int begin=0;
int end=n-1;
int index=Partition(input,begin,end);
while(index!=k-1)
{
if(index>k-1)
{
end=index-1;
index=Partition(input,begin,end);
}
else
{
begin=index+1;
index=Partition(input,begin,end);
}
}
for(int i=0;i<k;i++)
output[i]=input[i];
}
int main()
{
int a[]={8,3,2,19,3,2,0,1,9,34,8,5,6};
int b[13]={0};
getLeastKNum(a,13,b,6);
for(int i=0;i<6;i++)
cout<<b[i]<<" ";
return 0;
}