diff --git a/lectures/7.org b/lectures/7.org index 9fd1874..d92f2c1 100644 --- a/lectures/7.org +++ b/lectures/7.org @@ -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. diff --git a/lectures/8.org b/lectures/8.org index e2d7229..5d6f7f2 100644 --- a/lectures/8.org +++ b/lectures/8.org @@ -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 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) \] diff --git a/lectures/imgs/Bucket_sort_1.svg b/lectures/imgs/Bucket_sort_1.svg new file mode 100644 index 0000000..099d039 --- /dev/null +++ b/lectures/imgs/Bucket_sort_1.svg @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + 29 + 25 + 3 + 49 + 9 + 37 + 21 + 43 + 29 + 25 + 3 + 49 + 9 + 37 + 21 + 43 + 40–49 + 30–39 + 20–29 + 10–19 + 0–9 + + diff --git a/lectures/imgs/Bucket_sort_2.svg b/lectures/imgs/Bucket_sort_2.svg new file mode 100644 index 0000000..4f3b591 --- /dev/null +++ b/lectures/imgs/Bucket_sort_2.svg @@ -0,0 +1,72 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + 29 + 25 + 3 + 49 + 9 + 37 + 21 + 43 + + 29 + + + 25 + + + 3 + + + 49 + + + 9 + + + 37 + + + 21 + + + 43 + + 40–49 + 30–39 + 20–29 + 10–19 + 0–9 + + diff --git a/main.html b/main.html index c7c4696..816e2fa 100644 --- a/main.html +++ b/main.html @@ -3,18 +3,196 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + Algorithms - - - - - - +