插入排序?

时间:2015-12-08 10:03:38

标签: java arrays sorting data-structures

所以我们的老师给了我们这个泡泡排序代码

public class BubbleSort {
    public static void main(String[] args) {
        int maxSize = 100;
        ArrayBubble arr;
        arr = new ArrayBubble(maxSize);
        arr.insert(77);
        arr.insert(99);
        arr.insert(44);
        arr.insert(55);
        arr.insert(22);
        arr.insert(88);
        arr.insert(11);
        arr.insert(00);
        arr.insert(66);
        arr.insert(33);
        arr.display();
        arr.bubbleSort();
        arr.display();
    }
}
class ArrayBubble {
    private int[] list;
    private int nItems;
    public ArrayBubble(int max) {
        list = new int[max];
        nItems = 0;
    }
    public void insert(int value) {
        list[nItems] = value;
        nItems++;
    }
    public void display() {
        for (int j = 0; j < nItems; j++)
        System.out.print(list[j] + " ");
        System.out.println("");
    }
    public void bubbleSort() {
        int out, in ;
        for (out = nItems - 1; out > 1; out--)
        for ( in = 0; in < out; in ++)
        if (list[ in ] > list[ in +1]) swap( in , in +1);
    }
    public void swap(int one, int two) {
        int temp = list[one];
        list[one] = list[two];
        list[two] = temp;
    }
}

然后她要求我们修改此内容并使用此代码段进行插入排序

public void insertionSort() {
    int out, in ;
    for (out = 1; out < nItems; out++) {
        double temp = arr[out]; in = out;
        while ( in > 0 && arr[ in -1] >= temp) {
            arr[ in ] = a[ in -1];
            -- in ;
        }
        arr[ in ] = temp;
    }
}

这是冒泡排序的输出..所以如果我将其修改为插入,它应该具有相同的输出。

77 99 44 55 22 88 11 0 66 33 
0 11 22 33 44 55 66 77 88 99 

流程已完成。

我尝试用此代码替换冒泡排序代码,并将arr.bubbleSort重命名为arr.insertionSort但仍然无法正常工作。 谁能帮忙?谢谢。 (:

2 个答案:

答案 0 :(得分:0)

首先,您显然需要在类ArrayBubble中添加一个方法来实现插入排序。我对你的代码进行了一些修改,因为它没有编译。有关问题,请参阅评论

 public void insertionSort() {
        int out, in ;
        for (out = 1; out < nItems; out++) {
            int temp = list[out]; in = out; // -- temp should be int, since your list is an array of int. and your array is called list, not arr
            while ( in > 0 && list[ in -1] >= temp) {
                list[ in ] = list[ in -1];
                --in ;
            }
            list[ in ] = temp;
        }
    }

然后,您需要修改main中的BubbleSort方法以致电insertionSort

public static void main(String[] args) {
        int maxSize = 100;
        ArrayBubble arr;
        arr = new ArrayBubble(maxSize);
        arr.insert(77);
        arr.insert(99);
        arr.insert(44);
        arr.insert(55);
        arr.insert(22);
        arr.insert(88);
        arr.insert(11);
        arr.insert(00);
        arr.insert(66);
        arr.insert(33);
        arr.display();
        //arr.bubbleSort();
        arr.insertionSort();
        arr.display();
    }

它对我有用。

答案 1 :(得分:0)

Here is my solution for insertion sort. 
int[] input = { 5, 3, 2, 21, 70, 17, 2, 5, 19, 11 };
int[] result = insertionSort(input);

private static int[] insertionSort(int[] input) {
    if (input == null) {                                // if array is null, print this is a null array
        System.out.println("null array");
    } else if (input.length < 2) {                      // if array is empty or has 1 element, no need to sort, will return as is
        System.out.println("already sorted: ");
    } else {
        for (int i = 1; i < input.length; i++) {        // begin with the second element and iterate until the end
            for (int j = i - 1; j >= 0; j--) {          // from beginning until j will be already sorted
                if (input[j] > input[i]) {              // if index is less then the previous, swap and continue until the beginning of the list
                    swapWithPrev(input, i);             
                    i--;
                }
            }
        }
    }
    return input;
}

private static void swapWithPrev(int[] input, int index) {
    int temp = input[index - 1];
    input[index - 1] = input[index];
    input[index] = temp;
}