for j in 1...array.size z=j-1 key=array[j] while (z >= 0) and (array[z] > key) do array[z+1]=array[z] z=z-1 end array[z+1]=key end puts array
降序
for j in 1...array.size key = array[j] z = j-1 while z >= 0 and array[z]<key array[z+1]=array[z] z =z - 1 end array[z+1] = key end