一种简单的快排方式

通过while循环找到左右指针,右小于index左大于index,然后逐步递归
下面展示一些 内联代码片
public class test{
public static void sort(int[] array,int low,int high){
int i,j;//左右指针
int index;
//判定条件当低指针高于高位退出
if(low>=high){
return;
}
i=low;
j=high;
index=array[i];
while(i<j){
//从右遍历第一个小于index的值
while(i<j&&array[j]>index){
j–;
}
if(i<j){
//覆盖
array[i++]=array[j];
}
//从左开始遍历
while(i<j&&array[i]<index){
i++;
}
if(i<j){
array[j–]=array[i];
}
array[i]=index;
sort(array,low,i-1);
sort(array,i+1,high);
}

public static void quickSort(int []array ){
sort(array,0,array.length-1);
}
public static void main(String[] args){
int i=0;
int []a={3,4,6,6,7,3,1};
quickSort(a);
for(int i=0;i<a.length;i++){
System.out.print(a[i]);
}

}

}

// A code block
var foo = 'bar';
// An highlighted block
var foo = 'bar';
发布了7 篇原创文章 · 获赞 0 · 访问量 19

猜你喜欢

转载自blog.csdn.net/qq_40059773/article/details/105524686