#include <iostream> using namespace std; void print(int *a,int n){ if(a==NULL || n<=0) return; for(int i=0;i<n;i++){ cout<<a[i]<<" "; } cout<<endl; } //快排的思想 int Partition(int *a,int left,int right){ int l=left; int r=right; int temp; while(l<r){ while( l<r && a[l]%2!=0) l++; temp=a[l]; if(l<r){ while( l<r && a[r]%2==0) r--; if(l<r){ a[l]=a[r]; l++; a[r]=temp; r--; } } } } int main(){ int a[8]={1,2,3,4,5,6,7,8}; print(a,8); Partition(a,0,7); print(a,8); return 0; }
时间: 2024-10-24 13:12:05