Last active
February 10, 2017 19:09
-
-
Save gjhernandezp/0c1462a5193ce8e619a190f6bff2960a to your computer and use it in GitHub Desktop.
quicksort from
This file contains 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": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The Quick Sort with a different two finger partition alorithn from Problem Solving with Algorithms and Data Structures see at http://interactivepython.org/ http://interactivepython.org/runestone/static/pythonds/SortSearch/TheQuickSort.html" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def quickSort(alist):\n", | |
" quickSortHelper(alist,0,len(alist)-1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def quickSortHelper(alist,first,last):\n", | |
" if first<last:\n", | |
"\n", | |
" splitpoint = partition(alist,first,last)\n", | |
"\n", | |
" quickSortHelper(alist,first,splitpoint-1)\n", | |
" quickSortHelper(alist,splitpoint+1,last)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def partition(alist,first,last):\n", | |
" pivotvalue = alist[first]\n", | |
"\n", | |
" leftmark = first+1\n", | |
" rightmark = last\n", | |
"\n", | |
" done = False\n", | |
" while not done:\n", | |
"\n", | |
" while leftmark <= rightmark and alist[leftmark] <= pivotvalue:\n", | |
" leftmark = leftmark + 1\n", | |
"\n", | |
" while alist[rightmark] >= pivotvalue and rightmark >= leftmark:\n", | |
" rightmark = rightmark -1\n", | |
"\n", | |
" if rightmark < leftmark:\n", | |
" done = True\n", | |
" else:\n", | |
" temp = alist[leftmark]\n", | |
" alist[leftmark] = alist[rightmark]\n", | |
" alist[rightmark] = temp\n", | |
"\n", | |
" temp = alist[first]\n", | |
" alist[first] = alist[rightmark]\n", | |
" alist[rightmark] = temp\n", | |
"\n", | |
"\n", | |
" return rightmark" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[17, 20, 26, 31, 44, 54, 55, 77, 93]\n", | |
"[11, 17, 20, 26, 31, 44, 54, 55, 73, 77, 93]\n" | |
] | |
} | |
], | |
"source": [ | |
"alist = [54,26,93,17,77,31,44,55,20]\n", | |
"quickSort(alist)\n", | |
"print(alist)\n", | |
"alist = [73,54,26,93,17,77,31,44,55,20,11]\n", | |
"quickSort(alist)\n", | |
"print(alist)" | |
] | |
} | |
], | |
"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.11" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment