AcWing 3638. 排序
原题链接
简单
作者:
不知名_4
,
2024-07-26 14:42:22
,
所有人可见
,
阅读 3
Python 代码
先分奇数和偶数,分别排序,再直接合并
快速排序
n = int(input())
nums = list(map(int,input().split()))
left,right = [],[]
for i in nums:
if i%2==1:
left.append(i)
else:
right.append(i)
def Partition(arr,low,high):
p = arr[low] # 标志点
while low<high:
while low<high and arr[high]>=p:
high -= 1
arr[low] = arr[high]
while low<high and arr[low]<p:
low += 1
arr[high] = arr[low]
arr[low] = p
# 返回当前排序完成后,分界点所在位置
return low
def QuickSort(arr,low,high):
if low >= high:
return
mid = Partition(arr,low,high)
if mid-1 > low:
QuickSort(arr,low,mid-1)
if mid+1 < high:
QuickSort(arr,mid+1,high)
QuickSort(left,0,len(left)-1)
QuickSort(right,0,len(right)-1)
left.extend(right)
for i in left:print(i,end=' ')