【C++】快速排序

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/u011956367/article/details/100699087

不解释了,基础算法,递归分治思想

#include <iostream>
#include <algorithm>
using namespace std;
int num[]={6,8,4,1,2,5,23,15};//测试数组 
int len;//数组长度 
void show(){//打印数组 
	for(int i=0;i<len;i++){
		cout<<num[i]<<' ';
	}
	cout<<endl;
}
int partition(int a[],int low,int high){//快排划分
	int key=a[low];
	while(low<high){
		while(low<high&&a[high]>key) high--;
		swap(a[low],a[high]);
		while(low<high&&a[low]<key) low++;
		swap(a[low],a[high]);
	}
}
void qs(int a[],int low,int high){//快排
	if(low>=high) return;
	int index=partition(a,low,high);
	qs(a,low,index-1);
	qs(a,index+1,high);
}
int main(){
	len=sizeof(num)/sizeof(int);
	show();
	qs(num,0,len-1);
	show();
}

猜你喜欢

转载自blog.csdn.net/u011956367/article/details/100699087