QuickSort

来源:互联网 发布:淘宝卖家软件有哪些 编辑:程序博客网 时间:2024/06/06 12:59

Step in detail :

a. Shuffle the array.

b. Partition so that, for some j

---entry a[j] is in place

---no larger entry to the left of j

---no smaller entry to the right of j

c. Sort each piece recursively


Procesure:

<span style="font-size:10px;">#include<stdio.h>#define MAX 5int less(char x,char y){if(x<=y)return 1;elsereturn 0;}void exchange(char a[],int i,int j){char temp;temp=a[i];a[i]=a[j];a[j]=temp;}int partition(char a[],int lo,int hi){int i=lo,j=hi+1;while(1){while(less(a[++i],a[lo])){if(i==hi) break;}while(less(a[lo],a[--j])){if(j==lo)break;}if(i>=j)break;exchange(a,i,j);}exchange(a,lo,j);return j;}void sort(char a[],int lo,int hi){int j;if(lo>=hi)return ;j=partition(a,lo,hi);sort(a,0,j-1);sort(a,j+1,hi);}int main(){int i;char a[MAX];for(i=0;i<MAX;i++)scanf("%c",&a[i]);sort(a,0,MAX-1);for(i=0;i<MAX;i++)printf("%c ",a[i]);return 0;}</span>
Input: kajb

Output: a b j k

0 0
原创粉丝点击