Skip to content

Instantly share code, notes, and snippets.

@mariogeiger
Last active March 10, 2017 18:56
Show Gist options
  • Save mariogeiger/6019536 to your computer and use it in GitHub Desktop.
Save mariogeiger/6019536 to your computer and use it in GitHub Desktop.
quicksort on ipython notebook
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "quicksort"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"def qsort(m):\n",
" l = len(m)\n",
" if l <= 1:\n",
" return\n",
" p = m[0] # Pour faire simple on prend le premier \u00e9l\u00e9ment comme pivot\n",
" m1 = []\n",
" m2 = []\n",
" for i in m[1:]:\n",
" if i < p:\n",
" m1.append(i)\n",
" else:\n",
" m2.append(i)\n",
" qsort(m1)\n",
" qsort(m2)\n",
" m.clear()\n",
" m += m1 + [p] + m2"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from random import *\n",
"def randlist():\n",
" a = list(range(1,10))\n",
" shuffle(a)\n",
" return a"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = randlist()\n",
"print(x)\n",
"qsort(x)\n",
"print(x)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[9, 5, 8, 1, 4, 2, 3, 6, 7]\n",
"[1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
]
}
],
"prompt_number": 22
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment