diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae62a0d --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +# Sublime Text files: +*.sublime-* + +# Python compiled code: +*.pyc + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ca60403 --- /dev/null +++ b/LICENSE @@ -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. + diff --git a/sorting_algorithms/merge_sort/python/mergesort.py b/sorting_algorithms/merge_sort/python/mergesort.py new file mode 100644 index 0000000..71b7ca7 --- /dev/null +++ b/sorting_algorithms/merge_sort/python/mergesort.py @@ -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!!! =(") diff --git a/sorting_algorithms/merge_sort/python/randomNumberGenerator.py b/sorting_algorithms/merge_sort/python/randomNumberGenerator.py new file mode 100644 index 0000000..f3786e8 --- /dev/null +++ b/sorting_algorithms/merge_sort/python/randomNumberGenerator.py @@ -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)