-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added initial versions of countingsort and radixsort
- Loading branch information
1 parent
f65fddc
commit 72cf9d1
Showing
2 changed files
with
721 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,316 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Copyright 2021 Jens Liebehenschel, Frankfurt University of Applied Sciences, FB2, Computer Science\n", | ||
"### No liability or warranty; only for educational and non-commercial purposes\n", | ||
"### See some basic hints for working with Jupyter notebooks in README.md\n", | ||
"## Sorting an array using countingsort" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def countingsort(A):\n", | ||
" # create temporary array C for counting\n", | ||
" C=[0 for x in range(max(A)+1)]\n", | ||
" print_array_C(C)\n", | ||
"\n", | ||
" # count number of elements = i in C[i]\n", | ||
" for j in range(len(A)):\n", | ||
" C[A[j]] = C[A[j]] + 1\n", | ||
" print_array_C(C)\n", | ||
" \n", | ||
" # calculate number of elements <= i in C[i]\n", | ||
" for i in range(1,len(C)):\n", | ||
" C[i] = C[i] + C[i-1]\n", | ||
" print_array_C(C)\n", | ||
"\n", | ||
" # create (empty) output array B\n", | ||
" B=[-1 for x in range(len(A))]\n", | ||
"\n", | ||
" # create sorted output\n", | ||
" for j in range(len(A)-1,-1,-1):\n", | ||
" B[C[A[j]]-1] = A[j]\n", | ||
" C[A[j]] = C[A[j]] - 1\n", | ||
" print_array_C(C)\n", | ||
" \n", | ||
" return B" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# checks whether array is sorted correctly, this also works with identical keys in the array\n", | ||
"def check_sorting(a):\n", | ||
" return all(a[i] <= a[i+1] for i in range(len(a)-1))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def print_array_C(C):\n", | ||
" if output_temp_array:\n", | ||
" print (\"C:\",C)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Constants" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# The size of elements in tests can be changed here\n", | ||
"ARRAY_SIZE = 10\n", | ||
"# Some output text\n", | ||
"TEXT_BEFORE_SORTING = \"Before sorting:\"\n", | ||
"TEXT_AFTER_SORTING = \"After sorting:\"\n", | ||
"TEXT_CHECK_SORTING = \"Sorting correct? -\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Configuration of output " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# change output settings here\n", | ||
"output_temp_array = False\n", | ||
"output_temp_array = True" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Generate test data and test algorithm" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Before sorting: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", | ||
"C: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n", | ||
"C: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n", | ||
"C: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", | ||
"C: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", | ||
"After sorting: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", | ||
"Sorting correct? - True\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# test sorting sorted array\n", | ||
"sorted_array = list(range(ARRAY_SIZE))\n", | ||
"print(TEXT_BEFORE_SORTING, sorted_array)\n", | ||
"a = countingsort(sorted_array)\n", | ||
"print(TEXT_AFTER_SORTING, a)\n", | ||
"print(TEXT_CHECK_SORTING, check_sorting(a))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Before sorting: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]\n", | ||
"C: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n", | ||
"C: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n", | ||
"C: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", | ||
"C: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", | ||
"After sorting: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", | ||
"Sorting correct? - True\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# test sorting reversly sorted array\n", | ||
"reverse_sorted_array = list(range(ARRAY_SIZE-1, -1, -1))\n", | ||
"print(TEXT_BEFORE_SORTING, reverse_sorted_array)\n", | ||
"a = countingsort(reverse_sorted_array)\n", | ||
"print(TEXT_AFTER_SORTING, a)\n", | ||
"print(TEXT_CHECK_SORTING, check_sorting(a))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": { | ||
"scrolled": true | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Before sorting: [2, 8, 4, 9, 1, 6, 7, 3, 0, 5]\n", | ||
"C: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n", | ||
"C: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n", | ||
"C: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", | ||
"C: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", | ||
"After sorting: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", | ||
"Sorting correct? - True\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# test sorting random array\n", | ||
"# use numpy for generating a permutation\n", | ||
"import numpy\n", | ||
"# initialialize random number generator to obtain reproducable results\n", | ||
"# other values might be used or even no initialization done here\n", | ||
"numpy.random.seed(0)\n", | ||
"# now generate random data and test sorting\n", | ||
"array = list(numpy.random.permutation(ARRAY_SIZE))\n", | ||
"print(TEXT_BEFORE_SORTING, array)\n", | ||
"a = countingsort(array)\n", | ||
"print(TEXT_AFTER_SORTING, a)\n", | ||
"print(TEXT_CHECK_SORTING, check_sorting(a))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"output_temp_array_last_state = output_temp_array\n", | ||
"output_temp_array = False" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Before sorting: [36, 87, 70, 88, 140, 58, 193, 39, 87, 174, 88, 81, 165, 25, 77, 72, 9, 148, 115, 197]\n", | ||
"After sorting: [9, 25, 36, 39, 58, 70, 72, 77, 81, 87, 87, 88, 88, 115, 140, 148, 165, 174, 193, 197]\n", | ||
"Sorting correct? - True\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# now generate random data and test sorting\n", | ||
"MAX_VALUE = 200\n", | ||
"ARRAY_SIZE = 20\n", | ||
"array = [numpy.random.randint(MAX_VALUE) for x in range(ARRAY_SIZE)]\n", | ||
"print(TEXT_BEFORE_SORTING, array)\n", | ||
"a = countingsort(array)\n", | ||
"print(TEXT_AFTER_SORTING, a)\n", | ||
"print(TEXT_CHECK_SORTING, check_sorting(a))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 11, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"output_temp_array = output_temp_array_last_state" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### your tests here ..." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 12, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Before sorting: [1, 3, 2]\n", | ||
"C: [0, 0, 0, 0]\n", | ||
"C: [0, 1, 1, 1]\n", | ||
"C: [0, 1, 2, 3]\n", | ||
"C: [0, 0, 1, 2]\n", | ||
"After sorting: [1, 2, 3]\n", | ||
"Sorting correct? - True\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"array = [1,3,2]\n", | ||
"print(TEXT_BEFORE_SORTING, array)\n", | ||
"a = countingsort(array)\n", | ||
"print(TEXT_AFTER_SORTING, a)\n", | ||
"print(TEXT_CHECK_SORTING, check_sorting(a))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 13, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# ... and here ..." | ||
] | ||
} | ||
], | ||
"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.8.5" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
Oops, something went wrong.