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
|
||||
}
|
||||
121
Sorting Algorithms/Insertion Sort.ipynb
Normal file
121
Sorting Algorithms/Insertion Sort.ipynb
Normal file
@@ -0,0 +1,121 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Insertion Sort\n",
|
||||
"© 2021, Joe James"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### using for loop"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def insertion_sort1(A):\n",
|
||||
" for i in range(1, len(A)):\n",
|
||||
" for j in range(i-1, -1, -1):\n",
|
||||
" if A[j] > A[j+1]:\n",
|
||||
" A[j], A[j+1] = A[j+1], A[j]\n",
|
||||
" else:\n",
|
||||
" break"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### using while loop "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def insertion_sort2(A):\n",
|
||||
" for i in range(1, len(A)):\n",
|
||||
" j = i-1\n",
|
||||
" while A[j] > A[j+1] and j >= 0:\n",
|
||||
" A[j], A[j+1] = A[j+1], A[j]\n",
|
||||
" j -= 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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": 23,
|
||||
"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(insertion_sort1)\n",
|
||||
"test(insertion_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
|
||||
}
|
||||
110
Sorting Algorithms/Merge Sort.ipynb
Normal file
110
Sorting Algorithms/Merge Sort.ipynb
Normal file
@@ -0,0 +1,110 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Merge Sort\n",
|
||||
"© 2021, Joe James"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"def merge_sort(A):\n",
|
||||
" split(A, 0, len(A)-1)\n",
|
||||
" \n",
|
||||
"def split(A, first, last):\n",
|
||||
" if first < last:\n",
|
||||
" middle = (first + last)//2\n",
|
||||
" split(A, first, middle)\n",
|
||||
" split(A, middle+1, last)\n",
|
||||
" merge(A, first, middle, last)\n",
|
||||
" \n",
|
||||
"def merge(A, first, middle, last):\n",
|
||||
" L = A[first:middle+1]\n",
|
||||
" R = A[middle+1:last+1]\n",
|
||||
" L.append(sys.maxsize)\n",
|
||||
" R.append(sys.maxsize)\n",
|
||||
" l = r = 0\n",
|
||||
" \n",
|
||||
" for a in range (first, last+1):\n",
|
||||
" if L[l] <= R[r]:\n",
|
||||
" A[a] = L[l]\n",
|
||||
" l += 1\n",
|
||||
" else:\n",
|
||||
" A[a] = R[r]\n",
|
||||
" r += 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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": 22,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"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(merge_sort)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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
|
||||
}
|
||||
109
Sorting Algorithms/Quick Sort.ipynb
Normal file
109
Sorting Algorithms/Quick Sort.ipynb
Normal file
@@ -0,0 +1,109 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Quick Sort\n",
|
||||
"© 2021, Joe James"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 84,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import random\n",
|
||||
"\n",
|
||||
"def quick_sort(A):\n",
|
||||
" quick(A, 0, len(A)-1)\n",
|
||||
"\n",
|
||||
"def quick(A, low, hi):\n",
|
||||
" if low < hi:\n",
|
||||
" p = partition(A, low, hi)\n",
|
||||
" quick(A, low, p-1)\n",
|
||||
" quick(A, p+1, hi)\n",
|
||||
"\n",
|
||||
"def get_pivot(A, low, hi):\n",
|
||||
" return random.randrange(low, hi+1)\n",
|
||||
" \n",
|
||||
"def partition(A, low, hi):\n",
|
||||
" pivotIndex = get_pivot(A, low, hi)\n",
|
||||
" pivotValue = A[pivotIndex]\n",
|
||||
" A[pivotIndex], A[low] = A[low], A[pivotIndex]\n",
|
||||
" border = low\n",
|
||||
"\n",
|
||||
" for i in range(low, hi+1):\n",
|
||||
" if A[i] < pivotValue:\n",
|
||||
" border += 1\n",
|
||||
" A[i], A[border] = A[border], A[i]\n",
|
||||
" A[low], A[border] = A[border], A[low]\n",
|
||||
"\n",
|
||||
" return (border)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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": 85,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Success\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"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' + str(j)\n",
|
||||
" print(result)\n",
|
||||
"test(quick_sort)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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
|
||||
}
|
||||
110
Sorting Algorithms/Radix Sort.ipynb
Normal file
110
Sorting Algorithms/Radix Sort.ipynb
Normal file
@@ -0,0 +1,110 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Radix Sort\n",
|
||||
"© 2021, Joe James"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# flatten into a 1D List\n",
|
||||
"from functools import reduce\n",
|
||||
"def flatten(A):\n",
|
||||
" return reduce(lambda x, y: x + y, A)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 34,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def radix_sort(A):\n",
|
||||
" # get number of digits in largest item\n",
|
||||
" num_digits = len(str(max(A)))\n",
|
||||
" for digit in range(0, num_digits):\n",
|
||||
" B = [[] for i in range(10)]\n",
|
||||
" e = 10 ** digit\n",
|
||||
" for item in A:\n",
|
||||
" # num is the bucket number that the item will be put into\n",
|
||||
" num = item // e % 10\n",
|
||||
" B[num].append(item)\n",
|
||||
" A = flatten(B)\n",
|
||||
" return A"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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. \n",
|
||||
"Note that Radix is not in-place, so we need to assign the returned list to A, A = func(A)\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 35,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"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",
|
||||
" A = func(A)\n",
|
||||
" if A != sorted(A):\n",
|
||||
" result = 'Failed'\n",
|
||||
" print(result)\n",
|
||||
"test(radix_sort)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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
|
||||
}
|
||||
92
Sorting Algorithms/Selection Sort.ipynb
Normal file
92
Sorting Algorithms/Selection Sort.ipynb
Normal file
@@ -0,0 +1,92 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Selection Sort\n",
|
||||
"© 2021, Joe James"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def selection_sort(A):\n",
|
||||
" for i in range (0, len(A) - 1):\n",
|
||||
" minIndex = i\n",
|
||||
" for j in range (i+1, len(A)):\n",
|
||||
" if A[j] < A[minIndex]:\n",
|
||||
" minIndex = j\n",
|
||||
" if minIndex != i:\n",
|
||||
" A[i], A[minIndex] = A[minIndex], A[i]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"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(selection_sort)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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