-
选择第一个数为中间数,小于中间数的数放在左边,大于中间数的数放在右边。
-
递归的将中间数左边和右边的数都按照第一步进行,直到不能递归。
-
public class QuickSort { public static void main(String[] args) { int[] str={21,5,6,3,9,51,1,2}; QuickSort(str,0,str.length-1); System.out.println(Arrays.toString(str)); } public static void QuickSort(int[] str,int left,int rghit){ int l=left; //左下标 int r=rghit;//右下标 int p =str[(l+r)/2];//中间的数 while (l<r){ while (str[l]<p){ l++; } while (str[r]>p){ r--; } if (l==r){ break; } int tem = str[l]; str[l]=str[r]; str[r]=tem; if (str[l]==p){ l--; } if (str[r]==p){ l++; } } if (l==r){ l++; r--; } if (left<l){ QuickSort(str, l, rghit); } if (rghit>r){ QuickSort(str, left, r); } } }
十大经典算法之快速排序
猜你喜欢
转载自blog.csdn.net/qq_42305209/article/details/102502585
今日推荐
周排行