插入排序:计数交换和比较

Insertion sort: count swaps and comparisons

出于某种原因,我无法在 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");
    }
}

完成 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);