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

时间:2015-10-28 23:07:10

标签: java sorting insertion-sort

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

更新:泡泡的实例正在传递给选择和插入,现在已经修复了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");
    }
}

1 个答案:

答案 0 :(得分: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);