From d89d600e5befa0b462e1694ec10637b2d2bd39f8 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 18 Jun 2016 22:12:13 -0400 Subject: [PATCH] Polish selection sort challenge and solution Update constraints, algorithm, code, and test. --- .../selection_sort_challenge.ipynb | 6 ++++-- .../selection_sort_solution.ipynb | 20 +++++++++++++------ .../selection_sort/test_selection_sort.py | 2 +- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/sorting_searching/selection_sort/selection_sort_challenge.ipynb b/sorting_searching/selection_sort/selection_sort_challenge.ipynb index cee2ddd..c45fca6 100644 --- a/sorting_searching/selection_sort/selection_sort_challenge.ipynb +++ b/sorting_searching/selection_sort/selection_sort_challenge.ipynb @@ -35,6 +35,8 @@ "## Constraints\n", "\n", "* Is a naiive solution sufficient (ie not stable, not based on a heap)?\n", + " * Yes\n", + "* Are duplicates allowed?\n", " * Yes" ] }, @@ -134,7 +136,7 @@ " assert_equal(data, [5])\n", "\n", " print('Two or more elements')\n", - " data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n", + " data = [5, 1, 7, 2, 6, -3, 5, 7, -10]\n", " func(data)\n", " assert_equal(data, sorted(data))\n", "\n", @@ -183,7 +185,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.5.0" } }, "nbformat": 4, diff --git a/sorting_searching/selection_sort/selection_sort_solution.ipynb b/sorting_searching/selection_sort/selection_sort_solution.ipynb index 6f01e0b..d4f9e73 100644 --- a/sorting_searching/selection_sort/selection_sort_solution.ipynb +++ b/sorting_searching/selection_sort/selection_sort_solution.ipynb @@ -34,6 +34,8 @@ "## Constraints\n", "\n", "* Is a naiive solution sufficient (ie not stable, not based on a heap)?\n", + " * Yes\n", + "* Are duplicates allowed?\n", " * Yes" ] }, @@ -66,7 +68,7 @@ "\n", "Complexity:\n", "* Time: O(n^2) average, worst, best\n", - "* Space: O(1) iterative, O(n) recursive (unless tail-call elimination is available, then O(1)), generally not stable\n", + "* Space: O(1) iterative, O(m) recursive where m is the recursion depth (unless tail-call elimination is available, then O(1)), generally not stable\n", " * Note: Tail call elimination is not inherently available in Python. See the following [StackOverflow post](http://stackoverflow.com/a/13592002)." ] }, @@ -112,6 +114,9 @@ "outputs": [], "source": [ "def find_min_index(data, start):\n", + " # Raises an exception if: \n", + " # -data is None\n", + " # -start >= len(data) - 1\n", " min_index = start\n", " for i in range(start + 1, len(data)):\n", " if data[i] < data[min_index]:\n", @@ -120,13 +125,16 @@ "\n", "\n", "def swap(data, i, j):\n", - " if (i != j):\n", + " # Raises an exception if: \n", + " # -data is None\n", + " # -i or j are >= len(data)\n", + " if i != j:\n", " data[i], data[j] = data[j], data[i]\n", "\n", "\n", "def selection_sort_recursive(data, start=0):\n", " if data is None:\n", - " return None\n", + " return\n", " if start < len(data) - 1:\n", " swap(data, start, find_min_index(data, start))\n", " selection_sort_recursive(data, start+1)\n", @@ -135,7 +143,7 @@ "def selection_sort_iterative_alt(data):\n", " if data is None or len(data) < 2:\n", " return\n", - " for i in range(0, len(data) - 1):\n", + " for i in range(len(data)-1):\n", " swap(data, i, find_min_index(data, i))" ] }, @@ -186,7 +194,7 @@ " assert_equal(data, [5])\n", "\n", " print('Two or more elements')\n", - " data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n", + " data = [5, 1, 7, 2, 6, -3, 5, 7, -10]\n", " func(data)\n", " assert_equal(data, sorted(data))\n", "\n", @@ -262,7 +270,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.5.0" } }, "nbformat": 4, diff --git a/sorting_searching/selection_sort/test_selection_sort.py b/sorting_searching/selection_sort/test_selection_sort.py index 8f5fe03..17652c7 100644 --- a/sorting_searching/selection_sort/test_selection_sort.py +++ b/sorting_searching/selection_sort/test_selection_sort.py @@ -20,7 +20,7 @@ class TestSelectionSort(object): assert_equal(data, [5]) print('Two or more elements') - data = [5, 1, 7, 2, 6, -3, 5, 7, -1] + data = [5, 1, 7, 2, 6, -3, 5, 7, -10] func(data) assert_equal(data, sorted(data))