mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Polish selection sort solution
This commit is contained in:
parent
577ada7896
commit
d8ac0b8fc6
|
@ -86,14 +86,11 @@
|
|||
"outputs": [],
|
||||
"source": [
|
||||
"def selection_sort(data):\n",
|
||||
" if data is None:\n",
|
||||
" if data is None or len(data) < 2:\n",
|
||||
" return\n",
|
||||
" size = len(data)\n",
|
||||
" for i in range(size):\n",
|
||||
" if i == size - 1:\n",
|
||||
" break\n",
|
||||
" for i in range(len(data)-1):\n",
|
||||
" min_index = i\n",
|
||||
" for j in range(i + 1, size):\n",
|
||||
" for j in range(i + 1, len(data)):\n",
|
||||
" if data[j] < data[min_index]:\n",
|
||||
" min_index = j\n",
|
||||
" data[i], data[min_index] = data[min_index], data[i]"
|
||||
|
@ -136,9 +133,7 @@
|
|||
"\n",
|
||||
"\n",
|
||||
"def selection_sort_iterative_alt(data):\n",
|
||||
" if data is None:\n",
|
||||
" return None\n",
|
||||
" if len(data) == 0 or len(data) == 1:\n",
|
||||
" if data is None or len(data) < 2:\n",
|
||||
" return\n",
|
||||
" for i in range(0, len(data) - 1):\n",
|
||||
" swap(data, i, find_min_index(data, i))"
|
||||
|
|
Loading…
Reference in New Issue
Block a user