#!/usr/bin/python
#coding=utf-8
import random
def insert(ls):
for i in range(len(ls)-1):
print(i,ls)
min_=ls
index=i
for j in range(i+1,len(ls)):
if ls[j]<min_:
min_=ls[j]
index=j
ls.insert(i,min_)
del ls[index+1]
return ls
a=[5,1,77,34,25,19,29,99,0]
print(insert(a))
直接给出数组排序结果如下:
[yangyang2@localhost test]$ ./sort.sh
(0, [5, 1, 77, 34, 25, 19, 29, 99, 0])
(1, [0, 5, 1, 77, 34, 25, 19, 29, 99])
(2, [0, 1, 5, 77, 34, 25, 19, 29, 99])
(3, [0, 1, 5, 77, 34, 25, 19, 29, 99])
(4, [0, 1, 5, 19, 77, 34, 25, 29, 99])
(5, [0, 1, 5, 19, 25, 77, 34, 29, 99])
(6, [0, 1, 5, 19, 25, 29, 77, 34, 99])
(7, [0, 1, 5, 19, 25, 29, 34, 77, 99])
[0, 1, 5, 19, 25, 29, 34, 77, 99]
#!/usr/bin/python
#coding=utf-8
import random
def insert(ls):
for i in range(len(ls)-1):
print(i,ls)
min_=ls
index=i
for j in range(i+1,len(ls)):
if ls[j]<min_:
min_=ls[j]
index=j
ls.insert(i,min_)
del ls[index+1]
return ls
#print('请随意输入一组数字,以逗号分隔开')
#str=raw_input()
#list=str.split(',')
#print(insert(list))
输入一串数字,输入的数字与上面直接给出的数组一样,但是排序结果不对,结果如下:
[yangyang2@localhost test]$ ./sort.sh
请随意输入一组数字,以逗号分隔开
5,1,77,34,25,19,29,99,0
(0, ['5', '1', '77', '34', '25', '19', '29', '99', '0'])
(1, ['0', '5', '1', '77', '34', '25', '19', '29', '99'])
(2, ['0', '1', '5', '77', '34', '25', '19', '29', '99'])
(3, ['0', '1', '19', '5', '77', '34', '25', '29', '99'])
(4, ['0', '1', '19', '25', '5', '77', '34', '29', '99'])
(5, ['0', '1', '19', '25', '29', '5', '77', '34', '99'])
(6, ['0', '1', '19', '25', '29', '34', '5', '77', '99'])
(7, ['0', '1', '19', '25', '29', '34', '5', '77', '99'])
['0', '1', '19', '25', '29', '34', '5', '77', '99']
求大神分析是为啥?
|