快速排序(java实现)
作者:互联网
import java.util.Arrays;
import java.util.Scanner;
public class quickSort {
public static void sort(int[] a,int low,int hight){
int i,j,index;
if (low>hight){
return;
}
i = low;
j = hight;
index = a[i];
while (i<j){
while (i<j&&a[j]>=index){//从右边找比基准值小的,如果没找到则j--;
j--;
}
if (i<j){//走到这一步代表从右找到了比基准值小的
a[i++] = a[j];//将a[j]赋给了a[i],之后i++
}
while (i<j&&a[i]<=index){//从左边找比基准值大的,如果没找到则i++;
i++;
}
if (i<j){//走到这一步代表从左边找到了比基准大的
a[j--] = a[i];//这一步是把a[i]赋值给a[j];同时j--
}
}
a[i] = index;//此时i=j
sort(a,low,i-1);//对低子表进行排序
sort(a,i+1,hight);//对高子表进行递归排序
}
public static void quickSort(int[] a){
sort(a,0,a.length-1);
}
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] a = new int[n];
for (int i=0; i<n; i++){
a[i] = scanner.nextInt();
}
quickSort(a);
System.out.println(Arrays.toString(a));
}
}
标签:index,java,int,hight,low,import,排序,快速 来源: https://blog.csdn.net/double_sweet1/article/details/89297785