-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquicksort_algo.py
49 lines (44 loc) · 1.23 KB
/
quicksort_algo.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
def quicksort1(array):
if len(array) < 2:
# return array
print(array)
def quicksort2(array):
if len(array) == 2:
if array[0] > array[1]:
array = array[::-1]
print(array[::-1])
# return array
else:
print(array)
# return array
'''
def quicksort(array):
if len(array) > 2:
pivot = array[0]
s_pivot = []
g_pivot = []
for i in range(1, len(array)):
if array[i] <= pivot:
s_pivot += array[i]
else:
g_pivot += array[i]
print(s_pivot+pivot+g_pivot)
'''
def quicksort(array):
if len(array) < 2:
return array
else:
pivot = array[0]
less = [i for i in array[1:] if i < pivot]
greater = [i for i in array[1:] if i >= pivot]
return quicksort(less)+[pivot]+quicksort(greater)
a = [5]
quicksort1(a)
b= [16,9]
quicksort2(b)
c = [9,16,5,89,7,14,23,2,30,5,9]
print(quicksort(c))
'''
quicksort FUNCTION IS GIVING AN ERROR. INT OBJECT IS NOT ITERABLE AT LINE 23.
AS I THINK I AM TOO DUMB TO UNDERSTAND THIS ERROR, I WILL REALLY LIKE TO GET UNDEERSTOOD(Edit: I understood:)).!
'''