快速排序

package sorttest; public class QuickSort {  public static void sort(int[] number) {  sort(number, 0, number.length - 1); }  private static void sort(int[] number, int left, int right) {  if (left < right) {   int s = number[left];             //the first of left    int i = left;                    //left position give to i   int j = right + 1;                  while (true) {     // 向右找    while (i + 1 < number.length && number[++i] < s)     ; // 向左找    while (j - 1 > -1 && number[--j] > s)     ;    if (i >= j)     break;    swap(number, i, j);   }       number[left] = number[j];   number[j] = s;   for(int m=0;m<number.length;m++){    System.out.print(number[m]+"|");       }   System.out.print("/n");   sort(number, j + 1, right); // 對右邊進行遞迴   sort(number, left, j - 1); // 對左邊進行遞迴  } }  private static void swap(int[] number, int i, int j) {  int t;  t = number[i];  number[i] = number[j];  number[j] = t; }  public static void main(String[] args){  int[] num={16,8,15,23,46,24,22};  sort(num);//  for(int k=0;k<num.length;k++){//   System.out.print(num[k]+"|");//  } }}
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页