a=eval(input("请输入你要排序的列表:")) defcompare(t): if a[t]>a[t+1]: return0 else: return1 defcompare_list(a): lst=[] for i inrange(0,len(a)-1): lst.append(compare(i)) return lst whilesum(compare_list(a))<len(a)-1: for j inrange(0,len(a)-1): if compare(j)==0: c=a[j+1] a[j+1]=a[j] a[j]=c print(a)
a=eval(input("请输入你要排序的列表:")) for i inrange(1,len(a)): j=0 while a[i]>a[j]: j=j+1 else: a.insert(j,a[i]) del a[i+1] print(a)
快速排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
defquick_sort(data): iflen(data)>=2: mid=data[len(data)//2] left,right=[],[] data.remove(mid) for num in data: if num>=mid: right.append(num) else: left.append(num) return quick_sort(left)+[mid]+quick_sort(right) else: return data a=eval(input("请输入你要排序的列表:")) print(quick_sort(a))