Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.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_Selection Sort - Fatal编程技术网

如何在Java中循环选择排序?

如何在Java中循环选择排序?,java,selection-sort,Java,Selection Sort,我想展示选择排序的每个迭代,以打印出它是如何工作的,我将如何循环并打印它?我让它在分类后打印输出。这是我的密码: public class TestSelectionSort { public static void main(String[] args) { int list[] = { 2, 56, 34, 25, 73, 46, 89, 10, 5, 16 }; selectionSort(list, list.length);

我想展示选择排序的每个迭代,以打印出它是如何工作的,我将如何循环并打印它?我让它在分类后打印输出。这是我的密码:

public class TestSelectionSort {

    public static void main(String[] args) {

        int list[] = { 2, 56, 34, 25, 73, 46, 89, 10, 5, 16 };

        selectionSort(list, list.length);

        System.out.println("After sorting, the list elements are:");

        for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + " ");
        }

    }

    public static void selectionSort(int[] list, int listLength) {
        int index;
        int smallestIndex;
        int minIndex;
        int temp;

        for (index = 0; index < listLength - 1; index++) {
            //Step a
            smallestIndex = index;

            for (minIndex = index + 1; minIndex < listLength; minIndex++)
                if (list[minIndex] < list[smallestIndex])
                    smallestIndex = minIndex;

            //Step b
            temp = list[smallestIndex];
            list[smallestIndex] = list[index];
            list[index] = temp;

        }

    }
}
公共类TestSelectionSort{
公共静态void main(字符串[]args){
int list[]={2,56,34,25,73,46,89,10,5,16};
选择排序(列表、列表、长度);
System.out.println(“排序后,列表元素为:”);
for(int i=0;i
只需复制用于打印最终结果的代码段:

for(int i = 0; i < list.length; i++)
{
    System.out.print(list[i] + " ");
}
for(int i=0;i

selectionSort()

中循环结束之前,可以通过在选择排序的外部循环结束处添加打印语句来完成此操作。例如:

public static void selectionSort(int[] list, int listLength) {
        int index;
        int smallestIndex;
        int minIndex;
        int temp;

        for (index = 0; index < listLength - 1; index++) {
            //Step a
            smallestIndex = index;

            for (minIndex = index + 1; minIndex < listLength; minIndex++)
                if (list[minIndex] < list[smallestIndex])
                    smallestIndex = minIndex;

            //Step b
            temp = list[smallestIndex];
            list[smallestIndex] = list[index];
            list[index] = temp;

            System.out.println("Printing data for iteration no " + index);
            printData(list);

        }

    }

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

        System.out.println();
    }
publicstaticvoidselectionsort(int[]列表,int listLength){
整数指数;
int smallestIndex;
int-minIndex;
内部温度;
对于(索引=0;索引
非常感谢……我想我已经试过了,不过我可能已经把它放在我的循环之外了。