【问题标题】:Insertion Sort?插入排序?
【发布时间】:2015-12-08 10:03:38
【问题描述】:

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

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;
    }
}

然后她让我们修改这个并用这个sn-p代码进行插入排序

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 但仍然无法正常工作。 有人可以帮忙吗??谢谢你。 (:

【问题讨论】:

  • @Bombe,所有的家庭作业问题都不值得一票。她显然已经付出了努力,并在没有产生正确结果时寻求帮助..
  • 如果您可以指定“它仍然无法工作”会很有帮助。您收到什么错误消息?
  • @Codebender 我认为他们确实值得关闭;他们的老师有报酬回答这类问题。
  • 我同意邦贝的观点。你的老师提供的代码写得很糟糕。您只需更改一些变量名并将其添加到您的ArrayBubble 类中。花时间去做,练习和学习。
  • @gefei 它不排序..它只是显示相同的东西.. 77 99 44 55 22 88 11 0 66 33 77 99 44 55 22 88 11 0 66 33

标签: java arrays sorting data-structures


【解决方案1】:

首先,您显然需要在类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;
        }
    }

那你需要修改BubbleSort中你的main方法调用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();
    }

它对我有用。

【讨论】:

    【解决方案2】:
    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-16
      • 2016-02-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多