Soring Algorithms

Bucket sort done
main
lomna 1 year ago
parent 8ccd4d0ae8
commit aa90c42a7c

@ -3,7 +3,7 @@
** In place vs out place sorting algorithm
If the space complexity of a sorting algorithm is $\theta (1)$, then the algorithm is called in place sorting, else the algorithm is called out place sorting.
** Bubble sort
* Bubble sort
Simplest sorting algorithm, easy to implement so it is useful when number of elements to sort is small. It is an in place sorting algorithm. We will compare pairs of elements from array and swap them to be in correct order. Suppose input has n elements.
+ For first pass of the array, we will do *n-1* comparisions between pairs, so 1st and 2nd element; then 2nd and 3rd element; then 3rd and 4th element; till comparision between (n-1)th and nth element, swapping positions according to the size. /A single pass will put a single element at the end of the list at it's correct position./
+ For second pass of the array, we will do *n-2* comparisions because the last element is already in it's place after the first pass.

@ -388,3 +388,87 @@ For the following example, we will use the bubble sort since it is the easiest t
radix sort is paired with counting sort.
** Bucket sort
Counting sort only works for non-negative integers. Bucket sort is a generalization of counting sort. If we know the range of the elements in the array, we can sort them using bucket sort. In bucket sort, we distribute the elements into buckets (collections of elements). Each bucket will hold elements of different ranges. Then, we can either sort elements in the buckets using some other sorting algorithm or by using bucket sort algorithm recursively.
\\
Bucket sort works as follows:
1. Set up empty buckets
2. *Scatter* the elements into buckets based on different ranges.
3. *Sort* elements in non-empty buckets.
4. *Gather* the elements from buckets and place in orignal array.
| [[./imgs/Bucket_sort_1.svg ]] | *Elements are distributed among bins* |
| [[./imgs/Bucket_sort_2.svg]] | *Then, elements are sorted within each bin and then result is concatenated* |
To get the ranges of the buckets, we can use the smallest (min) and biggest (max) element of the array.
\\
The number of elements in each bucket will be,
\[ \text{Range of each bucket} (r) = \frac{(\text{max} - \text{min} + 1)}{ \text{number of buckets}} \]
Then, the ranges of buckets will be,
+ (min + 0.r) <==> (min + 1.r - 1)
+ (min + 1.r) <==> (min + 2.r - 1)
+ (min + 2.r) <==> (min + 3.r - 1)
+ (min + 3.r) <==> (min + 4.r - 1)
+ *etc.*
Then, we can get the bucket number to which we add any array[i] as,
\[ \text{bucket index} = \frac{ \text{array[i]} - \text{min} }{ r } \]
Where,
\[ r = \frac{(\text{max} - \text{min} + 1)}{ \text{number of buckets}} \]
#+BEGIN_SRC c
void bucket_sort(int array[], size_t n, int min, int max, int number_of_buckets, int output[]){
// a bucket will have capacity of [ (max - min + 1) / number_of_buckets ] elements
Vector<int> buckets[number_of_buckets];
int r = (max - min + 1) / number_of_buckets;
// if (max - min + 1) < number_of_buckets, then r could be 0.
// in this case, just set r to 1
if(r <= 0) r = 1;
for(int i = 0; i < n; i++){
// put array[i] in bucket number (array[i] - min) / r
buckets[ (array[i] - min) / r].put(array[i]);
}
// sort elements of buckets and append to final output array
for(int i = 0; i < number_of_buckets; i++){
buckets[i].sort();
output.append(bucket[i]);
}
}
#+END_SRC
*** Time complexity
The time complexity in bucket sort is affected by what sorting algorithm will be used to sort elements in a bucket.
\\
We also have to add the time complexities for initializing the buckets. Suppose there are k buckets, then the time to initialize then is $\theta (k)$.
\\
Also the scattering of elements in buckets will take $\theta (n)$ time.
+ *Worst Case* : Worst case for bucket sort is if all the *elements are in the same bucket*. In this case, the *time complexity is the same as the time complexity of the sorting algorithm used* plus the time to scatter elements and initialize buckets. Therfore,
\[ \text{Time complexity} = \theta (n + k + f(n) ) \]
Where, $f(n)$ is the time complexity of the sorting algorithm and *k* is the number of buckets.
\\
\\
\\
+ *Best Case & Average Case* : Best case for bucket sort is if elements are equally distributed. Then, all buckets will have $n/k$ elements. The time taken to sort single bucket will become f(n/k) and the time taken to sort k buckets will be,
\[ \text{time to sort all buckets} = k \times f \left( \frac{n}{k} \right) \]
Suppose we were using insertion sort, then
\[ \text{for insertion sort} : f(n) = n^2 \]
\[ f \left( \frac{n}{k} \right) = \frac{n^2}{k^2} \]
Therefore,
\[ \text{time to sort all buckets} = \frac{n^2}{k} \]
So, total time have time added to initialize buckets and also scatter elements.
\[ \text{Time complexity} = \theta ( n + k + \frac{n^2}{k} ) \]
This is considered the time complexity for average case.
For best case, we consider the number of buckets is approximately equal to number of elements.
\[ k \approx n \]
Therefore, in best case,
\[ \text{Time complexity} = \theta (n) \]

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.4, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="311px"
height="131px" viewBox="0 0 311 131" enable-background="new 0 0 311 131" xml:space="preserve">
<g id="Layer_3">
<g>
<path fill="#C6FFC6" stroke="#000000" stroke-miterlimit="10" d="M61.75,49.167v50.75c0,3.313-11.51,6-25.708,6
s-25.708-2.687-25.708-6v-50.75"/>
<ellipse fill="#E1FFE1" stroke="#000000" stroke-miterlimit="10" cx="36.042" cy="49.167" rx="25.708" ry="6"/>
</g>
<g>
<path fill="#C6FFC6" stroke="#000000" stroke-miterlimit="10" d="M121.324,49.167v50.75c0,3.313-11.51,6-25.708,6
s-25.708-2.687-25.708-6v-50.75"/>
<ellipse fill="#E1FFE1" stroke="#000000" stroke-miterlimit="10" cx="95.615" cy="49.167" rx="25.708" ry="6"/>
</g>
<g>
<path fill="#C6FFC6" stroke="#000000" stroke-miterlimit="10" d="M180.897,49.167v50.75c0,3.313-11.51,6-25.709,6
c-14.198,0-25.708-2.687-25.708-6v-50.75"/>
<ellipse fill="#E1FFE1" stroke="#000000" stroke-miterlimit="10" cx="155.189" cy="49.167" rx="25.708" ry="6"/>
</g>
<g>
<path fill="#C6FFC6" stroke="#000000" stroke-miterlimit="10" d="M240.472,49.167v50.75c0,3.313-11.51,6-25.709,6
c-14.197,0-25.708-2.687-25.708-6v-50.75"/>
<ellipse fill="#E1FFE1" stroke="#000000" stroke-miterlimit="10" cx="214.763" cy="49.167" rx="25.708" ry="6"/>
</g>
<g>
<path fill="#C6FFC6" stroke="#000000" stroke-miterlimit="10" d="M300.046,49.167v50.75c0,3.313-11.51,6-25.709,6
c-14.197,0-25.709-2.687-25.709-6v-50.75"/>
<ellipse fill="#E1FFE1" stroke="#000000" stroke-miterlimit="10" cx="274.337" cy="49.167" rx="25.709" ry="6"/>
</g>
</g>
<g id="text">
<text transform="matrix(1 0 0 1 24.3306 21.3335)" font-family="'LiberationSans'" font-size="18">29</text>
<text transform="matrix(1 0 0 1 58.5684 21.3335)" font-family="'LiberationSans'" font-size="18">25</text>
<text transform="matrix(1 0 0 1 97.811 21.3335)" font-family="'LiberationSans'" font-size="18">3</text>
<text transform="matrix(1 0 0 1 127.0435 21.3335)" font-family="'LiberationSans'" font-size="18">49</text>
<text transform="matrix(1 0 0 1 166.2861 21.3335)" font-family="'LiberationSans'" font-size="18">9</text>
<text transform="matrix(1 0 0 1 195.5186 21.3335)" font-family="'LiberationSans'" font-size="18">37</text>
<text transform="matrix(1 0 0 1 229.7568 21.3335)" font-family="'LiberationSans'" font-size="18">21</text>
<text transform="matrix(1 0 0 1 263.9941 21.3335)" font-family="'LiberationSans'" font-size="18">43</text>
<text transform="matrix(1 0 0 1 132.5806 72.5)" font-family="'LiberationSans'" font-size="18">29</text>
<text transform="matrix(1 0 0 1 155.1514 83.084)" font-family="'LiberationSans'" font-size="18">25</text>
<text transform="matrix(1 0 0 1 22.4771 78.666)" font-family="'LiberationSans'" font-size="18">3</text>
<text transform="matrix(1 0 0 1 256.377 76)" font-family="'LiberationSans'" font-size="18">49</text>
<text transform="matrix(1 0 0 1 39.6191 94.668)" font-family="'LiberationSans'" font-size="18">9</text>
<text transform="matrix(1 0 0 1 210.8525 83.334)" font-family="'LiberationSans'" font-size="18">37</text>
<text transform="matrix(1 0 0 1 136.4238 99.334)" font-family="'LiberationSans'" font-size="18">21</text>
<text transform="matrix(1 0 0 1 269.9941 94.668)" font-family="'LiberationSans'" font-size="18">43</text>
<text transform="matrix(1 0 0 1 257.6523 123.334)" font-family="'LiberationSans'" font-size="12">4049</text>
<text transform="matrix(1 0 0 1 198.0781 123.334)" font-family="'LiberationSans'" font-size="12">3039</text>
<text transform="matrix(1 0 0 1 138.5044 123.334)" font-family="'LiberationSans'" font-size="12">2029</text>
<text transform="matrix(1 0 0 1 78.9307 123.334)" font-family="'LiberationSans'" font-size="12">1019</text>
<text transform="matrix(1 0 0 1 26.0308 123.334)" font-family="'LiberationSans'" font-size="12">09</text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 4.1 KiB

@ -0,0 +1,72 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.4, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="311px"
height="131px" viewBox="0 0 311 131" enable-background="new 0 0 311 131" xml:space="preserve">
<g id="Layer_3">
<g>
<path fill="#C6FFC6" stroke="#000000" stroke-miterlimit="10" d="M61.75,30.67v50.75c0,3.313-11.51,6-25.708,6
s-25.708-2.687-25.708-6V30.67"/>
<ellipse fill="#E1FFE1" stroke="#000000" stroke-miterlimit="10" cx="36.042" cy="30.67" rx="25.708" ry="6"/>
</g>
<g>
<path fill="#C6FFC6" stroke="#000000" stroke-miterlimit="10" d="M121.324,30.67v50.75c0,3.313-11.51,6-25.708,6
s-25.708-2.687-25.708-6V30.67"/>
<ellipse fill="#E1FFE1" stroke="#000000" stroke-miterlimit="10" cx="95.615" cy="30.67" rx="25.708" ry="6"/>
</g>
<g>
<path fill="#C6FFC6" stroke="#000000" stroke-miterlimit="10" d="M180.897,30.67v50.75c0,3.313-11.51,6-25.709,6
c-14.198,0-25.708-2.687-25.708-6V30.67"/>
<ellipse fill="#E1FFE1" stroke="#000000" stroke-miterlimit="10" cx="155.189" cy="30.67" rx="25.708" ry="6"/>
</g>
<g>
<path fill="#C6FFC6" stroke="#000000" stroke-miterlimit="10" d="M240.472,30.67v50.75c0,3.313-11.51,6-25.709,6
c-14.197,0-25.708-2.687-25.708-6V30.67"/>
<ellipse fill="#E1FFE1" stroke="#000000" stroke-miterlimit="10" cx="214.763" cy="30.67" rx="25.708" ry="6"/>
</g>
<g>
<path fill="#C6FFC6" stroke="#000000" stroke-miterlimit="10" d="M300.046,30.67v50.75c0,3.313-11.51,6-25.709,6
c-14.197,0-25.709-2.687-25.709-6V30.67"/>
<ellipse fill="#E1FFE1" stroke="#000000" stroke-miterlimit="10" cx="274.337" cy="30.67" rx="25.709" ry="6"/>
</g>
</g>
<g id="text">
<text transform="matrix(1 0 0 1 158.4414 123.5049)" fill="#FF6600" font-family="'LiberationSans'" font-size="18">29</text>
<text transform="matrix(1 0 0 1 123.4077 123.5049)" fill="#FF6600" font-family="'LiberationSans'" font-size="18">25</text>
<text transform="matrix(1 0 0 1 23.311 123.5049)" fill="#990000" font-family="'LiberationSans'" font-size="18">3</text>
<text transform="matrix(1 0 0 1 263.5439 123.5049)" fill="#000099" font-family="'LiberationSans'" font-size="18">49</text>
<text transform="matrix(1 0 0 1 58.3452 123.5049)" fill="#990000" font-family="'LiberationSans'" font-size="18">9</text>
<text transform="matrix(1 0 0 1 193.4756 123.5049)" fill="#336633" font-family="'LiberationSans'" font-size="18">37</text>
<text transform="matrix(1 0 0 1 88.3735 123.5049)" fill="#FF6600" font-family="'LiberationSans'" font-size="18">21</text>
<text transform="matrix(1 0 0 1 228.5098 123.5049)" fill="#000099" font-family="'LiberationSans'" font-size="18">43</text>
<g>
<text transform="matrix(1 0 0 1 154.0806 82.5049)" fill="#FF6600" font-family="'LiberationSans'" font-size="18">29</text>
</g>
<g>
<text transform="matrix(1 0 0 1 145.6514 67.5879)" fill="#FF6600" font-family="'LiberationSans'" font-size="18">25</text>
</g>
<g>
<text transform="matrix(1 0 0 1 23.9771 60.1704)" fill="#990000" font-family="'LiberationSans'" font-size="18">3</text>
</g>
<g>
<text transform="matrix(1 0 0 1 268.877 75.0049)" fill="#000099" font-family="'LiberationSans'" font-size="18">49</text>
</g>
<g>
<text transform="matrix(1 0 0 1 35.6191 76.1719)" fill="#990000" font-family="'LiberationSans'" font-size="18">9</text>
</g>
<g>
<text transform="matrix(1 0 0 1 204.8525 67.8389)" fill="#336633" font-family="'LiberationSans'" font-size="18">37</text>
</g>
<g>
<text transform="matrix(1 0 0 1 133.9238 52.8384)" fill="#FF6600" font-family="'LiberationSans'" font-size="18">21</text>
</g>
<g>
<text transform="matrix(1 0 0 1 259.4941 58.6714)" fill="#000099" font-family="'LiberationSans'" font-size="18">43</text>
</g>
<text transform="matrix(1 0 0 1 257.6523 16.3374)" font-family="'LiberationSans'" font-size="12">4049</text>
<text transform="matrix(1 0 0 1 198.0781 16.3374)" font-family="'LiberationSans'" font-size="12">3039</text>
<text transform="matrix(1 0 0 1 138.5044 16.3374)" font-family="'LiberationSans'" font-size="12">2029</text>
<text transform="matrix(1 0 0 1 78.9307 16.3374)" font-family="'LiberationSans'" font-size="12">1019</text>
<text transform="matrix(1 0 0 1 26.0308 16.3374)" font-family="'LiberationSans'" font-size="12">09</text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 4.5 KiB

File diff suppressed because it is too large Load Diff

@ -1,24 +1,16 @@
#+TITLE: Algorithms
#+AUTHOR: Anmol Nawani
#+SETUPFILE: ./org/theme-readtheorg-local.setup
# #+HTML_HEAD: <style>pre.src{background:#343131;color:white;} </style>
# #+SETUPFILE: ./org/theme-readtheorg-local.setup
# # #+HTML_HEAD: <style>pre.src{background:#343131;color:white;} </style>
#+OPTIONS: html-postamble:nil
# https://www.enjoyalgorithms.com/ : Great website
* Lecture 1
#+INCLUDE: "./lectures/1.org"
* Lecture 2
#+INCLUDE: "./lectures/2.org"
* Lecture 3
#+INCLUDE: "./lectures/3.org"
* Lecture 4
#+INCLUDE: "./lectures/4.org"
* Lecture 5
#+INCLUDE: "./lectures/5.org"
* Lecture 6
#+INCLUDE: "./lectures/6.org"
* Lecture 7
#+INCLUDE: "./lectures/7.org"
* Lecture 8
#+INCLUDE: "./lectures/8.org"
#+INCLUDE: "./lectures/1.org" :minlevel 1
#+INCLUDE: "./lectures/2.org" :minlevel 1
#+INCLUDE: "./lectures/3.org" :minlevel 1
#+INCLUDE: "./lectures/4.org" :minlevel 1
#+INCLUDE: "./lectures/5.org" :minlevel 1
#+INCLUDE: "./lectures/6.org" :minlevel 1
#+INCLUDE: "./lectures/7.org" :minlevel 1
#+INCLUDE: "./lectures/8.org" :minlevel 1

Loading…
Cancel
Save