mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Update selection sort challenge algorithm discussion (#139)
This commit is contained in:
parent
c69769757d
commit
f71936199b
|
@ -72,8 +72,17 @@
|
||||||
"\n",
|
"\n",
|
||||||
"Complexity:\n",
|
"Complexity:\n",
|
||||||
"* Time: O(n^2) average, worst, best\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",
|
"* 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)."
|
" * 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",
|
" self._swap(data, i, self._find_min_index(data, i))\n",
|
||||||
" return data\n",
|
" return data\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def sort_recursive(self, data, start=0):\n",
|
" def sort_recursive(self, data):\n",
|
||||||
" if data is None:\n",
|
" if data is None:\n",
|
||||||
" raise TypeError('data cannot be None')\n",
|
" raise TypeError('data cannot be None')\n",
|
||||||
" if len(data) < 2:\n",
|
" if len(data) < 2:\n",
|
||||||
" return data\n",
|
" return data\n",
|
||||||
" return self._sort_recursive(data, start)\n",
|
" return self._sort_recursive(data, start=0)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def _sort_recursive(self, data, start):\n",
|
" def _sort_recursive(self, data, start):\n",
|
||||||
" if data is None:\n",
|
" if data is None:\n",
|
||||||
|
|
Loading…
Reference in New Issue
Block a user