Files
python/C++课程/待整理知识/啊哈算法/QuickSort.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

45 lines
706 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
#include <cstdio>
using namespace std;
int a[101],n;
void quicksort(int left,int right) {
int i,j,t,tmp;
if(left>right)
return ; //当i变为left值,进入第一个递归,right变为0,此时return 结束此递归函数。
tmp=a[left];
i=left;
j=right;
while(i!=j) {
while(a[j]>=tmp && i<j)
j--;
while(a[i]<=tmp && i<j)
i++;
if(i<j) {
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
a[left]=a[i];
a[i]=tmp;
quicksort(left,i-1);
quicksort(i+1,right);
return ;//此处return结束的是这个quicksort函数
}
int main() {
int i,j;
cin>>n;
for(i=1; i<=n; i++) {
cin>>a[i];
}
quicksort(1,n);
for(i=1; i<=n; i++) {
cout<<a[i];
}
return 0;
}