From 95d0561fe1d1b4df1dba8cbd129b24be0fb85ceb Mon Sep 17 00:00:00 2001 From: Haris Riaz Date: Sat, 27 Oct 2018 01:25:34 +0500 Subject: [PATCH 1/3] 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/3] 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/3] 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." +