【问题标题】:Insertion sort: count swaps and comparisons插入排序:计数交换和比较
【发布时间】:2015-10-28 23:07:09
【问题描述】:

由于某种原因,我无法在 InsertionSort 部分中获得比较和交换的计数,它只输出零。当我为它隔离代码时,它会输出一些交换和比较(尽管我不知道它是否错误,考虑到两者的数字相同,可能是错误的)并且数组根本没有排序。我真的很困惑为什么这不起作用,非常感谢任何帮助!

更新:bubble 的实例被传递给选择和插入,现在已经修复了,结果 T 也有选择部分的问题。有关如何修复它们的任何建议?

更新 2:修复了选择部分!仍然对插入感到困惑。

import java.util.Scanner;

public class Sorting {

public static void main(String[] args) {

    int n, c;
Scanner scan = new Scanner(System.in);

System.out.print("Number of elements: ");
n = scan.nextInt();
int[] bubbleSortArray = new int[n];
int[] selectionSortArray = new int[n];
int[] insertionSortArray = new int[n];

System.out.print("Enter " + n + " elements: ");

for (c = 0; c < n; c++) {
    int i = scan.nextInt();
    bubbleSortArray[c] = i;
    selectionSortArray[c] = i;
    insertionSortArray[c] = i;
}

BubbleSort(bubbleSortArray);
SelectionSort(selectionSortArray);
InsertionSort(insertionSortArray);

}


static void BubbleSort(int[] array) {

    int n = array.length;
    int cm = 0;
    int sw = 0;

    for (int c = 0; c < (n - 1); c++) {
        for (int d = 0; d < n - c - 1; d++) {
            cm++;
            if (array[d] > array[d + 1]) {
                int swap = array[d];
                array[d] = array[d + 1];
                array[d + 1] = swap;
                sw++;
            }
        }
    }

    System.out.print("Bubble sort: ");

    for (int c = 0; c < n; c++) {
        System.out.print(array[c] + " ");


    }
    System.out.println("- " + cm + " comparisons, " + sw + " swaps");
}

static void SelectionSort(int[] array) {

     int n = array.length;
     int cm = 0;
     int sw = 0;

     for (int c = 0; c < n - 1; c++) {
            int index = c;
            for (int d = c + 1; d < n; d++){
                cm++;
                if (array[d] < array[index])
                    index = d;
            }
            int temp = array[index]; 
            sw++;
            array[index] = array[c];
            array[c] = temp;
        }
        System.out.print("Selection sort: ");   
        for (int c = 0; c < n; c++) {
            System.out.print(array[c] + " ");

            }
        System.out.println("- " + cm + " comparisons, " + sw + " swaps");
        }

static void InsertionSort(int[] array) {

    int n = array.length;
    int cm = 0;
    int sw = 0;

    for (int c = 1; c < n; c++){
        int temp = array[c];
        for (int d = c - 1; d > 0 && temp < array[d]; d--) {
            array[d+1] = array[d];
            array[d+1] = temp;
            cm++;
            sw++;

        }
    }
    System.out.print("Insertion sort: ");   
    for (int c = 0; c < n; c++) {
        System.out.print(array[c] + " ");


}
    System.out.println("- " + cm + " comparisons, " + sw + " swaps");
    }
}

【问题讨论】:

    标签: java sorting insertion-sort


    【解决方案1】:

    在完成 BubbleSort 数组排序后,您将该排序后的实例传递给 SelectionSort 和 InsertionSort。

    如果您想获得每种类型的结果,您可以这样做:

        int n, c;
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Number of elements: ");
        n = scan.nextInt();
        int[] bubbleSortArray = new int[n];
        int[] selectionSortArray = new int[n];
        int[] insertionSortArray = new int[n];
    
        System.out.print("Enter " + n + " elements: ");
    
        for (c = 0; c < n; c++) {
            int i = scan.nextInt();
            bubbleSortArray[c] = i;
            selectionSortArray[c] = i;
            insertionSortArray[c] = i;
        }
    
        BubbleSort(bubbleSortArray);
        SelectionSort(selectionSortArray);
        InsertionSort(insertionSortArray);
    

    【讨论】:

    • 啊,好吧,我假设发生了类似的事情。谢谢你!我现在要更新问题,插入部分仍然存在同样的问题 + 选择现在也没有给出正确的输出。
    • 插入排序应该是这样的:for (int c = 1; c &lt; n; c++) { int temp = array[c]; int d; for (d = c - 1; d &gt;= 0 &amp;&amp; temp &lt; array[d]; d--) { array[d + 1] = array[d]; cm++; sw++; } array[d + 1] = temp; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 2023-03-05
    • 2021-03-22
    • 2015-07-08
    • 2017-08-06
    相关资源
    最近更新 更多