Add files via upload
This commit is contained in:
109
Sorting Algorithms/Bubble Sort.ipynb
Normal file
109
Sorting Algorithms/Bubble Sort.ipynb
Normal file
@@ -0,0 +1,109 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Bubble Sort\n",
|
||||
"© 2021, Joe James"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def bubble_sort1(A):\n",
|
||||
" for i in range (0, len(A) - 1):\n",
|
||||
" for j in range (0, len(A) - i - 1):\n",
|
||||
" if A[j] > A[j+1]:\n",
|
||||
" A[j], A[j+1] = A[j+1], A[j]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# optimized to exit if no swaps occur \n",
|
||||
"def bubble_sort2(A):\n",
|
||||
" for i in range (0, len(A) - 1):\n",
|
||||
" done = True\n",
|
||||
" for j in range (0, len(A) - i - 1):\n",
|
||||
" if A[j] > A[j+1]:\n",
|
||||
" A[j], A[j+1] = A[j+1], A[j]\n",
|
||||
" done = False\n",
|
||||
" if done:\n",
|
||||
" return"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Test Function\n",
|
||||
"Set a result flag. The j loop is used to perform 1000 test iterations. The next two lines create and shuffle list A of 100 integers. Then A is passed to our sorting function. The sorted result is compared to a sort using Python's sorted function. After 1000 iterations of the test, the result is printed."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Success\n",
|
||||
"Success\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import random\n",
|
||||
"\n",
|
||||
"def test(func):\n",
|
||||
" result = 'Success'\n",
|
||||
" for j in range(1000):\n",
|
||||
" A = [k for k in range(100)]\n",
|
||||
" random.shuffle(A)\n",
|
||||
" func(A)\n",
|
||||
" if A != sorted(A):\n",
|
||||
" result = 'Failed'\n",
|
||||
" print(result)\n",
|
||||
"test(bubble_sort1)\n",
|
||||
"test(bubble_sort2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.7.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Reference in New Issue
Block a user