java - 我的程序似乎写得很完美,但停止运行并允许用户在没有扫描仪的情况下输入?

标签 java sorting compiler-errors java.util.scanner

第一次在这里发帖。

我正在尝试创建一个比较快速排序、合并排序、冒泡排序和选择排序的类。我已经实现了所有排序方法并创建了一个随机数组方法,该方法用 1000 个随机整数填充随机数组。但是,当我运行程序时,我的主要方法在初始欢迎消息后停止并允许用户输入。任何帮助将不胜感激,我确信我错过了一些简单的错误。

import java.util.Random;
public class TestSort {
private static int selectCount;
private static int bubbleCount;
private static int mergeCount;
private static int quickCount;

public static void main(String[] args){
    System.out.println("Welcome to the search tester.  "
            + "We are going to see which algorithm performs the best out of 20 tests"); 

    int testSelection = 0;
    int testBubble = 0;
    int testQuick = 0;
    int testMerge = 0;

    //Check tests
        int[] a = new int[1000];
        populateArray(a);

        int[] select = a;
        int[] bubble = a;
        int[] quick = a;
        int[] merge = a;

        testSelection = selectionSort(select);
        testBubble = bubbleSort(bubble);
        testQuick = quickSort(quick,0,0);
        testMerge = mergeSort(merge);

        System.out.println("Selection sort number of checks: " + testSelection);
        System.out.println("Bubble sort number of checks: " + testBubble);
        System.out.println("Quick sort number of checks: " + testQuick);
        System.out.println("Merge sort number of checks: " + testMerge);
        System.out.println("");
    }


public static int[] populateArray(int[] a)
{
    Random r = new Random();
    a = new int[1000];


    for(int i=0; i < a.length; i++)
    {
        int num = r.nextInt(1000);
        a[i] = num;
    }
    return a;
}

//Sorting methods

public static int selectionSort(int[] a)
{
    for (int i = 0; i < a.length; i++)
    {
        int smallestIndex = i;
        for (int j=i; j<a.length; j++)
        {
            if(a[j]<a[smallestIndex])
            {
                smallestIndex = j;
            }
        }
        if(smallestIndex != i) //Swap
        {
            int temp = a[i];
            a[i] = a[smallestIndex];
            a[smallestIndex] = temp;
            selectCount++;
        }
    }
    return selectCount;
}

public static int bubbleSort (int[] a)
{
    boolean hasSwapped = true;
    while (hasSwapped == true)
    {
        hasSwapped = true;
        for(int i = 0; i<a.length-1; i++)
        {
            if(a[i] > a[i+1]) //Needs to swap
            {
                int temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
                hasSwapped = true;
                bubbleCount++;
            }
        }
    }
    return bubbleCount;
}

public static int mergeSort(int [] a)
{
    int size = a.length;
    if(size<2)//recursive halting point
    {
        return 0;
    }

    int mid = size/2;
    int leftSize = mid;
    int rightSize = size-mid;
    int [] left = new int[leftSize];
    int [] right = new int[rightSize];

    for(int i = 0; i<mid; i++)
    { 
        mergeCount++;
        left[i] = a[i];
    }
    for(int i = mid; i<size; i++)
    {
        mergeCount++;
        right[i-mid]=a[i];
    }
    mergeSort(left);
    mergeSort(right);
    //merge
    merge(left,right,a);
    return mergeCount;
}
private static void merge(int [] left, int [] right, int [] a)
{
    int leftSize = left.length;
    int rightSize = right.length;
    int i = 0;//index of the left array
    int j = 0; //index of right array
    int k = 0; //index of the sorted array [a]

    while(i<leftSize && j<rightSize)
    {
        if(left[i]<=right[j])
        {
            a[k] = left[i];
            i++;
            k++;
        }
        else
        {
            a[k] = right[j];
            j++;
            k++;
        }
    }
    while(i<leftSize)
    {
        a[k] =left[i];
        i++;
        k++;
    }
    while(j<rightSize)
    {
        a[k] =right[j];
        j++;
        k++;
    }
}

public static int quickSort(int[] a, int left, int right)
{
    //partition, where pivot is picked 
    int index = partition(a,left,right);
    if(left<index-1)//Still elements on left to be sorted
        quickSort(a,left,index-1);
    if(index<right) //Still elements on right to be sorted
        quickSort(a,index+1,right);
        quickCount++;

    return quickCount;
}
private static int partition(int[] a, int left, int right)
{
    int i = left;
    int j = right; //Left and right are indexes
    int pivot = a[(left+right/2)]; //Midpoint, pivot

    while(i<j)
    {
        while(a[i]<pivot)
        {
            i++;
        }
        while(a[j]>pivot)
        {
            j--;
        }
        if(i<=j) //Swap
        {
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
            i++;
            j--;
        }
    }
        return i;
    }
}

最佳答案

你的无限循环是在 bubbleSort 中:

public static int bubbleSort(int[] a)
{
    boolean hasSwapped = true;
    while (hasSwapped == true)
    {
        hasSwapped = false; // Need to set this to false
        for (int i = 0; i < a.length - 1; i++)
        {
            if (a[i] > a[i + 1]) // Needs to swap
            {
                int temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
                hasSwapped = true;
                bubbleCount++;
            }
        }
    }
    return bubbleCount;
}

关于java - 我的程序似乎写得很完美,但停止运行并允许用户在没有扫描仪的情况下输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39669740/

相关文章:

java - 如果我在 Java 中截断文件, "removed"字节会发生什么?

java - 在工作线程中读取 GUI 对象数据

compiler-errors - 对yywrap()的 undefined reference

c# - 通用类型。没有装箱转换或类型参数转换

java - 无法解析以下类的父类(super class)型。请确保您在类路径 : 中具有所需的依赖项

Java Mysql删除日期之间的记录语法错误

java - 使用 Groovy 创建 Oracle 数据库链接

c++ - 二进制搜索插入字符字符串。错误在哪里?

python 3 : values of a list change when it enters a for loop

C++ 使用 std::sort 为非常小的 std::vector 抛出 std::bad_alloc 异常