、任意输一批正整数(直到输入 口结束〉,对给定关键字序列进行快速排序,试输出每 一道快速划分的序列结果。
时间: 2023-07-01 16:28:16 浏览: 91
好的,我可以为您提供代码实现。以下是 Python 代码:
```python
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[0]
left = []
right = []
for i in range(1, len(arr)):
if arr[i] < pivot:
left.append(arr[i])
else:
right.append(arr[i])
sorted_left = quick_sort(left)
sorted_right = quick_sort(right)
return sorted_left + [pivot] + sorted_right
arr = []
while True:
try:
num = int(input())
arr.append(num)
except:
break
sorted_arr = quick_sort(arr)
print(sorted_arr)
```
根据示例输入和输出,我们可以将快速排序中每一道划分的序列结果打印出来,如下所示:
```python
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[0]
left = []
right = []
for i in range(1, len(arr)):
if arr[i] < pivot:
left.append(arr[i])
else:
right.append(arr[i])
sorted_left = quick_sort(left)
sorted_right = quick_sort(right)
return sorted_left + [pivot] + sorted_right
arr = []
while True:
try:
num = int(input())
arr.append(num)
except:
break
def print_quick_sort_sequence(arr):
if len(arr) <= 1:
return
pivot = arr[0]
left = []
right = []
for i in range(1, len(arr)):
if arr[i] < pivot:
left.append(arr[i])
else:
right.append(arr[i])
print(left + [pivot] + right)
print_quick_sort_sequence(left)
print_quick_sort_sequence(right)
print_quick_sort_sequence(arr)
sorted_arr = quick_sort(arr)
print(sorted_arr)
```
这样,我们就可以在程序中输出每一道快速划分的序列结果了。
阅读全文