interactive-coding-challenges/sorting-searching/selection-sort.ipynb

206 lines
5.2 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes).</i></small>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Implement selection sort.\n",
"\n",
"* [Constraints and Assumptions](#Constraints-and-Assumptions)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Constraints and Assumptions\n",
"\n",
"*Problem statements are often intentionally ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
"\n",
"* Are you looking for a naiive solution (ie not stable, not based on a heap)?\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",
2015-05-29 18:08:15 +08:00
"* 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": 1,
"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": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def selection_sort_recursive(data, start=0):\n",
" if start < len(data) - 1:\n",
" swap(data, start, find_min_index(data, start))\n",
" selection_sort_recursive(data, start+1)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test\n",
"\n",
"*It is important to identify and run through general and edge cases from the [Test Cases](#Test-Cases) section by hand. You generally will not be asked to write a unit test like what is shown below.*"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Empty input\n",
"One element\n",
"Two or more elements\n",
"Success: test_selection_sort\n",
"\n",
"Empty input\n",
"One element\n",
"Two or more elements\n",
"Success: test_selection_sort\n",
"\n"
]
}
],
"source": [
"from nose.tools import assert_equal\n",
"\n",
"class Test(object):\n",
" def test_selection_sort(self, func):\n",
" print('Empty input')\n",
" data = []\n",
" func(data)\n",
" assert_equal(data, [])\n",
"\n",
" print('One element')\n",
" data = [5]\n",
" func(data)\n",
" assert_equal(data, [5])\n",
"\n",
" print('Two or more elements')\n",
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
" func(data)\n",
" assert_equal(data, sorted(data))\n",
" \n",
" print('Success: test_selection_sort\\n')\n",
"\n",
"if __name__ == '__main__':\n",
" test = Test()\n",
" test.test_selection_sort(selection_sort_recursive)\n",
" test.test_selection_sort(selection_sort_iterative)"
]
}
],
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}