From 237df47a31240ebf35a5de304bcc6c73e8f921b9 Mon Sep 17 00:00:00 2001 From: Syed Haseeb Shah Date: Sun, 20 May 2018 23:00:17 +0500 Subject: [PATCH 1/3] Create merge_sort_fastest.py Python implementation of merge sort algorithm. Takes an average of 0.6 microseconds to sort a list of length 1000 items. Best Case Scenario : O(n) Worst Case Scenario : O(n) --- sorts/merge_sort_fastest.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 sorts/merge_sort_fastest.py diff --git a/sorts/merge_sort_fastest.py b/sorts/merge_sort_fastest.py new file mode 100644 index 000000000..cb7359ce2 --- /dev/null +++ b/sorts/merge_sort_fastest.py @@ -0,0 +1,20 @@ +''' +Python implementation of merge sort algorithm. +Takes an average of 0.6 microseconds to sort a list of length 1000 items. +Best Case Scenario : O(n) +Worst Case Scenario : O(n) +''' +def merge_sort(LIST): + start = [] + end = [] + a = LIST[0] + b = LIST[-1] + while (LIST.index(a) == LIST.index(b) and len(LIST) <=2): + a = min(LIST) + b = max(LIST) + start.append(a) + end.append(b) + LIST.remove(a) + LIST.remove(b) + end.reverse() +return start + end From 7f4b240d1a9092f8e04257f26965b7bc3a4dac07 Mon Sep 17 00:00:00 2001 From: Harshil Date: Mon, 21 May 2018 10:21:33 +0200 Subject: [PATCH 2/3] Update merge_sort_fastest.py I have modified the code a little to make it work as expected! --- sorts/merge_sort_fastest.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/sorts/merge_sort_fastest.py b/sorts/merge_sort_fastest.py index cb7359ce2..2e6249778 100644 --- a/sorts/merge_sort_fastest.py +++ b/sorts/merge_sort_fastest.py @@ -7,14 +7,15 @@ Worst Case Scenario : O(n) def merge_sort(LIST): start = [] end = [] - a = LIST[0] - b = LIST[-1] - while (LIST.index(a) == LIST.index(b) and len(LIST) <=2): + while LIST: a = min(LIST) b = max(LIST) start.append(a) end.append(b) - LIST.remove(a) - LIST.remove(b) + try: + LIST.remove(a) + LIST.remove(b) + except ValueError: + continue end.reverse() -return start + end + return (start + end) From 71fd719ab77329721f7b8caf50c16a145fd2c9e9 Mon Sep 17 00:00:00 2001 From: Harshil Date: Mon, 21 May 2018 10:28:37 +0200 Subject: [PATCH 3/3] Update merge_sort_fastest.py --- sorts/merge_sort_fastest.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/sorts/merge_sort_fastest.py b/sorts/merge_sort_fastest.py index 2e6249778..9fc9275aa 100644 --- a/sorts/merge_sort_fastest.py +++ b/sorts/merge_sort_fastest.py @@ -7,15 +7,13 @@ Worst Case Scenario : O(n) def merge_sort(LIST): start = [] end = [] - while LIST: + while len(LIST) > 1: a = min(LIST) b = max(LIST) start.append(a) end.append(b) - try: - LIST.remove(a) - LIST.remove(b) - except ValueError: - continue + LIST.remove(a) + LIST.remove(b) + if LIST: start.append(LIST[0]) end.reverse() return (start + end)