快速排序算法
#include <stdio.h> int Pation(int ch[],int begin,int end); void Sort(int ch[],int begin,int end){ if(begin<end){ int mid=Pation(ch,begin,end); Sort(ch,begin,mid-1); Sort(ch,mid+1,end); } } int Pation(int ch[],int begin,int end){ int tmp=ch[end]; int i=begin-1; int j=begin; while(j<end){ if(ch[j]<=tmp){ i++; int change=ch[i]; ch[i]=ch[j]; ch[j]=change; } j++; } ch[end]=ch[i+1]; ch[i+1]=tmp; return i+1; } int main(){ int ch[5]={4,2,6,1,3}; Sort(ch,0,4); int count=0; while(count<5) printf("%i \n",ch[count++]); return 0; }
i指向数组中从左边开始的最后一个小于等于tmp的位置,开始时位置是begin-1,假定其值为无穷小。j就是当前比较的数组的位置。
浙公网安备 33010602011771号