diff --git a/README.md b/README.md index c3b4ebe..c68904c 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/sorting-searching/selection-sort.ipynb b/sorting-searching/selection-sort.ipynb new file mode 100644 index 0000000..b512981 --- /dev/null +++ b/sorting-searching/selection-sort.ipynb @@ -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 +}