|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Radix Sort\n", |
| 8 | + "(c) 2020, Joe James" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": 5, |
| 14 | + "metadata": {}, |
| 15 | + "outputs": [], |
| 16 | + "source": [ |
| 17 | + "# get number of digits in largest item\n", |
| 18 | + "def __get_num_digits(A):\n", |
| 19 | + " m = 0\n", |
| 20 | + " for item in A:\n", |
| 21 | + " m = max(m, item)\n", |
| 22 | + " return len(str(m))" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "code", |
| 27 | + "execution_count": 6, |
| 28 | + "metadata": {}, |
| 29 | + "outputs": [], |
| 30 | + "source": [ |
| 31 | + "# flatten into a 1D List\n", |
| 32 | + "from functools import reduce\n", |
| 33 | + "def __flatten(A):\n", |
| 34 | + " return reduce(lambda x, y: x + y, A)" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": 7, |
| 40 | + "metadata": {}, |
| 41 | + "outputs": [], |
| 42 | + "source": [ |
| 43 | + "def radix(A, num_digits):\n", |
| 44 | + " for digit in range(0, num_digits):\n", |
| 45 | + " B = [[] for i in range(10)]\n", |
| 46 | + " for item in A:\n", |
| 47 | + " # num is the bucket number that the item will be put into\n", |
| 48 | + " num = item // 10 ** (digit) % 10\n", |
| 49 | + " B[num].append(item)\n", |
| 50 | + " A = __flatten(B)\n", |
| 51 | + " return A" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "code", |
| 56 | + "execution_count": 9, |
| 57 | + "metadata": {}, |
| 58 | + "outputs": [ |
| 59 | + { |
| 60 | + "name": "stdout", |
| 61 | + "output_type": "stream", |
| 62 | + "text": [ |
| 63 | + "[1, 2, 3, 45, 53, 55, 213, 288, 289]\n", |
| 64 | + "[0, 1, 2, 3, 4, 5] [999994, 999995, 999996, 999997, 999998, 999999]\n" |
| 65 | + ] |
| 66 | + } |
| 67 | + ], |
| 68 | + "source": [ |
| 69 | + "def main():\n", |
| 70 | + " A = [55, 45, 3, 289, 213, 1, 288, 53, 2]\n", |
| 71 | + " num_digits = __get_num_digits(A)\n", |
| 72 | + " A = radix(A, num_digits)\n", |
| 73 | + " print(A)\n", |
| 74 | + " \n", |
| 75 | + " B = [i for i in range(1000000)]\n", |
| 76 | + " from random import shuffle\n", |
| 77 | + " shuffle(B)\n", |
| 78 | + " B = radix(B, __get_num_digits(B))\n", |
| 79 | + " print(B[:6], B[-6:])\n", |
| 80 | + "\n", |
| 81 | + "main()" |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "code", |
| 86 | + "execution_count": null, |
| 87 | + "metadata": {}, |
| 88 | + "outputs": [], |
| 89 | + "source": [] |
| 90 | + } |
| 91 | + ], |
| 92 | + "metadata": { |
| 93 | + "kernelspec": { |
| 94 | + "display_name": "Python 3", |
| 95 | + "language": "python", |
| 96 | + "name": "python3" |
| 97 | + }, |
| 98 | + "language_info": { |
| 99 | + "codemirror_mode": { |
| 100 | + "name": "ipython", |
| 101 | + "version": 3 |
| 102 | + }, |
| 103 | + "file_extension": ".py", |
| 104 | + "mimetype": "text/x-python", |
| 105 | + "name": "python", |
| 106 | + "nbconvert_exporter": "python", |
| 107 | + "pygments_lexer": "ipython3", |
| 108 | + "version": "3.7.0" |
| 109 | + } |
| 110 | + }, |
| 111 | + "nbformat": 4, |
| 112 | + "nbformat_minor": 2 |
| 113 | +} |
0 commit comments