數列排序
試題 基礎練習
資源限制
時間限制:1.0s 內存限制:512.0MB
問題描述
給定一個長度爲n的數列,將這個數列按從小到大的順序排列。1<=n<=200
輸入格式
第一行爲一個整數n。
第二行包含n個整數,爲待排序的數,每個整數的絕對值小於10000。
輸出格式
輸出一行,按從小到大的順序輸出排序後的數列。
樣例輸入
5
8 3 6 4 9
樣例輸出
3 4 6 8 9
解題:
冒泡排序
x = int(input())
lst = list(map(int,input().split()))
for i in range(0,len(lst)):
for j in range(0,len(lst)-i-1):
if lst[j]>lst[j+1]:
(lst[j],lst[j+1])=(lst[j+1],lst[j])
for a in range(x):
print(lst[a],end=' ')
else:使用python自帶函數sort
n = int(input())
arr = list(map(int,input().split()))
arr.sort()
for i in range(n):
print(arr[i],end=' ')