mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Polish selection sort challenge and solution
Update constraints, algorithm, code, and test.
This commit is contained in:
parent
ea1bb12a13
commit
d89d600e5b
|
@ -35,6 +35,8 @@
|
|||
"## Constraints\n",
|
||||
"\n",
|
||||
"* Is a naiive solution sufficient (ie not stable, not based on a heap)?\n",
|
||||
" * Yes\n",
|
||||
"* Are duplicates allowed?\n",
|
||||
" * Yes"
|
||||
]
|
||||
},
|
||||
|
@ -134,7 +136,7 @@
|
|||
" assert_equal(data, [5])\n",
|
||||
"\n",
|
||||
" print('Two or more elements')\n",
|
||||
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
||||
" data = [5, 1, 7, 2, 6, -3, 5, 7, -10]\n",
|
||||
" func(data)\n",
|
||||
" assert_equal(data, sorted(data))\n",
|
||||
"\n",
|
||||
|
@ -183,7 +185,7 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.4.3"
|
||||
"version": "3.5.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
|
@ -34,6 +34,8 @@
|
|||
"## Constraints\n",
|
||||
"\n",
|
||||
"* Is a naiive solution sufficient (ie not stable, not based on a heap)?\n",
|
||||
" * Yes\n",
|
||||
"* Are duplicates allowed?\n",
|
||||
" * Yes"
|
||||
]
|
||||
},
|
||||
|
@ -66,7 +68,7 @@
|
|||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(n^2) average, worst, best\n",
|
||||
"* Space: O(1) iterative, O(n) recursive (unless tail-call elimination is available, then O(1)), generally not stable\n",
|
||||
"* Space: O(1) iterative, O(m) recursive where m is the recursion depth (unless tail-call elimination is available, then O(1)), generally not stable\n",
|
||||
" * Note: Tail call elimination is not inherently available in Python. See the following [StackOverflow post](http://stackoverflow.com/a/13592002)."
|
||||
]
|
||||
},
|
||||
|
@ -112,6 +114,9 @@
|
|||
"outputs": [],
|
||||
"source": [
|
||||
"def find_min_index(data, start):\n",
|
||||
" # Raises an exception if: \n",
|
||||
" # -data is None\n",
|
||||
" # -start >= len(data) - 1\n",
|
||||
" min_index = start\n",
|
||||
" for i in range(start + 1, len(data)):\n",
|
||||
" if data[i] < data[min_index]:\n",
|
||||
|
@ -120,13 +125,16 @@
|
|||
"\n",
|
||||
"\n",
|
||||
"def swap(data, i, j):\n",
|
||||
" if (i != j):\n",
|
||||
" # Raises an exception if: \n",
|
||||
" # -data is None\n",
|
||||
" # -i or j are >= len(data)\n",
|
||||
" if i != j:\n",
|
||||
" data[i], data[j] = data[j], data[i]\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def selection_sort_recursive(data, start=0):\n",
|
||||
" if data is None:\n",
|
||||
" return None\n",
|
||||
" return\n",
|
||||
" if start < len(data) - 1:\n",
|
||||
" swap(data, start, find_min_index(data, start))\n",
|
||||
" selection_sort_recursive(data, start+1)\n",
|
||||
|
@ -135,7 +143,7 @@
|
|||
"def selection_sort_iterative_alt(data):\n",
|
||||
" if data is None or len(data) < 2:\n",
|
||||
" return\n",
|
||||
" for i in range(0, len(data) - 1):\n",
|
||||
" for i in range(len(data)-1):\n",
|
||||
" swap(data, i, find_min_index(data, i))"
|
||||
]
|
||||
},
|
||||
|
@ -186,7 +194,7 @@
|
|||
" assert_equal(data, [5])\n",
|
||||
"\n",
|
||||
" print('Two or more elements')\n",
|
||||
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
||||
" data = [5, 1, 7, 2, 6, -3, 5, 7, -10]\n",
|
||||
" func(data)\n",
|
||||
" assert_equal(data, sorted(data))\n",
|
||||
"\n",
|
||||
|
@ -262,7 +270,7 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.4.3"
|
||||
"version": "3.5.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
|
@ -20,7 +20,7 @@ class TestSelectionSort(object):
|
|||
assert_equal(data, [5])
|
||||
|
||||
print('Two or more elements')
|
||||
data = [5, 1, 7, 2, 6, -3, 5, 7, -1]
|
||||
data = [5, 1, 7, 2, 6, -3, 5, 7, -10]
|
||||
func(data)
|
||||
assert_equal(data, sorted(data))
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user