快排
快速排序
#include<iostream> using namespace std; int Partition(int r[], int first , int end) { int i=first,j=end; while(i<j) { while(i<j&&r[i]<=r[j]) j--; if(i<j) { int temp=r[i]; r[i]=r[j]; r[j]=temp; i++; } while(i<j&&r[i]<=r[j]) i++; if(i<j) { int temp=r[i]; r[i]=r[j]; r[j]=temp; j--; } } return i; } void QuickSort(int r[], int first, int end) { if(first<end) { int k=Partition(r,first,end); QuickSort(r,first,k-1); QuickSort(r,k+1,end); } } int main() { int a[100]; int n; cin>>n; for(int i=0; i<n; i++) cin>>a[i]; QuickSort(a,0,n-1); for(int i=0; i<n; i++) cout<<a[i]<<" "; return 0; }

浙公网安备 33010602011771号