a[j+1]_"def main(): a = [] n">
赞
踩
冒泡排序
- n = int(input("请输入数组的长度:"))
- a = []
- for i in range(n):
- a.append(int(input("请输入数组元素:")))
- i = 0
- def bubblesort (a,n) :
- if (n<=1):
- return
- for i in range(n):
- j = 0
- while (j+1<n-i):
- if (a[j]>a[j+1]):
- a[j],a[j+1] = a[j+1],a[j]
- j+=1
- return (a)
- bubblesort(a,n)
- print(a)
插入排序
- n=int(input("请输入数组的长度:"))
- a=list(map(int,input().split()))
- if(n<=1):
- print(a)
- else:
- for i in range(n):
- j=i-1
- while(j>=0):
- if(a[j]>a[j+1]):
- a[j],a[j+1]=a[j+1],a[j]
- j-=1
- else:
- break
- print(a)
选择排序
- n = int(input("请输入数组的长度:"))
- s = list (map(int,input("请以空格为间隔输入数组:").split()))
- if n<=1:
- print(s)
- else:
- for i in range(n-1):
- min = i
- for j in range(i,n):
- if s[j]<s[min]:
- min = j
- s[i],s[min]=s[min],s[i]
- print(s)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。