蓝桥杯打卡day3——排序算法

冒泡排序

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+1a[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]

你可能感兴趣的:(蓝桥杯,职场和发展)