2015-05-29 07:10:47 +08:00
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## Problem: Implement quick 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",
|
2015-05-29 17:59:11 +08:00
|
|
|
"* Is a naiive quicksort (ie not in-place) ok?\n",
|
2015-05-29 07:10:47 +08:00
|
|
|
" * 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",
|
2015-05-29 17:59:11 +08:00
|
|
|
"Wikipedia's animation:\n",
|
|
|
|
"![alt text](http://upload.wikimedia.org/wikipedia/commons/6/6a/Sorting_quicksort_anim.gif)\n",
|
|
|
|
"\n",
|
2015-05-29 07:10:47 +08:00
|
|
|
"* Set pivot to the middle element in the data\n",
|
|
|
|
"* For each element:\n",
|
|
|
|
" * If current element is the pivot, continue\n",
|
|
|
|
" * If the element is less than the pivot, add to left array\n",
|
|
|
|
" * Else, add to right array\n",
|
|
|
|
"* Recursively apply quicksort to the left array\n",
|
|
|
|
"* Recursively apply quicksort to the right array\n",
|
|
|
|
"* Merge the left array + pivot + right array\n",
|
|
|
|
"\n",
|
|
|
|
"Complexity:\n",
|
|
|
|
"* Time: O(n log(n)) average, best, O(n^2) worst\n",
|
2015-05-29 18:08:15 +08:00
|
|
|
"* Space: O(n), n extra space, n recursion depth, generally not stable"
|
2015-05-29 07:10:47 +08:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## Code"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {
|
|
|
|
"collapsed": false
|
|
|
|
},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"def quick_sort(data):\n",
|
|
|
|
" if len(data) < 2:\n",
|
|
|
|
" return data\n",
|
|
|
|
" left = []\n",
|
|
|
|
" right = []\n",
|
|
|
|
" pivot_index = len(data) / 2\n",
|
|
|
|
" pivot_value = data[pivot_index]\n",
|
|
|
|
" for i in xrange(0, len(data)):\n",
|
|
|
|
" if i == pivot_index:\n",
|
|
|
|
" continue\n",
|
|
|
|
" if data[i] < pivot_value:\n",
|
|
|
|
" left.append(data[i])\n",
|
|
|
|
" else:\n",
|
|
|
|
" right.append(data[i])\n",
|
|
|
|
" left = quick_sort(left)\n",
|
|
|
|
" right = quick_sort(right)\n",
|
|
|
|
" return left + [pivot_value] + right"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {
|
|
|
|
"collapsed": false
|
|
|
|
},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"print('Empty input')\n",
|
|
|
|
"data = []\n",
|
2015-05-29 18:51:04 +08:00
|
|
|
"print(quick_sort(data))\n",
|
2015-05-29 07:10:47 +08:00
|
|
|
"print('One element')\n",
|
|
|
|
"data = [5]\n",
|
2015-05-29 18:51:04 +08:00
|
|
|
"print(quick_sort(data))\n",
|
2015-05-29 07:10:47 +08:00
|
|
|
"print('Two or more elements')\n",
|
|
|
|
"data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
|
|
|
"print(quick_sort(data))"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## Pythonic-Code\n",
|
|
|
|
"\n",
|
|
|
|
"From http://stackoverflow.com/questions/18262306/quick-sort-with-python"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {
|
|
|
|
"collapsed": true
|
|
|
|
},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"def quick_sort(arr): \n",
|
|
|
|
" if len(arr) <= 1:\n",
|
|
|
|
" return arr\n",
|
|
|
|
" else:\n",
|
|
|
|
" return qsort([x for x in arr[1:] if x<arr[0]]) + [arr[0]] + qsort([x for x in arr[1:] if x>=arr[0]])"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"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
|
|
|
|
}
|