-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThe running time of Quicksort.py
58 lines (41 loc) · 1.06 KB
/
The running time of Quicksort.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
__author__ = 'marina_senyutina'
def sort_insertion(arr1,n):
ti = 0
for j in range(2,n+1):
arr = []
arr = arr1[:j]
temp = arr[j-1]
for i in range(j-1,-1,-1):
temp2 = arr[i-1]
if (temp < temp2) and (i != 0):
arr[i] = temp2
ti += 1
else:
arr[i] = temp
if arr[i] == temp: break
arr1 = arr + arr1[j:]
return ti
t = 0
def quick_sort_lom(ar, first, last):
if first < last:
p = partion(ar, first, last)
quick_sort_lom(ar, first, p-1)
quick_sort_lom(ar, p+1, last)
def partion(ar, first, last):
global t
pv = ar[last]
i = first-1
for j in range(first, last):
if ar[j] <= pv:
i += 1
ar[i],ar[j] = ar[j],ar[i]
t+=1
print(*ar)
ar[i+1],ar[last] = ar[last],ar[i+1]
t+=1
return i+1
n = int(input().strip())
arr1 = [int(c_temp) for c_temp in input().strip().split(' ')]
arr2 = list(arr1)
(quick_sort_lom(arr1,0, len(arr1)-1))
print(sort_insertion(arr2,n)-t)