插入排序算

Php code posted
created at 29 Apr 19:55, updated at 30 Apr 20:40

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 插入排序算法
def insert_sort(data)
    j = 1
    while j < data.size
        key = data[j]
        i = j-1
        while i >= 0 and data[i] > key
            data[i+1] = data[i]     # 右移数据
            i -= 1
        end
        data[i+1] = key
        j += 1
    end
    data
end

puts insert_sort([33,5,53,2,56,7888])
350 Bytes in 2 ms with coderay