快速排序算法C++实现

快速排序算法的描述:

        快速排序算法属于分治模式,数组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);//
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值