分治思想:涉及递归,例如归并排序、堆排序、快速排序等
#include<stdio.h>
int Partion(int a[],int low,int high){
int i,j,t;
j=low-1;
for(i=low;i<=high;i++){
if(a[i]<=a[high]){
j++;
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
return j;
}
void QuickSort(int a[],int low,int high){
int pivot;
if(low<high){
pivot=Partion(a,low,high);
QuickSort(a,low,pivot-1);
QuickSort(a,pivot+1,high);
}
}
int main(){
int i,j,t;
int size=5;
int a[5]={
2,1,5,3,8
};
QuickSort(a,0,4);
for(i=0;i<size;i++){
printf("%d ",a[i]);
}
}
//写在一起
void QuickSort(int a[],int low,int high){
int i,j,t;
j=low-1;
for(i=low;i<=high;i++){
if(a[i]<=a[high]){
j++;
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
if(low<high){
QuickSort(a,low,j-1);
QuickSort(a,j+1,high);
}
}