Created
December 3, 2016 15:07
-
-
Save alisianoi/45a0c60ba6dc98bbe7658b063a51dc5f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import random" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"def quicksort(xs):\n", | |
" def sort(xs, l, r):\n", | |
" if (l == r):\n", | |
" return\n", | |
" \n", | |
" i, j = l, r\n", | |
" pivot = xs[(i + j) / 2]\n", | |
" \n", | |
" while (i <= j):\n", | |
" while (xs[i] < pivot):\n", | |
" i += 1\n", | |
" while (pivot < xs[j]):\n", | |
" j -= 1\n", | |
" \n", | |
" if (i <= j):\n", | |
" xs[i], xs[j] = xs[j], xs[i]\n", | |
" \n", | |
" i += 1\n", | |
" j -= 1\n", | |
" \n", | |
" i, j = 0, len(xs) - 1\n", | |
" sort(xs, i, j)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def create_list(sz=10, lo=-1024, hi=1024, seed=42):\n", | |
" random.seed(seed)\n", | |
" \n", | |
" xs = [random.randint(lo, hi) for i in range(sz)]\n", | |
" \n", | |
" return xs" | |
] | |
} | |
], | |
"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.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment