quick sort

Implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import random

def (a, left, right):
if left>=right:
return
p = left + random.randrange(right-left+1)
temp = a[left]
a[left] = a[p]
a[p] = temp
i = left
j = right
x = a[i]
while (i<j) :
while (i<j) and (a[j]>x):

# while (i<j) and (a[j]<x):
j = j - 1
a[i] = a[j]
while (i<j) and (a[i]<=x):

# while (i<j) and (a[i]>=x):
i = i + 1
a[j] = a[i]
a[i] = x
QuickSort(a, left, i-1)
QuickSort(a, i+1, right)

n = int(input("The number of the list: "))
line = input("The unsorted list: ")
a = list(map(int, line.split()))
QuickSort(a, 0, n-1)
for i in range(n):
print(a[i], end=' ')

Notes

Keyboards input

To convert str to speical foramt list, using split() to seperate list and convert items’ type

Outputs format

To print a list in one line seperated by SPACE, using end=’ ‘, which can be used with other symbols like ‘,’