mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Added notebook solving the following: Implement selection sort.
This commit is contained in:
parent
f022739318
commit
9d2655548e
|
@ -41,6 +41,10 @@ Continually updated IPython Notebooks containing coding problems and solutions (
|
|||
* [Implement a queue using two stacks](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/stacks-queues/queue-from-stacks.ipynb)
|
||||
* [Sort a stack using another stack as a buffer](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/stacks-queues/sort-stack.ipynb)
|
||||
|
||||
## Sorting and Searching
|
||||
|
||||
* [Implement selection sort](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/sorting-searching/selection-sort.ipynb#)
|
||||
|
||||
## Hacker Rank
|
||||
|
||||
* [Utopian Tree](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/hacker-rank/utopian-tree.ipynb)
|
||||
|
|
147
sorting-searching/selection-sort.ipynb
Normal file
147
sorting-searching/selection-sort.ipynb
Normal file
|
@ -0,0 +1,147 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Problem: Implement selection sort.\n",
|
||||
"\n",
|
||||
"* [Clarifying Questions](#Clarifying-Questions)\n",
|
||||
"* [Test Cases](#Test-Cases)\n",
|
||||
"* [Algorithm](#Algorithm)\n",
|
||||
"* [Code](#Code)\n",
|
||||
"* [Pythonic-Code](#Pythonic-Code)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Clarifying Questions\n",
|
||||
"\n",
|
||||
"* Are we sorting integers?\n",
|
||||
" * Yes\n",
|
||||
"* Can we assume the input is valid integers?\n",
|
||||
" * Yes"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"* Empty input\n",
|
||||
"* One element\n",
|
||||
"* Two or more elements"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Algorithm\n",
|
||||
"\n",
|
||||
"We can do this recursively or iteratively. Iteratively will be more efficient as it doesn't require the extra space overhead with the recursive calls.\n",
|
||||
"\n",
|
||||
"* For each element\n",
|
||||
" * Check every element to the right to find the min\n",
|
||||
" * If min < current element, swap\n",
|
||||
"\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))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def find_min_index(data, start):\n",
|
||||
" min_index = start\n",
|
||||
" for i in xrange(start + 1, len(data)):\n",
|
||||
" if data[i] < data[min_index]:\n",
|
||||
" min_index = i\n",
|
||||
" return min_index\n",
|
||||
"\n",
|
||||
"def swap(data, i, j):\n",
|
||||
" if (i != j):\n",
|
||||
" data[i], data[j] = data[j], data[i]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def selection_sort_recursive(data, start):\n",
|
||||
" if start < len(data) - 1:\n",
|
||||
" swap(data, start, find_min_index(data, start))\n",
|
||||
" selection_sort_recursive(data, start+1)\n",
|
||||
"\n",
|
||||
"print('Empty input')\n",
|
||||
"data = []\n",
|
||||
"selection_sort_recursive(data, 0)\n",
|
||||
"print(data)\n",
|
||||
"print('One element')\n",
|
||||
"data = [5]\n",
|
||||
"selection_sort_recursive(data, 0)\n",
|
||||
"print(data)\n",
|
||||
"print('Two or more elements')\n",
|
||||
"data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
||||
"selection_sort_recursive(data, 0)\n",
|
||||
"print(data)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def selection_sort_iterative(data):\n",
|
||||
" if len(data) == 0 or len(data) == 1:\n",
|
||||
" return\n",
|
||||
" for i in xrange(0, len(data) - 1):\n",
|
||||
" swap(data, i, find_min_index(data, i))"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 2",
|
||||
"language": "python",
|
||||
"name": "python2"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.9"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
Loading…
Reference in New Issue
Block a user