java 需要帮助查找以下代码中的错误

vql8enpb  于 2023-02-28  发布在  Java
关注(0)|答案(4)|浏览(84)

下面所附的代码将生成以下输出:

The array is: 4 3 6 9 3 9 5 4 1 9
This array DOES contain 5.
Sorted by Arrays.sort(): 1 3 3 4 4 5 6 9 9 9
Sorted by Sweep Sort: 1 3 3 4 4 5 6 9 9 9
Sorted by Selection Sort: 1 3 3 4 4 5 6 9 9 9
Sorted by Insertion Sort: 1 3 3 4 4 5 6 9 9 9

但事实并非如此。我按照我正在阅读的书中的说明进行了操作,但没有帮助。我能听听你对这些错误可能是什么的看法吗?我不是在要求解决方案,我希望有人指出错误所在的正确方向,以及错误的类型。

import java.util.Arrays;

/**
 * This class looks like it's meant to provide a few public static methods
 * for searching and sorting arrays.  It also has a main method that tests
 * the searching and sorting methods.
 * 
 * TODO: The search and sort methods in this class contain bugs that can
 * cause incorrect output or infinite loops.  Use the Eclipse debugger to 
 * find the bugs and fix them
 */

public class BuggySearchAndSort {

    public static void main(String[] args) {

        int[] A = new int[10];  // Create an array and fill it with small random ints.
        for (int i = 0; i < 10; i++)
            A[i] = 1 + (int)(10 * Math.random());

        int[] B = A.clone();   // Make copies of the array.
        int[] C = A.clone();
        int[] D = A.clone();

        System.out.print("The array is:");
        printArray(A);

        if (contains(A,5))
            System.out.println("This array DOES contain 5.");
        else
            System.out.println("This array DOES NOT contain 5.");

        Arrays.sort(A);  // Sort using Java's built-in sort method!
        System.out.print("Sorted by Arrays.sort():  ");
        printArray(A);   // (Prints a correctly sorted array.)

        bubbleSort(B);
        System.out.print("Sorted by Bubble Sort:    ");
        printArray(B);

        selectionSort(C);
        System.out.print("Sorted by Selection Sort: ");
        printArray(C);

        insertionSort(D);
        System.out.print("Sorted by Insertion Sort: ");
        printArray(D);

    }

    /**
     * Tests whether an array of ints contains a given value.
     * @param array a non-null array that is to be searched
     * @param val the value for which the method will search
     * @return true if val is one of the items in the array, false if not
     */
    public static boolean contains(int[] array, int val) {
        for (int i = 0; i < array.length; i++) {
            if (array[i] == val)
                return true;
            else
                return false;
        }
        return false;
    }

    /**
     * Sorts an array into non-decreasing order.  This inefficient sorting
     * method simply sweeps through the array, exchanging neighboring elements
     * that are out of order.  The number of times that it does this is equal
     * to the length of the array.
     */
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length-1; i++) {
                if (array[j] > array[j+1]) { // swap elements j and j+1
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                }
            }
        }
    }

    /**
     * Sorts an array into non-decreasing order.  This method uses a selection
     * sort algorithm, in which the largest item is found and placed at the end of 
     * the list, then the second-largest in the next to last place, and so on.
     */
    public static void selectionSort(int[] array) {
        for (int top = array.length - 1; top > 0; top--) {
            int positionOfMax = 0;
            for (int i = 1; i <= top; i++) {
                if (array[1] > array[positionOfMax])
                    positionOfMax = i;
            }
            int temp = array[top];  // swap top item with biggest item
            array[top] = array[positionOfMax];
            array[positionOfMax] = temp;
        }
    }

    /**
     * Sorts an array into non-decreasing order.  This method uses a standard
     * insertion sort algorithm, in which each element in turn is moved downwards
     * past any elements that are greater than it.
     */
    public static void insertionSort(int[] array) {
        for (int top = 1; top < array.length; top++) {
            int temp = array[top];  // copy item that into temp variable
            int pos = top - 1;
            while (pos > 0 && array[pos] > temp) {
                   // move items that are bigger than temp up one position
                array[pos+1] = array[pos];
                pos--;
            }
            array[pos] = temp;  // place temp into last vacated position
        }
    }

    /**
     * Outputs the ints in an array on one line, separated by spaces,
     * with a line feed at the end.
     */
    private static void printArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(" ");
            System.out.print(array[i]);
        }
        System.out.println();
    }

}
xfb7svmp

xfb7svmp1#

public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length-1; i++) { //<---- wrong increment. it should be j++
                if (array[j] > array[j+1]) { // swap elements j and j+1
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;

                }
            }
        }
    }

这部分至少有一个错误。2如果你程序员不能确定,你应该检查你的循环。

fwzugrvs

fwzugrvs2#

我已经完成了我的项目,决定放弃一个帮助

public static void main(String[] args) {
    
    int[] A = new int[10];  // Create an array and fill it with small random ints.
    for (int i = 0; i < 10; i++)
        A[i] = 1 + (int)(10 * Math.random());
    
    int[] B = A.clone();   // Make copies of the array.
    int[] C = A.clone();
    int[] D = A.clone();
    
    System.out.print("The array is:");
    printArray(A);
    
    if (contains(A,5))
        System.out.println("This array DOES contain 5.");
    else
        System.out.println("This array DOES NOT contain 5.");
    
    Arrays.sort(A);  // Sort using Java's built-in sort method!
    System.out.print("Sorted by Arrays.sort():  ");
    printArray(A);   // (Prints a correctly sorted array.)

    sweepSort(B);
    System.out.print("Sorted by Sweep Sort:    "); //Changed name 
    printArray(B);

    selectionSort(C);
    System.out.print("Sorted by Selection Sort: ");
    printArray(C);
    
    insertionSort(D);
    System.out.print("Sorted by Insertion Sort: ");
    printArray(D);

}

public static boolean contains(int[] array, int val) {
    for (int i = 0; i < array.length; i++) {
        if (array[i] == val)
            return true;
                                                            //removed else
    }                                                       //removed return false;
    return false;
}

public static void sweepSort(int[] array) {
    for (int i = 0; i < array.length; i++) {
        for (int j = 0; j < array.length-1; j++) { //'i++' => 'j++'
            if (array[j] > array[j+1]) { // swap elements j and j+1
                int temp = array[j];
                array[j] = array[j+1];
                array[j+1] = temp;
            }
        }
    }
}

public static void selectionSort(int[] array) {
    for (int top = array.length - 1; top > 0; top--) {
        int positionOfMax = 0;
        for (int i = 1; i <= top; i++) {
            if (array[i] > array[positionOfMax]) //replaced [1]
                positionOfMax = i;
        }
        int temp = array[top];  // swap top item with biggest item
        array[top] = array[positionOfMax];
        array[positionOfMax] = temp;
    }
}

public static void insertionSort(int[] array) {
    for (int top = 1; top < array.length; top++) {
        int temp = array[top];  // copy item that into temp variable
        int pos = top - 1;
        while (pos >= 0 && array[pos] >= temp) { // '>' => '>='
               // move items that are bigger than temp up one position
            array[pos+1] = array[pos];
            pos--;
        }
        array[pos+1] = temp;  // place temp into last vacated position //added '+1' 
    }
}

private static void printArray(int[] array) {
    for (int i = 0; i < array.length; i++) {
        System.out.print(" ");
        System.out.print(array[i]);
    }
    System.out.println();
}

}
llycmphe

llycmphe3#

谢谢大家的帮助.使用调试器我发现了主要问题
错误1:

public static void sweepSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length-1; i++) {<------// need change i++ to j++
                if (array[j] > array[j+1]) { // swap elements j and j+1
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                }
            }
        }
    }

错误2和3:

public static void insertionSort(int[] array) {
        for (int top = 1; top < array.length; top++) {
            int temp = array[top];  // copy item that into temp variable
            int pos = top - 1;
            while (pos > 0 && array[pos] > temp) { //<----- need to change '>' to '>='
                   // move items that are bigger than temp up one position
                array[pos+1] = array[pos];
                pos--;
            }
            array[pos ] = temp;  // place temp into last vacated position // <------------------------need to change 'array[pos ]' to 'array[pos + 1]'
        }
    }

错误四:

public static boolean contains(int[] array, int val) {
            for (int i = 0; i < array.length; i++) {
                if (array[i] == val)
                    return true;
                else  //<---------- need to remove this
                return false; //<---------- need to remove this
            }
            return false;
        }
57hvy0tb

57hvy0tb4#

package sort;

import java.util.Arrays;

/**
 * This class looks like it's meant to provide a few public static methods
 * for searching and sorting arrays.  It also has a main method that tests
 * the searching and sorting methods.
 * 
 * TODO: The search and sort methods in this class contain bugs that can
 * cause incorrect output or infinite loops.  Use the Eclipse debugger to 
 * find the bugs and fix them
 */
public class BuggySearchAndSort {

    public static void main(String[] args) {

        int[] A = new int[10];  // Create an array and fill it with small random ints.
        for (int i = 0; i < 10; i++)
            A[i] = 1 + (int)(10 * Math.random());

        int[] B = A.clone();   // Make copies of the array.
        int[] C = A.clone();
        int[] D = A.clone();

        System.out.print("The array is:");
        printArray(A);

        if (contains(A,5))
            System.out.println("This array DOES contain 5.");
        else
            System.out.println("This array DOES NOT contain 5.");

        Arrays.sort(A);  // Sort using Java's built-in sort method!
        System.out.print("Sorted by Arrays.sort():  ");
        printArray(A);   // (Prints a correctly sorted array.)

        bubbleSort(B);
        System.out.print("Sorted by Bubble Sort:    ");
        printArray(B);

        selectionSort(C);
        System.out.print("Sorted by Selection Sort: ");
        printArray(C);

        insertionSort(D);
        System.out.print("Sorted by Insertion Sort: ");
        printArray(D);

    }

    /**
     * Tests whether an array of ints contains a given value.
     * @param array a non-null array that is to be searched
     * @param val the value for which the method will search
     * @return true if val is one of the items in the array, false if not
     */
    public static boolean contains(int[] array, int val) {
        for (int i = 0; i < array.length; i++) {
            if (array[i] == val)
                return true;

        }
        return false;
    }

    /**
     * Sorts an array into non-decreasing order.  This inefficient sorting
     * method simply sweeps through the array, exchanging neighboring elements
     * that are out of order.  The number of times that it does this is equal
     * to the length of the array.
     */
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length-1; j++) {
                if (array[j] > array[j+1]) { // swap elements j and j+1
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                }
            }
        }
    }

    /**
     * Sorts an array into non-decreasing order.  This method uses a selection
     * sort algorithm, in which the largest item is found and placed at the end of 
     * the list, then the second-largest in the next to last place, and so on.
     */
    public static void selectionSort(int[] array) {
        for (int top = array.length - 1; top > 0; top--) {
            int positionOfMax = 0;
            for (int i = 0; i <= top; i++) {
                if (array[i] > array[positionOfMax])
                    positionOfMax = i;
            }
            int temp = array[top];  // swap top item with biggest item
            array[top] = array[positionOfMax];
            array[positionOfMax] = temp;
        }
    }

    /**
     * Sorts an array into non-decreasing order.  This method uses a standard
     * insertion sort algorithm, in which each element in turn is moved downwards
     * past any elements that are greater than it.
     */
    public static void insertionSort(int[] array) {
        for (int top = 1; top < array.length; top++) {
            int temp = array[top];  // copy item that into temp variable
            int pos = top ;
            while (pos > 0 && array[pos-1] >= temp) {
                   // move items that are bigger than temp up one position
                array[pos] = array[pos-1];
                pos--;
            }
            array[pos] = temp;  // place temp into last vacated position
        }
    }

    /**
     * Outputs the ints in an array on one line, separated by spaces,
     * with a line feed at the end.
     */
    private static void printArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(" ");
            System.out.print(array[i]);
        }
        System.out.println();
    }

}

相关问题