From f71936199b10d30fbdcddaf9ff8843b1432a25ae Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 21 Jan 2017 19:35:29 -0500 Subject: [PATCH] Update selection sort challenge algorithm discussion (#139) --- .../selection_sort_solution.ipynb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/sorting_searching/selection_sort/selection_sort_solution.ipynb b/sorting_searching/selection_sort/selection_sort_solution.ipynb index 8fbd7ae..ebfd029 100644 --- a/sorting_searching/selection_sort/selection_sort_solution.ipynb +++ b/sorting_searching/selection_sort/selection_sort_solution.ipynb @@ -72,8 +72,17 @@ "\n", "Complexity:\n", "* Time: O(n^2) average, worst, best\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)." + "* Space: O(1) iterative, O(m) recursive where m is the recursion depth (unless tail-call elimination is available, then O(1))\n", + " * Note: Tail call elimination is not inherently available in Python, see the following [StackOverflow post](http://stackoverflow.com/a/13592002).\n", + "\n", + "Misc: \n", + "\n", + "* In-place\n", + "* Most implementations are not stable, due to swapping of values\n", + "\n", + "Selection sort might be a good option if moving elements is more expensive than comparing them, as it requires at most n-1 swaps.\n", + "\n", + "The finding of a minimum element can be done with a min heap, which would change the worst-case run time to O(n log(n)) and increase the space to O(n). This is called a heap sort." ] }, { @@ -116,12 +125,12 @@ " self._swap(data, i, self._find_min_index(data, i))\n", " return data\n", "\n", - " def sort_recursive(self, data, start=0):\n", + " def sort_recursive(self, data):\n", " if data is None:\n", " raise TypeError('data cannot be None')\n", " if len(data) < 2:\n", " return data\n", - " return self._sort_recursive(data, start)\n", + " return self._sort_recursive(data, start=0)\n", "\n", " def _sort_recursive(self, data, start):\n", " if data is None:\n",