题目描述:
#include <iostream>
using namespace std;
int partition(int A[],int p,int r){//p与r 是分割的范围,即将数组A[p...r]分割成A[p...q-1]、A[q+1...r]两部分
int x = A[r],i = p-1;//分割时以A[r]为基准
for( int j = p; j < r; j++ ){
if(A[j] <= x){
i = i+1;
swap(A[i],A[j]);
}
}
swap(A[i+1],A[r]);
return i+1;//返回 下标q
}
int main(){
int n,i,x;
cout<<"Please input a number:";
cin>>n;
int A[n];
cout<<"Please input numbers one by one:\n";
for(i = 0; i < n; i++ ) cin>>A[i];
x = partition(A,0,n-1);
for( i = 0; i < n ; i++){
if( i == x) cout<<"["<<A[i]<<"]"<<" ";
else cout<<A[i]<<" ";
}
return 0;
}