mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
149 lines
3.5 KiB
Plaintext
149 lines
3.5 KiB
Plaintext
{
|
|
"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 you looking for a generic naiive selection sort (ie not stable)?\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",
|
|
"Wikipedia's animation:\n",
|
|
"![alt text](http://upload.wikimedia.org/wikipedia/commons/9/94/Selection-Sort-Animation.gif)\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)), generally not stable"
|
|
]
|
|
},
|
|
{
|
|
"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
|
|
}
|