package edu.xlead;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class QuickSort1 {
/**
*
* @param arr 需要排序的数组
* @param left The leftmost index at the beginning
* @param right The rightmost index at the beginning =arr.length-1
*/
public static void quickSort1(int []arr,int left,int right){
LinkedHashMap<Integer,Integer>lhp =new LinkedHashMap<>();
lhp.put(left,right);
while (!lhp.isEmpty()){
Iterator<Map.Entry<Integer,Integer>> ite=lhp.entrySet().iterator();
left=ite.next().getKey();
right=lhp.get(left);
lhp.remove(left,right);
if (left>=right){
continue;
}
int i=left;
int j=right;
int temp=arr[right];
while (i<j){
while (temp>=arr[i]&&i<j){
i++;
}
if(i<j){
arr[j--]=arr[i];
}
while (temp<=arr[j]&&i<j){
j--;
}
if(i<j){
arr[i++]=arr[j];
}
}
arr[i]=temp;
lhp.put(left,i-1);
lhp.put(i+1,right);
}
}
public static void main(String[] args) {
int []a={112,115,633,2578,268,225,245};
quickSort1(a,0,a.length-1);
System.out.print(Arrays.toString(a));
}
}
【Java】非递归实现快速排序(LinkedHashMap)
最新推荐文章于 2021-11-14 19:23:13 发布