简体   繁体   English

快速排序Java

[英]Quick Sort Java

Hi I'm currently working on getting my Quick Sort program working but cannot figure out where I'm going wrong, i have spent hours trying to find out why it's not working but no luck,when I run my code nothing happens. 嗨,我正在努力让我的快速排序程序工作,但无法弄清楚我哪里出错,我花了几个小时试图找出它为什么不起作用但没有运气,当我运行我的代码没有任何反应。 I have four other sorting algorithms working in a similar fashion which is what's confusing me the most. 我还有其他四种排序算法以类似的方式工作,这让我感到困惑。 Below is my code for the Quick Sort program 以下是我的快速排序程序代码

import java.io.IOException;

public class QuickSort  {



public static int[] compute(int[] array, int lower, int higher )throws IOException{

    if(lower<higher){
        int pivot=split(array,lower,higher);
        if(pivot>1)
            compute(array, lower, pivot-1);

        if(pivot+1<higher)
            compute(array, pivot+1, higher);


    }

    return array;

}


public static int split(int[] array, int lower, int higher){

    while(true){
        int pivot=array[lower];

        while(array[lower]<pivot)
            lower++;

        while(array[higher]>pivot)
            higher--;


        if(lower<higher){

            int temp=array[higher];
            array[higher]=array[lower];
            array[lower]=temp;
        }
        else{
        return higher;
    }
    }
}

} }

Here is my Test class that's running the code: 这是我运行代码的Test类:

import java.io.IOException;
import java.util.Scanner;


public class Test extends ReadIn{ 

static BubbleSort bubble=new BubbleSort();


  public static void main(String[] args) throws IOException{
  System.out.println("Enter 1 for BubbleSort\nEnter 2 for Insertion Sort\nEnter 3      for Selection Sort\nEnter 4 for Merge Sort\nEnter 5 for QuickSort\nPlease input sorting algorithm to use for sorting:");
  Scanner input=new Scanner(System.in);
  int number=input.nextInt();
  final long startTime = System.currentTimeMillis();
  int[] Array=read();

  if(number==1){
      Array=BubbleSort.compute(Array);
        for(int i=0;i<BubbleSort.compute(Array).length;i++){
      System.out.println(Array[i]);

    }

  }
  if(number==2){
      Array=InsertionSort.compute(Array);
        for(int i=0;i<InsertionSort.compute(Array).length;i++){
      System.out.println(Array[i]);

    }

  }
  if(number==3){
      Array=SelectionSort.compute(Array);
        for(int i=0;i<SelectionSort.compute(Array).length;i++){
      System.out.println(Array[i]);

    }

  }
  if(number==4){
      Array=MergeSort.compute(Array);
        for(int i=0;i<MergeSort.compute(Array).length;i++){
      System.out.println(Array[i]);

    }

  }
  if(number==5){
  Array=QuickSort.compute(Array,0,Array.length-1);
        for(int i=0;i<QuickSort.compute(Array,0,Array.length-1).length;i++){
      System.out.print(Array[i]);

    }

  }


    final long endTime = System.currentTimeMillis();
    System.out.println("Total execution time: " + (endTime - startTime) );
}





}

When I press 5 nothing happens, the rest work perfectly. 当我按5没有任何反应时,其余的工作完美。 I cant figure out whatsoever what the problem is so any help or input would be appreciated. 我无法弄清楚问题是什么,所以任何帮助或输入将不胜感激。

Your Quicksort code loops forever if there's duplicate numbers in the input, since the numbers can just keep swapping with each other. 如果输入中有重复的数字,您的Quicksort代码将永远循环,因为数字可以保持相互交换。 As mentioned in the comment, you should manually try out your code with a sample array, or check the code running with a debugger . 如评论中所述,您应该使用示例数组手动尝试代码,或者检查使用调试器运行的代码。 See the example array below. 请参阅下面的示例数组。

You might want to switch the while(true) loop to a recursive call to make the code a little clearer. 您可能希望将while(true)循环切换为递归调用,以使代码更清晰一些。 Also, you can practice going through the different steps of Quicksort on my Quicksort online tutorial . 此外,您可以在我的Quicksort在线教程中练习Quicksort的不同步骤。

Say the array = {3,1,2,3} and the pivot is 3. Nothing will change and the code will loop forever. 假设array = {3,1,2,3}并且pivot是3.什么都不会改变,代码将永远循环。

while(true){
    int pivot=array[lower];

    while(array[lower]<pivot)
        lower++;

    while(array[higher]>pivot)
        higher--;


    if(lower<higher){   //this will just swap the 3's with each other. 
        int temp=array[higher];
        array[higher]=array[lower];  
        array[lower]=temp;
    }
    else{
      return higher;
    }
}

While it looks like your program is doing nothing, it probably just loops infinitely inside this loop: 虽然看起来你的程序什么都不做,但它可能只是在这个循环中无限循环:

while(true){
    int pivot=array[lower];

    while(array[lower]<pivot)
        lower++;

    while(array[higher]>pivot)
        higher--;


    if(lower<higher){

        int temp=array[higher];
        array[higher]=array[lower];
        array[lower]=temp;

        // I recommend adding this line for you to see what's going on
        System.out.println("lower="+lower+", higher="+higher+", pivot="+pivot);
    }
    else{
        return higher;
    }
}

for example, if you have equal values in your Array, say value 65 is there twice, that loop will run forever... 例如,如果您的数组中的值相等,那么值65就是两次,该循环将永远运行...

int[] Array={1,41,2,90,32,65,12,43,78,65,46,67};

Also, in your loop above I added and extra printout of higher, lower, and pivot values - it will help you see what is happening inside that devious loop. 此外,在上面的循环中,我添加了额外的更高,更低和透视值的打印输出 - 它将帮助您了解在这个狡猾的循环中发生的事情。

In general writing a while(true) loop is not a good practice, because it's so easy to hang your system on it. 一般来说,写一个while(true)循环不是一个好习惯,因为挂起系统很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM