Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 非递归快速排序_Java_Quicksort - Fatal编程技术网

Java 非递归快速排序

Java 非递归快速排序,java,quicksort,Java,Quicksort,我很想知道我的非递归快速排序算法的实现是否存在一些缺点或隐患。为了优化它,应该修改什么?以我的方式比较两个对象时会出现什么问题 public class QuickSort <T extends Number> { private Integer first, last, boundLo, boundHi, pivot; Integer temp[] = {0, 0}; public void sort(NewArrayList<T> vect) { Dequ

我很想知道我的非递归快速排序算法的实现是否存在一些缺点或隐患。为了优化它,应该修改什么?以我的方式比较两个对象时会出现什么问题

public class QuickSort <T extends Number> {

private Integer first, last, boundLo, boundHi, pivot;
Integer temp[] = {0, 0};

public void sort(NewArrayList<T> vect) {
    Deque<Integer[]> stack = new ArrayDeque<Integer[]>();

    first = 0;
    last = vect.size() - 1;
    stack.push(new Integer[] {first, last});

    while(!stack.isEmpty()) {
        sortStep(vect, stack);  
    }
}

private void sortStep(NewArrayList<T> vect, Deque<Integer[]> stack) {
    // initialize indices
    temp = stack.pop();
    first = temp[0];
    last = temp[1];

    boundLo = first;
    boundHi = last;
    pivot = last;

    while(first < last) {
        if(vect.get(first).doubleValue() >= vect.get(pivot).doubleValue()) {
            last--;
            if(first != last) 
                vect.swap(first, last);         
            vect.swap(last, pivot);
            pivot--;
        }
        else first++;
    }

    if(boundLo < (pivot - 1)) 
        stack.add(new Integer[] {boundLo, pivot - 1});

    if(boundHi > (pivot + 1)) 
        stack.add(new Integer[] {pivot + 1, boundHi});

}

}
公共类快速排序{
私有整数first、last、boundLo、boundHi、pivot;
整数温度[]={0,0};
公共void排序(NewArrayList vect){
Deque stack=new ArrayDeque();
第一个=0;
last=vect.size()-1;
push(新整数[]{first,last});
而(!stack.isEmpty()){
排序步骤(向量、堆栈);
}
}
私有void排序步骤(NewArrayList向量,Deque堆栈){
//初始化索引
temp=stack.pop();
第一个=温度[0];
last=温度[1];
boundLo=第一;
boundHi=最后一个;
枢轴=最后一个;
while(第一次<最后一次){
if(vect.get(first).doubleValue()>=vect.get(pivot).doubleValue()){
最后--;
如果(第一个!=最后一个)
向量交换(第一,最后);
向量交换(最后一个,枢轴);
支点--;
}
else-first++;
}
如果(边界<(枢轴-1))
add(新整数[]{boundLo,pivot-1});
如果(边界>(枢轴+1))
add(新整数[]{pivot+1,boundHi});
}
}
ArrayList是这种类型的最佳收藏吗

public class  NewArrayList<T> extends ArrayList<T> {

public NewArrayList() {
    super();
}

public void swap(int index1, int index2) {
    this.set(index1, this.set(index2, this.get(index1)));
} 
}
公共类NewArrayList扩展了ArrayList{
公共NewArrayList(){
超级();
}
公共无效交换(int index1,int index2){
this.set(index1,this.set(index2,this.get(index1));
} 
}
根据建议修改后的代码

public class QuickSort <T extends Number> {

private int first, last, boundLo, boundHi, pivot;
int temp[] = {0, 0};

public QuickSort() {
    super();
}

public void sort(List<T> list) {

    Deque<int[]> stack = new ArrayDeque<int[]>();

    first = 0;
    last = list.size() - 1;

    stack.push(new int[] {first, last});

    while(!stack.isEmpty()) {
        sortStep(list, stack);  
    }
}

private void sortStep(List<T> list, Deque<int[]> stack) {

    temp = stack.pop();
    first = temp[0];
    last = temp[1];

    boundLo = first;
    boundHi = last;
    pivot = last;

    while(first < last) {
        if(list.get(first).doubleValue() >= list.get(pivot).doubleValue()) {
            last--;
            if(first != last) 
                Collections.swap(list, first, last);            
            Collections.swap(list, last, pivot);
            pivot--;
        }
        else first++;
    }

    if(boundLo < (pivot - 1)) 
        stack.add(new int[] {boundLo, pivot - 1});

    if(boundHi > (pivot + 1)) 
        stack.add(new int[] {pivot + 1, boundHi});
}

}
公共类快速排序{
private int first、last、boundLo、boundHi、pivot;
int temp[]={0,0};
公共快速排序(){
超级();
}
公共无效排序(列表){
Deque stack=new ArrayDeque();
第一个=0;
last=list.size()-1;
push(新的int[]{first,last});
而(!stack.isEmpty()){
排序步骤(列表、堆栈);
}
}
私有无效排序步骤(列表、数据堆栈){
temp=stack.pop();
第一个=温度[0];
last=温度[1];
boundLo=第一;
boundHi=最后一个;
枢轴=最后一个;
while(第一次<最后一次){
if(list.get(first).doubleValue()>=list.get(pivot).doubleValue()){
最后--;
如果(第一个!=最后一个)
集合。交换(列表、第一个、最后一个);
集合.交换(列表、最后一个、透视);
支点--;
}
else-first++;
}
如果(边界<(枢轴-1))
add(新的int[]{boundLo,pivot-1});
如果(边界>(枢轴+1))
add(新int[]{pivot+1,boundHi});
}
}
公共类排序{
/**返回已排序的属性列表*/
受保护的int[]sortAttributes1(int[]数组){
Queue Queue=new ArrayDeque();
而(!queue.isEmpty()){
Range=queue.poll();
if(range.isEmpty()){
继续;
}
int left=range.getLeft();
int right=range.getRight();
//划分范围
右=分区(数组、左、右);
Range lr=新范围(Range.getLeft(),right-1);
Range rr=新范围(右+1,Range.getRight());
队列添加(lr);
队列添加(rr);
}
返回数组;
}
私有int分区(int[]数组,int左,int右){
int pivot=右-左>>>2;

while(左)这是一个很好的问题,但它不属于上。请尝试将您的问题发布到上。不要创建ArrayList的子类,仅用于添加
swap
方法。已经有
集合。swap
适用于所有列表。界面上应该有一个好的算法(在本例中为
List
)并且不依赖于特定的实现。并且不要使用
Integer[]
int[]
也可以这样做。通常当人们要求反馈时,他们不只是希望有人从头开始创建一个全新的解决方案。
public class Sort {   
    /** Returns a sorted list of attributes. */
    protected int[] sortAttributes1(int[] array) {

        Queue<Range> queue = new ArrayDeque<Range>();

        while (!queue.isEmpty()) {
            Range range = queue.poll();
            if (range.isEmpty()) {
                continue;
            }
            int left = range.getLeft();
            int right = range.getRight();
            // partition the range
            right = partition(array, left, right);
            Range lr = new Range(range.getLeft(), right - 1);
            Range rr = new Range(right + 1, range.getRight());

            queue.add(lr);
            queue.add(rr);
        }
        return array;
    }


    private int partition(int[] array, int left, int right) {
        int pivot = right - left >>> 2;


        while (left <= right) {
            int pivotVal = array[pivot];
            if (array[left] <= pivotVal) {
                left++;
                continue;
            }
            right--;
            if (left == right)continue;
            int temp = array[left];
            array[left] = array[right];
            array[right] = temp;
        }

        int temp = array[pivot];
        array[pivot] = array[right];
        array[right] = temp;

        return right;
    }    
}