File tree Expand file tree Collapse file tree 1 file changed +17
-7
lines changed Expand file tree Collapse file tree 1 file changed +17
-7
lines changed Original file line number Diff line number Diff line change 1
1
2
2
def quick_sort (arr ):
3
3
4
- # Check if array is divisable
4
+ ''' Check if array is divisable else
5
+ retrun single element array '''
5
6
if len (arr )> 1 :
6
7
7
8
start_index = 0
8
9
end_index = len (arr )
9
- mid_point = int ((start_index + end_index )/ 2 )
10
-
11
- list1 = list (filter (lambda x : x < arr [mid_point ],arr ))
12
- list2 = list (filter (lambda x : x > arr [mid_point ],arr ))
13
-
10
+ mid_index = int ((start_index + end_index )/ 2 )
11
+
12
+ '''
13
+ Divide the array in 2 sub arrays and arrange them as
14
+ 1 - This will put the mid point in its sorted index
15
+ 2 - Smaller then mid_point
16
+ 3 - Greater then mid_point
17
+ '''
18
+ list1 = list (filter (lambda x : x < arr [mid_index ],arr ))
19
+ list2 = list (filter (lambda x : x > arr [mid_index ],arr ))
20
+
21
+ '''Continue division till we get last undivisable element'''
14
22
list1 = quick_sort (list1 )
23
+
15
24
list2 = quick_sort (list2 )
16
25
17
- list1 .append (arr [mid_point ])
26
+ list1 .append (arr [mid_index ])
18
27
28
+ '''Append sorted sub arrays and return '''
19
29
return (list1 + list2 )
20
30
else :
21
31
return arr
You can’t perform that action at this time.
0 commit comments