b373: [福州19中]车厢重组
# -*- coding: utf-8 -*-
#b373: [福州19中]车厢重组
def bubble_sort(nums):
n = len(nums)
m=0
for i in range(n-1):
for j in range(n-i-1):
if nums[j] > nums[j+1]:
print(nums)
m=m+1
temp = nums[j]
nums[j] = nums[j+1]
nums[j+1] = temp
print('一个数据,是最少的旋转次数-->' ,m)
nums =[]
print('b373: [福州19中]车厢重组')
while True:
try:
n = int(input('车厢总数N--->')) #
nums = list(map(int,input("\nEnter the numbers : ").strip().split()))[:n]
print("排序前 = %s" % nums)
bubble_sort(nums)
print("排序後 = %s" % nums)
except:
break
>>> %Run -c $EDITOR_CONTENT
b373: [福州19中]车厢重组
车厢总数N--->4
Enter the numbers : 4 3 2 1
排序前 = [4, 3, 2, 1]
[4, 3, 2, 1]
[3, 4, 2, 1]
[3, 2, 4, 1]
[3, 2, 1, 4]
[2, 3, 1, 4]
[2, 1, 3, 4]
一个数据,是最少的旋转次数--> 6
排序後 = [1, 2, 3, 4]
车厢总数N--->
>>>
沒有留言:
張貼留言