From 18a6cc951017c5d193bd30577371b0afb180e39c Mon Sep 17 00:00:00 2001 From: Scott Schmidt Date: Thu, 6 Aug 2015 13:19:45 -0700 Subject: [PATCH 1/5] Update repo with radix sort --- radix_sort.py | 30 ++++++++++++++++++++++++++++++ test_radix_sort.py | 0 2 files changed, 30 insertions(+) create mode 100644 radix_sort.py create mode 100644 test_radix_sort.py diff --git a/radix_sort.py b/radix_sort.py new file mode 100644 index 0000000..4c10e12 --- /dev/null +++ b/radix_sort.py @@ -0,0 +1,30 @@ +"""Jonny Five's Docstrings are alive!""" + + +def radix_srt(un_list): + radix = 10 + maxLen = False + tmp, placement = -1, 1 + + while not maxLen: + maxLen = True + buckets = [list() for num in range(radix)] + + for i in un_list: + tmp = i // placement + buckets[tmp % radix].append(i) + if maxLen and tmp > 0: + maxLen = False + + a = 0 + for b in range(radix): + buck = buckets[b] + for i in buck: + un_list[a] = i + a += 1 + + placement *= radix + + +if __name__ == '__main__': + pass diff --git a/test_radix_sort.py b/test_radix_sort.py new file mode 100644 index 0000000..e69de29 From 7696c2f10eafa74a5ff406092e98630996c4cd14 Mon Sep 17 00:00:00 2001 From: Jonathan Stallings Date: Thu, 6 Aug 2015 13:48:49 -0700 Subject: [PATCH 2/5] Add simple tests for radix --- test_radix_sort.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test_radix_sort.py b/test_radix_sort.py index e69de29..00693ec 100644 --- a/test_radix_sort.py +++ b/test_radix_sort.py @@ -0,0 +1,40 @@ +from random import shuffle +import pytest + +from radix_sort import radix_srt + + +def test_radix_srt(): + expected = range(20) + actual = expected[:] + shuffle(actual) + radix_srt(actual) + assert expected == actual + + +def test_radix_srt_with_duplicates(): + expected = [1, 3, 3, 6, 7, 8, 8, 8] + actual = expected[:] + shuffle(actual) + radix_srt(actual) + assert expected == actual + + +def test_radix_srt_with_zero_items(): + expected = [] + actual = [] + radix_srt(actual) + assert expected == actual + + +def test_radix_srt_with_one_item(): + expected = [1] + actual = [1] + radix_srt(actual) + assert expected == actual + + +def test_radix_sort_wrong_type(): + with pytest.raises(TypeError): + radix_srt(15) + From 37e09a2c8d6f49d4b164a87e2ff6a6a5afe20c52 Mon Sep 17 00:00:00 2001 From: Jonathan Stallings Date: Thu, 6 Aug 2015 14:46:50 -0700 Subject: [PATCH 3/5] Add performance demo --- radix_sort.py | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/radix_sort.py b/radix_sort.py index 4c10e12..a6ab104 100644 --- a/radix_sort.py +++ b/radix_sort.py @@ -1,7 +1,22 @@ -"""Jonny Five's Docstrings are alive!""" +"""This module contains the radix sort method (radix_srt), which performs an +in-place sort on a passed in list. Radix sort has a best case time +complexity of O(n log n. This implementation of radix sort is stable, but not +adaptive. + +References: + +Radix Sort in Python +http://www.geekviewpoint.com/python/sorting/radixsort +by Isai Damier +""" def radix_srt(un_list): + """Sort a list in place with radix sort. + + args: + un_list: the list to be sorted. + """ radix = 10 maxLen = False tmp, placement = -1, 1 @@ -27,4 +42,25 @@ def radix_srt(un_list): if __name__ == '__main__': - pass + from random import randint + bigints = [randint(9000, 10000) for x in range(1, 10001)] + smallints = [randint(0, 9) for x in range(1, 10001)] + nums = range(0, 10001) + BEST_CASE = smallints + WORST_CASE = bigints + + from timeit import Timer + + SETUP = """from __main__ import BEST_CASE, WORST_CASE, radix_srt""" + + best = Timer('radix_srt({})'.format(BEST_CASE), SETUP).timeit(100) + + worst = Timer('radix_srt({})'.format(WORST_CASE), SETUP).timeit(100) + + print(""" + Best case represented as a list with integers between 0 and 9. + Worst case represented as a list with large integers (here, between + 9000 and 9999). + """) + print('Best Case: {}'.format(best)) + print('Worst Case: {}'.format(worst)) From c5a95149e010eeddb861470d584a56d6aa090415 Mon Sep 17 00:00:00 2001 From: Jonathan Stallings Date: Thu, 6 Aug 2015 14:48:31 -0700 Subject: [PATCH 4/5] Remove unneeded lines --- radix_sort.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/radix_sort.py b/radix_sort.py index a6ab104..07c6473 100644 --- a/radix_sort.py +++ b/radix_sort.py @@ -43,11 +43,8 @@ def radix_srt(un_list): if __name__ == '__main__': from random import randint - bigints = [randint(9000, 10000) for x in range(1, 10001)] - smallints = [randint(0, 9) for x in range(1, 10001)] - nums = range(0, 10001) - BEST_CASE = smallints - WORST_CASE = bigints + WORST_CASE = [randint(9000, 10000) for x in range(1, 10001)] + BEST_CASE = [randint(0, 9) for x in range(1, 10001)] from timeit import Timer From 22d546e8626fc62d52f19e8d11699f215bb0228d Mon Sep 17 00:00:00 2001 From: Jonathan Stallings Date: Thu, 6 Aug 2015 14:49:20 -0700 Subject: [PATCH 5/5] Improve docstring --- radix_sort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/radix_sort.py b/radix_sort.py index 07c6473..23699a3 100644 --- a/radix_sort.py +++ b/radix_sort.py @@ -1,6 +1,6 @@ """This module contains the radix sort method (radix_srt), which performs an in-place sort on a passed in list. Radix sort has a best case time -complexity of O(n log n. This implementation of radix sort is stable, but not +complexity of O(n log n). This implementation of radix sort is stable, but not adaptive. References: @@ -15,7 +15,7 @@ def radix_srt(un_list): """Sort a list in place with radix sort. args: - un_list: the list to be sorted. + un_list: the list of positive integers to be sorted. """ radix = 10 maxLen = False