File tree 2 files changed +46
-0
lines changed
2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ def merge_sort (unsorted ):
2
+ if len (unsorted ) < 2 :
3
+ return unsorted
4
+
5
+ left = unsorted [:len (unsorted )// 2 ]
6
+ right = unsorted [len (unsorted )// 2 :]
7
+ result = []
8
+
9
+ left = merge_sort (left )
10
+ right = merge_sort (right )
11
+
12
+ while len (left ) > 0 and len (right ) > 0 :
13
+ if left [0 ] < right [0 ]:
14
+ result .append (left .pop (0 ))
15
+ else :
16
+ result .append (right .pop (0 ))
17
+ result += left
18
+ result += right
19
+
20
+ return result
Original file line number Diff line number Diff line change
1
+ import statistics
2
+
3
+
4
+ def quick_sort (unsorted ):
5
+ if len (unsorted ) < 2 :
6
+ return unsorted
7
+
8
+ pivot = statistics .median ([unsorted [0 ], unsorted [len (unsorted )// 2 ], unsorted [- 1 ]])
9
+ i = - 1
10
+ j = len (unsorted )
11
+ while True :
12
+ i += 1
13
+ j -= 1
14
+ while unsorted [i ] < pivot :
15
+ i += 1
16
+ while unsorted [j ] > pivot :
17
+ j -= 1
18
+
19
+ if j <= i :
20
+ break
21
+
22
+ temp = unsorted [i ]
23
+ unsorted [i ] = unsorted [j ]
24
+ unsorted [j ] = temp
25
+
26
+ return quick_sort (unsorted [:i ]) + quick_sort (unsorted [i :])
You can’t perform that action at this time.
0 commit comments