package itheima06;
public class Quick {
public static boolean judge(Comparable s1,Comparable s2)
{
return s1.compareTo(s2)<0;
}
public static void exchange(Comparable[] a, int i, int j) {
Comparable temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void sort(Comparable[] a) {
int low = 0;
int high = a.length-1;
sort(a, low, high);
}
public static void sort(Comparable[] a,int low,int high)
{
if(low>=high)
{
return;
}
int partition=partition(a, low, high);
sort(a,low,partition-1);
sort(a, partition+1, high);
}
public static int partition(Comparable[] a,int low,int high)
{
Comparable index=a[low];
int right=high+1;
int left=low;
while(true)
{
while(judge(index, a[--right]))
{
if(right==low)//小
break;
}
while(judge(a[++left], index))
{
if(left==high)//大
break;
}
if(left>=right)
{
break;
}else {
exchange(a, right, left);
}
}
exchange(a, low, right);
return right;
}
}
数据结构-java-快速排序-工具类
最新推荐文章于 2022-10-30 22:36:08 发布