a =[10,9,8,7,6,5,4,3,2,1]for i inrange(len(a)-1):for j inrange(len(a)-1-i):if( a[j]> a[j+1]):
temp = a[j]
a[j]= a[j +1]
a[j +1]= temp
print("排序后的列表为", a)
2. 选择排序
a =[10,9,8,7,6,5,4,3,2,1]for i inrange(len(a)-1):for j inrange(i+1,len(a)):if( a[i]> a[j]):
temp = a[i]
a[i]= a[j]
a[j]= temp
print("排序后的列表为", a)
3. 选择法排序
a =[10,9,8,7,6,5,4,3,2,1]for i inrange(1,len(a)):
wait = a[i]
j = i -1while j>=0:if( a[j]> wait ):
a[j+1]= a[j]else:break;
j = j -1
a[j+1]= wait
print("排序后的列表为", a)
4. 折半法排序
a =[10,9,8,7,6,5,4,3,2,1]for i inrange(1,len(a)):
wait = a[i]
low =0
high = i -1while( low <= high ):
mid =(low + high)//2if( wait < a[mid]):
high = mid -1else:
how = mid +1
j = i -1while( j > high ):
a[j +1]= a[j]
j = j -1
a[high +1]= wait
print("排序后的列表为", a)
5. 希尔排序
a =[10,9,8,7,6,5,4,3,2,1]
step =len(a)//2while( step >0):for i inrange(0+step,len(a), step):
wait = a[i]
j = i -1while( j >=0):if( a[j]> wait ):
a[j +1]= a[j]else:break
j = j -1
a[ j +1]= wait
step //=2print("排序后的列表为", a)
6. 桶排序
a =[10,9,8,7,6,5,4,3,2,1,20]
b =[]
bucket =[]for i inrange(max(a)+1):
bucket.append(0)for j inrange(len(a)):
bucket[a[j]]+=1for k inrange(max(a),0,-1):for l inrange(bucket[k],0,-1):
b.append(k)print("排序后的列表为", b)