mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Added notebook solving the following: Implement quick sort.
This commit is contained in:
parent
83be0902d1
commit
dfd462b5db
|
@ -47,6 +47,7 @@ Most solutions are in Python.
|
|||
|
||||
* [Implement selection sort](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/sorting-searching/selection-sort.ipynb#)
|
||||
* [Implement insertion sort](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/sorting-searching/insertion-sort.ipynb#)
|
||||
* [Implement quick sort](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/sorting-searching/quick-sort.ipynb#)
|
||||
|
||||
## Hacker Rank
|
||||
|
||||
|
|
158
sorting-searching/quick-sort.ipynb
Normal file
158
sorting-searching/quick-sort.ipynb
Normal file
|
@ -0,0 +1,158 @@
|
|||
{
|
||||
"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",
|
||||
"* Are you looking for a naiive quicksort (ie not in-place)?\n",
|
||||
" * 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",
|
||||
"* 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",
|
||||
"* Space: O(n), n extra space, n recursion depth"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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",
|
||||
"#quick_sort(data)\n",
|
||||
"print(data)\n",
|
||||
"print('One element')\n",
|
||||
"data = [5]\n",
|
||||
"#quick_sort(data)\n",
|
||||
"print(data)\n",
|
||||
"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
|
||||
}
|
Loading…
Reference in New Issue
Block a user