快速排序算法的描述:
快速排序算法属于分治模式,数组A[begin...end]被划分为A[begin...p-1]和A[p+1....end],使得A[begin...p-1]中的每一个元素都小于A[p]
#include<iostream>
using namespace std;
int partition(int *,int ,int );
void QuickSorting(int *,int ,int );
void main()
{
int b=0;
int a[10]={3,4,1,6,4,9,12,65,51,33};
QuickSorting(a,0,9);
for(int i=0;i<10;++i)cout<<a[i]<<",";
cin>>b;
}
int partition(int* pArray,int begin,int end)
{
int i=begin-1;
for(int j=begin;j<end;++j)
{
//
if(*(pArray+j)<=*(pArray+end))
{
++i;
//交换数值
int temp;
temp=*(pArray+j);
*(pArray+j)=*(pArray+i);
*(pArray+i)=temp;
}
}
++i;
int temp=*(pArray+i);
*(pArray+i)=*(pArray+end);
*(pArray+end)=temp;
return i;
}
//快速排序算法
void QuickSorting(int *pArray,int begin,int end)
{
if(begin<=end)
{
int Partition = partition(pArray,begin,end);//找出划分
QuickSorting(pArray,begin,Partition-1);//递归调用
QuickSorting(pArray,Partition+1,end);//
}
}