繁体   English   中英

我的程序看起来写得很完美,但是停止运行并且允许用户输入而无需扫描仪?

[英]My program seems to be written perfectly, but stops running and allows user input with no 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;
}

问题出在您的bubbleSort()方法中。 hasSwapped布尔值永远不会设置为false,因此while循环无限次。

您的代码中还有另一个问题。 main方法,您必须将分配arraypopulateArray()方法返回到a 这样的赋值如int[] select = a; 您在main方法中所做的事情不会做您想做的事情。 而是将数组a发送给您的排序方法。

像这样:

int[] a = new int[1000];
a=populateArray(a);

testSelection = selectionSort(a);
testBubble = bubbleSort(a);
testQuick = quickSort(a,0,0);
testMerge = mergeSort(a);

暂无
暂无

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

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