File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Author: OMKAR PATHAK
2
+ # Created On: 31st July 2017
3
+
4
+ # Best = Average = Worst = O(nlog(n))
5
+
6
+ # merge function to merge the separated lists
7
+ def merge (a ,b ):
8
+ """ Function to merge two arrays """
9
+ c = []
10
+ while len (a ) != 0 and len (b ) != 0 :
11
+ if a [0 ] < b [0 ]:
12
+ c .append (a [0 ])
13
+ a .remove (a [0 ])
14
+ else :
15
+ c .append (b [0 ])
16
+ b .remove (b [0 ])
17
+ if len (a ) == 0 :
18
+ c += b
19
+ else :
20
+ c += a
21
+ return c
22
+
23
+ # Code for merge sort
24
+ def sort (x ):
25
+ """ Function to sort an array using merge sort algorithm """
26
+ if len (x ) == 0 or len (x ) == 1 :
27
+ return x
28
+ else :
29
+ middle = len (x )// 2
30
+ a = sort (x [:middle ])
31
+ b = sort (x [middle :])
32
+ return merge (a ,b )
33
+
34
+ # time complexities
35
+ def bestcase_complexity ():
36
+ return 'O(nlogn)'
37
+
38
+ def averagecase_complexity ():
39
+ return 'O(nlogn)'
40
+
41
+ def worstcase_complexity ():
42
+ return 'O(nlogn)'
43
+
44
+ # easily retrieve the source code of the sort function
45
+ def get_code ():
46
+ import inspect
47
+ return inspect .getsource (sort ), inspect .getsource (merge )
You can’t perform that action at this time.
0 commit comments