Skip to content

Commit

Permalink
Added working MergeSort written in Python
Browse files Browse the repository at this point in the history
  • Loading branch information
x10an14 committed Dec 8, 2015
1 parent 231a1a3 commit 2005b46
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Sublime Text files:
*.sublime-*

# Python compiled code:
*.pyc

22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 x10an14

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

88 changes: 88 additions & 0 deletions sorting_algorithms/merge_sort/python/mergesort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#! python
# For floor division on line 16
from __future__ import division
# Import random number generator for testing
import randomNumberGenerator as rndNrs


# The recursive splitting of the arrays
def mergesort_internal(inputlist):
size = len(inputlist)
if size < 2:
# If only one element in list (basecase)
pass
else:
# Divide
midpoint = size // 2
lower_half = mergesort_internal(inputlist[:midpoint])
upper_half = mergesort_internal(inputlist[midpoint:])

# Conquer
i, j, k = 0, 0, 0
while i < len(lower_half) and j < len(upper_half):
if upper_half[j] < lower_half[i]:
inputlist[k] = upper_half[j]
j += 1
else:
inputlist[k] = lower_half[i]
i += 1
k += 1

# If upper_half emptied first:
while i < len(lower_half):
inputlist[k] = lower_half[i]
i, k = i + 1, k + 1

# if lower_half emptied first:
while j < len(upper_half):
inputlist[k] = upper_half[j]
j, k = j + 1, k + 1

return inputlist


def mergesort(inputlist, start=None, stop=None):
size = len(inputlist)

# Check first if whole array should be sorted
if start is None:
start = 0
if stop is None:
stop = size

# Check then whether indices are valid
if stop - start < 1 or size < start - stop:
return [-1]

# Perform merge sort
return mergesort_internal(inputlist[start:stop])

# Code for executing/testing
print("Starting...")

# Get random number of elements to sort
amountOfIntsSorted = rndNrs.random_ints(1, lower=4, upper=100)
amountOfIntsSorted = amountOfIntsSorted[0]

# Get list of random ints
testList = rndNrs.random_ints(amountOfIntsSorted, lower=0, upper=10000)
# testList = [2, 24, 4, 5, 3, 1]
print(testList)

print("Sorting the above list...")
sortedList = mergesort(testList)

print("Sorted list:")
print(sortedList)

# Check if list was sorted correctly, by using python's sorted() as comparison
correctList = sorted(testList)
correctlySorted = True
for x in xrange(amountOfIntsSorted):
if sortedList[x] != correctList[x]:
correctlySorted = False

if correctlySorted:
print("List was sorted correctly!!! =D")
else:
print("List was sorted incorrectly!!! =(")
11 changes: 11 additions & 0 deletions sorting_algorithms/merge_sort/python/randomNumberGenerator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#! python
import random

# From:
# http://bytes.com/topic/python/answers/829129-generate-random-list-integers


def random_ints(amount, lower=0, upper=100):
return [random.randrange(lower, upper + 1) for i in range(amount)]

# print random_ints(100)

0 comments on commit 2005b46

Please sign in to comment.