# 快速排序(二)

/**
* 参考:http://caterpillar.onlyfun.net/Gossip/AlgorithmGossip/QuickSort3.htm
* <p>Title: </p>
* <p>Description: </p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class QuickSort1 {
public static void swap(int[] number, int i, int j) { //交换number数组索引i,j处的值
int tmp;
tmp = number[i];
number[i] = number[j];
number[j] = tmp;
}
public static void sort(int[] number,int left,int right){
if(left < right){
int p  = partition(number,left,right);
sort(number,left,p-1);
sort(number,p+1,right);
}
}
public static int partition(int[] number, int left, int right) {
int i = left - 1;
int s = number[right];
int tmp;
for ( int j = left; j < right ; j++) {
if (number[j] <= s){
swap(number, ++i, j);
}
}
swap(number, ++i, right);
return i;
}

public static void print(int[] number) { //打印输入数组内容
for (int i = 0; i < number.length; i++) {
System.out.print(number[i] + ",");
}
System.out.println("");
}

public static void main(String[] args) {
int[] number = {
2, 8, 7, 1, 3, 5, 6, 4};
sort(number, 0, number.length - 1);

print(number);
}
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：快速排序(二) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)