插入排序问题

时间:2015-12-11 10:50:15

标签: java algorithm

我曾尝试编写一个插入排序算法,我认为应该有效,但事实并非如此。

int [] array = {108, 10, 45, 67, 108, 23, 29, 108, 48, 67,902, 2, 32, 74, 108, 902};

for(out = 1; out < array.length; out++) {
    min = array[out];
    in = out - 1;

    while(array[in] > min && in > 0) {
        array[in + 1] = array[in];
        in--;
    }
    array[in] = min;   
}

运行时得到的输出:

32 45 45 45 45 48 67 74 108 108 108 108 902 902 902 902

预期输出为:

2, 10, 23, 29, 32, 45, 48, 67, 67, 74, 108, 108, 108, 108, 902, 902

2 个答案:

答案 0 :(得分:1)

试试这个here

    int[] array = {902, 902, 48, 108, 32, 45, 45, 67, 74, 108, 45, 45, 3, 108, 902, 108, 902};
    for (int i = 1; i < array.length-1; i++) {
        int x = array[i];
        int j = i;
        while (j > 0 && array[j-1] > x) {
            array[j] = array[j-1];
            j = j - 1;
        }
        array[j] = x;
    }
    System.out.println(Arrays.toString(array));

答案 1 :(得分:1)

def update_issue_points
   agendas = owner.agendas
   self.issue_points = issue_points + 10.0
   self.save
end