Skip to content

Commit

Permalink
Add insertion sort (descending order)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybrettrobertson committed Oct 5, 2020
1 parent 219c4db commit 370cd93
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def insertion_sort(array):\n",
" 'Sorts the array using the Insertion Sort algorithm.'\n",
" 'Sorts the array in ascending order using the Insertion Sort algorithm.'\n",
" \n",
" assert isinstance(array, list) == True, 'ERROR: Input must be a Python list object.'\n",
" \n",
Expand All @@ -47,7 +47,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 2,
"metadata": {},
"outputs": [
{
Expand All @@ -57,8 +57,8 @@
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-28-2ae4af20a3e7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minsertion_sort\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'a'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-27-0a08504b4b81>\u001b[0m in \u001b[0;36minsertion_sort\u001b[0;34m(array)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m'Sorts the array using the Insertion Sort algorithm.'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'ERROR: Input must be a Python list object.'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-2-2ae4af20a3e7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minsertion_sort\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'a'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-1-c92abf87ff64>\u001b[0m in \u001b[0;36minsertion_sort\u001b[0;34m(array)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m'Sorts the array in ascending order using the Insertion Sort algorithm.'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'ERROR: Input must be a Python list object.'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAssertionError\u001b[0m: ERROR: Input must be a Python list object."
]
}
Expand All @@ -69,7 +69,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 3,
"metadata": {},
"outputs": [
{
Expand All @@ -86,7 +86,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 4,
"metadata": {},
"outputs": [
{
Expand All @@ -103,7 +103,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand All @@ -127,12 +127,12 @@
},
{
"cell_type": "code",
"execution_count": 56,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def insertion_sort(array):\n",
" 'Sorts the array using the Insertion Sort algorithm.'\n",
" 'Sorts the array in ascending order using the Insertion Sort algorithm.'\n",
" \n",
" assert isinstance(array, list) == True, 'ERROR: Input must be a Python list object.'\n",
" \n",
Expand All @@ -149,9 +149,16 @@
" return array"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Testing"
]
},
{
"cell_type": "code",
"execution_count": 57,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand All @@ -168,7 +175,96 @@
},
{
"cell_type": "code",
"execution_count": 58,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0]\n"
]
}
],
"source": [
"print(insertion_sort([0]))"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[10, 10, 10]\n"
]
}
],
"source": [
"print(insertion_sort([10, 10, 10]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"def insertion_sort(array):\n",
" 'Sorts the array in descending order using the Insertion Sort algorithm.'\n",
" \n",
" assert isinstance(array, list) == True, 'ERROR: Input must be a Python list object.'\n",
" \n",
" n = len(array)\n",
" \n",
" for i in range(1, n):\n",
" key = array[i]\n",
" j = i - 1\n",
" while j >= 0 and array[j] < key:\n",
" array[j + 1] = array[j]\n",
" j -= 1\n",
" array[j + 1] = key\n",
" \n",
" return array"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Testing"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[400, 400, 100, 10, 6, 5, 0]\n"
]
}
],
"source": [
"print(insertion_sort([10, 5, 400, 6, 100, 0, 400]))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
Expand All @@ -185,7 +281,7 @@
},
{
"cell_type": "code",
"execution_count": 59,
"execution_count": 13,
"metadata": {},
"outputs": [
{
Expand Down
Loading

0 comments on commit 370cd93

Please sign in to comment.