Commit 7b9e1f6a by BellCodeEditor

auto save

parent d85c24b3
Showing with 70 additions and 0 deletions
a = [55,23,87,62,16]
for i in range(len(a)-1):
for j in range(i+1,len(a)):
if a[i]<a[j]:
a[i],a[j]=a[j],a[i]
print(a)
print(a)
\ No newline at end of file
array=[2,3,5,7,1,4,6,15,5,2,7,9,10,15,9,17,12]
def qs(list):# 创建快速排序函数,传参列表
if len(list)<2:# 如果列表长度小于2
return list# 返回值就是列表本身
else:
mid = int(len(list)/5)
print('mid:',mid)
guess = list[mid]
left=[]
right=[]
# left,right=[],[]
list.remove(guess)
for i in list:
if i>=guess:
right.append(i)
else:
left.append(i)
print(left)
print(guess)
print(right)
return qs(left)+[guess]+qs(right)
print(qs(array))
# 创建快速排序函数,传参列表
# 如果列表长度小于2
# 返回值就是列表本身
# 否则
# 确定索引(可以是最低索引,中间索引,最高索引)
# 获取对应索引的值,作为基准值
# 创建左列表和右列表
# 将列表中的基准值删除
# 对列表进行遍历
# 如果遍历的值大于等于基准值
# 添加到右列表中
# 否则 (也就是小于基准值)
# 添加到左列表中
# 返回值 调用自身函数对左列表,拼接列表格式的基准值,拼接调用自身函数对右列表
# 调用函数对具体列表进行打印
\ No newline at end of file
import random
list=[]
for i in range(10):
list.append(random.randint(1,100))
print(list)
print()
for i in range(len(list)-1):
index=i
for j in range(i+1,len(list)):
if list[index]>list[j]:
index=j
list[i],list[index]=list[index],list[i]
print(list)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment