#include<iostream>
using namespace std;
template<class T>
void Swap(T &a,T &b){
T temp=a;
a=b;
b=temp;
}
template<class T>
void QuickSort(T A[],int n)
{
QSort(A,0,n-1);
}
template<class T>
void QSort(T A[],int left,int right)
{
int i,j;
if(left<right)
{
i=left;
j=right+1;
do{
do i++;while(A[i]<A[left]);
do j--;while(A[j]>A[left]);
if(i<j)
Swap(A[i],A[j]);
}while(i<j);
Swap(A[left],A[j]);
QSort(A,left,j-1);
QSort(A,j+1,right);
}
}
int main (){
int n,i=0;
cout<<"input the num of the list: ";
cin>>n;
int *arr=new int[n];
cout<<"input the list: ";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
QuickSort(arr,n);
cout<<"the list after QuickSort: ";
for(i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}
c++实现快速排序(QuickSort)
最新推荐文章于 2025-02-25 23:26:26 发布