#include <iostream>
using namespace std;
/*****************快速排序函数模板*******************/
template<class T>
void quickSort(T str[],int first,int last){
int position=0;
if (first<last)
{
position=partition(str,first,last);
quickSort(str,first,position-1);
quickSort(str,position+1,last);
}
}
/*****************序列划分函数***********************/
template<class T>
int partition(T str[],int first,int last){ //first 为数组第0个元素,last为数组最后一个元素
T temp=str[last];
int j=first-1;
for (int i=first;i<last;i++)
{
if (str[i]<=temp) //按照升序将数据进行排列
{
j+=1;
myswap(str[j],str[i]);
}
}
myswap(str[last],str[j+1]);
return j+1;
}
/*******************交换函数*************************/
template<class T>
void myswap(T &i,T &j){
T flag=i;
i=j;
j=flag;
}
void main()
{
int str[]={1,3,2,4,5,7,6};
quickSort(str,0,6);
for (int i=0;i<7;i++)
{
cout<<str[i]<<" ";
}
cout<<endl;
}
排序算法之快速排序
最新推荐文章于 2024-07-18 22:30:07 发布