首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >插入排序?

插入排序?
EN

Stack Overflow用户
提问于 2015-12-08 10:03:38
回答 2查看 443关注 0票数 0

所以我们的老师给了我们这个气泡分类的密码

代码语言:javascript
复制
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;
    }
}

然后她让我们修改这个,用这个片段代码进行插入排序。

代码语言:javascript
复制
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;
    }
}

这是气泡类型的输出。因此,如果我将它修改为插入,它应该具有相同的输出。

代码语言:javascript
复制
77 99 44 55 22 88 11 0 66 33 
0 11 22 33 44 55 66 77 88 99 

程序已完成。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-08 10:32:31

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

代码语言:javascript
复制
 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

代码语言:javascript
复制
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();
    }

对我来说很管用。

票数 0
EN

Stack Overflow用户

发布于 2018-11-29 11:48:19

代码语言:javascript
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34153007

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档