Skip to content

Create merge_sort_fastest.py #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 21, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions sorts/merge_sort_fastest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'''
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)
'''

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your function don't work. I tested it:

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)

liste = [4,1,99,3,2,2,0,100]
print liste
print merge_sort(liste)

The output is:

[4, 1, 99, 3, 2, 2, 0, 100]
[]

def merge_sort(LIST):
start = []
end = []
while len(LIST) > 1:
a = min(LIST)
b = max(LIST)
start.append(a)
end.append(b)
LIST.remove(a)
LIST.remove(b)
if LIST: start.append(LIST[0])
end.reverse()
return (start + end)