冰楓論壇
標題:
[C++] 快速排序法
[打印本頁]
作者:
whitefox
時間:
2023-5-31 23:43
標題:
[C++] 快速排序法
練習排序法中的快速排序法
#include <iostream>
using namespace std;
int a[101], n;
void quicksort(int left, int right){
int i, j , t, temp, x;
if(left>right)
return;
temp=a[left];
i=left;
j=right;
while(i!=j){
while (a[j]>=temp && i<j)
j--;
while (a[i]<=temp && i<j)
i++;
if(i<j){
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
a[left]=a[i];
a[i]=temp;
for (x=1;x<=n;x++)
cout<<a[x]<<" ";
cout<<"("<<temp<<")"<<endl;
quicksort(left, i-1);
quicksort(i+1, right);
}
int main(){
int i, j, t;
cin>>n;
for (i=1;i<=n;i++)
cin>>a[i];
quicksort(1, n);
for (i=1;i<=n;i++)
cout<<a[i]<<" ";
cout<<endl;
system("PAUSE");
return 0;
}
複製代碼
歡迎光臨 冰楓論壇 (https://bingfong.com/)
Powered by 冰楓