From 95d0561fe1d1b4df1dba8cbd129b24be0fb85ceb Mon Sep 17 00:00:00 2001 From: Haris Riaz Date: Sat, 27 Oct 2018 01:25:34 +0500 Subject: [PATCH 1/5] Implemented in-place counting sort --- sorting/counting.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 sorting/counting.py diff --git a/sorting/counting.py b/sorting/counting.py new file mode 100644 index 0000000..addbd8d --- /dev/null +++ b/sorting/counting.py @@ -0,0 +1,38 @@ + +"""Python implementation of in-place counting sort algorithm""" + +# input args: +# 1. list of values to be sorted +# 2. max val in list +# output: +# sorted list + + +def counting_sort(array, maxval): + n = len(array) + m = maxval + 1 + # init with zeros + count = [0] * m + for a in array: + # count occurences + count[a] += 1 + i = 0 + for a in range(m): + # make 'count[a]' copies of 'a' + for c in range(count[a]): + array[i] = a + i += 1 + return array + + +import sys + +# accessing command line arguments + +input = sys.argv[1] +array = map(int, input.strip('[]').split(',')) +maxval = int(sys.argv[2]) + +# printing sorted array + +print("Sorted array: ", counting_sort(array, maxval)) From 1d880ec3b2f70e1965ef3b181cdf51ff55d5241b Mon Sep 17 00:00:00 2001 From: Haris Riaz Date: Sat, 27 Oct 2018 01:30:15 +0500 Subject: [PATCH 2/5] Add comments on calling from command line --- sorting/counting.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sorting/counting.py b/sorting/counting.py index addbd8d..da8e74c 100644 --- a/sorting/counting.py +++ b/sorting/counting.py @@ -4,6 +4,7 @@ # input args: # 1. list of values to be sorted # 2. max val in list +# call format for counting.py: $python counting.py "[list of integers/floats]" "max int/float val" # output: # sorted list From 3f1e9a4166c34304c90cae2f04fd0afd73de49be Mon Sep 17 00:00:00 2001 From: Haris Riaz Date: Sat, 27 Oct 2018 17:53:52 +0500 Subject: [PATCH 3/5] Added tests for in-place counting sort --- sorting/counting.py | 22 +--------------------- tests.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/sorting/counting.py b/sorting/counting.py index da8e74c..9306b63 100644 --- a/sorting/counting.py +++ b/sorting/counting.py @@ -1,15 +1,7 @@ """Python implementation of in-place counting sort algorithm""" -# input args: -# 1. list of values to be sorted -# 2. max val in list -# call format for counting.py: $python counting.py "[list of integers/floats]" "max int/float val" -# output: -# sorted list - - -def counting_sort(array, maxval): +def countingsort(array, maxval): n = len(array) m = maxval + 1 # init with zeros @@ -25,15 +17,3 @@ def counting_sort(array, maxval): i += 1 return array - -import sys - -# accessing command line arguments - -input = sys.argv[1] -array = map(int, input.strip('[]').split(',')) -maxval = int(sys.argv[2]) - -# printing sorted array - -print("Sorted array: ", counting_sort(array, maxval)) diff --git a/tests.py b/tests.py index 270c350..ca41035 100644 --- a/tests.py +++ b/tests.py @@ -55,3 +55,13 @@ print "Bucket Sort incorrect" except: print "Bucketsort function errored or is incomplete" + +try: + from counting import countingsort + if(countingsort(list(nums), numpy.max(nums)) == sortedNums): + print "Counting Sort success!" + else: + print "Counting Sort incorrect." +except: + print "Countingsort function errored or is incomplete." + From 3f06f9118b922dd0d8d314dda6c26a7a845cd690 Mon Sep 17 00:00:00 2001 From: Haris Riaz Date: Sat, 27 Oct 2018 18:06:56 +0500 Subject: [PATCH 4/5] Updated README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index cda093b..a58c2f1 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,8 @@ Go ahead and add a one line intro about you and add your favorite emoji (you can - Hi, my name is Brandon and my favorite emoji is 🍔 +- Hi!, I'm Haris and my favorite emoji is 👨‍💻 + ## Conclusion Thank you for the overwhelming amount of contributions! I hope that everybody made their 4 pull requests for Hacktoberfest, and that your journey to open source doesn't end here. I am *slowly* getting through the pull requests. Check back if you don't see your changes in this repo! Best of luck :) From 40c847c1ff1b4a019fa4f08f26d19a1b4c3f8b89 Mon Sep 17 00:00:00 2001 From: Haris Riaz Date: Sun, 28 Oct 2018 06:53:13 +0500 Subject: [PATCH 5/5] Added an implementation of timsort algorithm along with tests --- sorting/tim.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests.py | 8 +++++ 2 files changed, 89 insertions(+) create mode 100644 sorting/tim.py diff --git a/sorting/tim.py b/sorting/tim.py new file mode 100644 index 0000000..23c0a5d --- /dev/null +++ b/sorting/tim.py @@ -0,0 +1,81 @@ + + +def binary_search(the_array, item, start, end): + if start == end: + if the_array[start] > item: + return start + else: + return start + 1 + if start > end: + return start + + mid = round((start + end)/ 2) + + if the_array[mid] < item: + return binary_search(the_array, item, mid + 1, end) + + elif the_array[mid] > item: + return binary_search(the_array, item, start, mid - 1) + + else: + return mid + +# If array or size of run is small, use Insertion sort + +def insertion_sort(the_array): + l = len(the_array) + for index in range(1, l): + value = the_array[index] + pos = binary_search(the_array, value, 0, index - 1) + the_array = the_array[:pos] + [value] + the_array[pos:index] + the_array[index+1:] + + return the_array + +# use merge function of mergesort + +def merge(left, right): + + if not left: + return right + if not right: + return left + if left[0] < right[0]: + return [left[0]] + merge(left[1:], right) + return [right[0]] + merge(left, right[1:]) + +def timsort(the_array): + runs, sorted_runs = [], [] + length = len(the_array) + new_run = [the_array[0]] + + # for every i in the range of 1 to length of array + for i in range(1, length): + # if i is at the end of the list + if i == length - 1: + new_run.append(the_array[i]) + runs.append(new_run) + break + # if the i'th element of the array is less than the one before it + if the_array[i] < the_array[i-1]: + # if new_run is set to None (NULL) + if not new_run: + runs.append([the_array[i]]) + new_run.append(the_array[i]) + else: + runs.append(new_run) + new_run = [the_array[i]] + # else if its equal to or more than + else: + new_run.append(the_array[i]) + + # for every item in runs, append it using insertion sort + for item in runs: + sorted_runs.append(insertion_sort(item)) + + # for every run in sorted_runs, merge them + sorted_array = [] + for run in sorted_runs: + sorted_array = merge(sorted_array, run) + + return sorted_array + diff --git a/tests.py b/tests.py index ca41035..773aa52 100644 --- a/tests.py +++ b/tests.py @@ -65,3 +65,11 @@ except: print "Countingsort function errored or is incomplete." +try: + from tim import timsort + if(timsort(list(nums)) == sortedNums): + print "Timsort success!" + else: + print "Timsort incorrect." +except: + print "Timsort function errored or is incomplete." \ No newline at end of file