{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## algorithm" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "identity = lambda i: i" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def heapify(heap, key=identity):\n", " n = len(heap)\n", " for i in reversed(range(n // 2)):\n", " sift_down(heap, i, n, key=key)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def heap_push(heap, value, key=identity):\n", " i = len(heap)\n", " heap.append(value)\n", " sift_up(heap, i, key=key)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def heap_pop(heap, key=identity):\n", " item, heap[0] = heap[0], heap[-1]\n", " del heap[-1]\n", " heap and sift_down(heap, 0, len(heap), key=key)\n", " return item" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def sift_down(heap, i, n, key=identity):\n", " # item to be sifted\n", " item = heap[i]\n", " item_key = key(item)\n", "\n", " while True:\n", " smallest, k = item_key, i\n", " j = 2 * i + 1\n", "\n", " # left child\n", " if j < n:\n", " left_key = key(heap[j])\n", " if left_key < smallest:\n", " smallest, k = left_key, j\n", "\n", " # right child\n", " if j + 1 < n and key(heap[j + 1]) < smallest:\n", " k = j + 1\n", "\n", " # swap or finish\n", " if k != i:\n", " heap[i] = heap[k]\n", " i = k\n", " else:\n", " break\n", "\n", " heap[i] = item" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def sift_up(heap, i, key=identity):\n", " # item to be sifted\n", " item = heap[i]\n", " item_key = key(item)\n", "\n", " while i:\n", " j = i // 2\n", "\n", " if item_key < key(heap[j]):\n", " heap[i] = heap[j]\n", " i = j\n", " else:\n", " break\n", "\n", " heap[i] = item" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def heap_sort(data, key=identity):\n", " heapify(data, key=key)\n", " \n", " for i in reversed(range(len(data))):\n", " data[0], data[i] = data[i], data[0]\n", " sift_down(data, 0, i, key=key)\n", " \n", " data.reverse()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## run" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### priority queues" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data1, data2 = [9, 7, 5, 3, 1], [8, 6, 4, 2, 0]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, " ] } ], "source": [ "heapify(data1)\n", "heapify(data2)\n", "\n", "while data1 or data2:\n", " data2 and heap_push(data1, heap_pop(data2))\n", " print(heap_pop(data1), end=', ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### string array" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data = ['hello', 'bye', 'good-bye', 'hi', 'hey!', 'good night']" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['bye', 'good night', 'good-bye', 'hello', 'hey!', 'hi']" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "heap_sort(data)\n", "data" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['hi', 'bye', 'hey!', 'hello', 'good-bye', 'good night']" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "heap_sort(data, key=len)\n", "data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### string array treated as hexadecimal values" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data = ['ff', '100', 'ac', '5', '99cc', '393', '000152']" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['000152', '100', '393', '5', '99cc', 'ac', 'ff']" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "heap_sort(data)\n", "data" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['5', 'ac', 'ff', '100', '000152', '393', '99cc']" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "heap_sort(data, key=lambda i: int(i, 16))\n", "data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.0" } }, "nbformat": 4, "nbformat_minor": 2 }