java 实现快速排序

来源:互联网 发布:巫师3狼派装备数据 编辑:程序博客网 时间:2024/06/16 07:26
package com;
/**
 * 实现快速排序
 * @author 小小王
 *
 */
public class TestQuickSort {
public static void quickSort(int[] a,int low,int high){
int i = low;//开始下标
int j = high;
if(low >= high){
return;
}
int index = a[i];
while(i < j){
while(i < j && a[j] >= index)
j --;
if(i < j)
a[i++] = a[j];
while(i < j && a[i] < index)
i++;
if(i < j)
a[j--] = a[i];
}
a[i] = index;
quickSort(a,low,i-1);
quickSort(a, i+1, high);
}
public static void sort(int[] a){
quickSort(a,0,a.length-1);
}
public static void main(String[] args) {
int a[] = {4,6,2,4,8,5,2,9};
sort(a);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}