python实现选择法升序、插入法升序、希尔排序

#-*-coding:GBK-*
num = 0
data = []
while num != -1:
	num = int(input('Please enter a num:\n'))
	data.append(num)
data.pop()

print('the original data is ')
for i in range(len(data)):
	print('%d' %data[i], end = ' ')
print()

def select(data):
	for i in range(len(data)-1):
		for j in range(i+1, len(data)):
			if data[i] > data[j]:
				data[i], data[j] = data[j], data[i]
				
select(data)
print('最终结果')
for m in range(len(data)):
	print(data[m], end = ' ')

python实现选择法升序、插入法升序、希尔排序_第1张图片插入法
每次都从最后一个元素开始比较,如果比他小的话整个向后移动一位,然后把被比较的数放在前面

def incert(data):
	for i in range(1, len(data)):
		tmp = data[i]
		no = i - 1
		while no >= 0 and tmp < data[no]:
			data[no+1] = data[no]
			no -= 1
		data[no+1] = tmp

希尔排序

def shell(data):
	k = 1
	jmp = len(data) // 2
	while jmp != 0:
		for i in range(jmp, len(data)):
			tmp  = data[i]
			j = i - jmp
			while tmp < data[j] and j >= 0:
				data[j + jmp] = data[j]
				j = j-jmp
			data[jmp +j] = tmp
		print('第%d次排序过程' %k, end = ' ')
		print(data)
		k += 1
		jmp = jmp //2	
		```

你可能感兴趣的:(python,选择法)