{
 "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://github.com/donnemartin/interactive-coding-challenges).</i></small>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Solution Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Problem: Implement selection sort.\n",
    "\n",
    "* [Constraints](#Constraints)\n",
    "* [Test Cases](#Test-Cases)\n",
    "* [Algorithm](#Algorithm)\n",
    "* [Code](#Code)\n",
    "* [Unit Test](#Unit-Test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Constraints\n",
    "\n",
    "* Is a naiive solution sufficient (ie not stable, not based on a heap)?\n",
    "    * Yes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test Cases\n",
    "\n",
    "* Empty input -> []\n",
    "* One element -> [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": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def find_min_index(data, start):\n",
    "    min_index = start\n",
    "    for i in range(start + 1, len(data)):\n",
    "        if data[i] < data[min_index]:\n",
    "            min_index = i\n",
    "    return min_index\n",
    "\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(data, start=0):\n",
    "    if start < len(data) - 1:\n",
    "        swap(data, start, find_min_index(data, start))\n",
    "        selection_sort(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 range(0, len(data) - 1):\n",
    "        swap(data, i, find_min_index(data, i))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Unit Test\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting test_selection_sort.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile test_selection_sort.py\n",
    "from nose.tools import assert_equal\n",
    "\n",
    "\n",
    "class TestSelectionSort(object):\n",
    "\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",
    "\n",
    "def main():\n",
    "    test = TestSelectionSort()\n",
    "    test.test_selection_sort(selection_sort)\n",
    "    try:\n",
    "        test.test_selection_sort(selection_sort_iterative)\n",
    "    except NameError:\n",
    "        # Alternate solutions are only defined\n",
    "        # in the solutions file\n",
    "        pass\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "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": [
    "%run -i test_selection_sort.py"
   ]
  }
 ],
 "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
}
