Notes on algorithms
Tags: computerscience, javascript, programming
Published: 2020-01-10 13:47:45

---
title: Notes on algorithms
published: true
description: notes on algorithms after CS50 week 3
tags: computerscience, javascript, programming
cover_image: https://thepracticaldev.s3.amazonaws.com/i/xostdllclnv7e09tri35.jpeg
---

{% youtube kPRA0W1kECg %}

I'm doing [CS50: Introduction to Computer Science](https://www.edx.org/course/cs50s-introduction-to-computer-science) on [edx.org](https://www.edx.org). I find it's a great way to review what I learn by completing, rewriting and sharing some of my notes. 

Note: Big O notation can be though of “on the order of” and it represents the running time of an algorithm. In the C examples, `n` is equivalent to `sizeof(arr)/sizeof(arr[0])` which translates in JavaScript to `arr.length`. 

Week 3 is about algorithms. 😺

## Table Of Contents
* [Linear Search](#linear-search)
* [Binary Search](#binary-search)
* [Bubble Sort](#bubble-sort)
* [Selection Sort](#selection-sort)
* [Insertion Sort](#insertion-sort)
* [Recursion](#recursion)
* [Merge Sort](#merge-sort)

### Linear Search <a name="linear-search"></a>
To iterate across the array from left to right searching for a target element.

_Pseudocode example #1:_
```
Repeat, starting at the first element:
    If the element is the target element, stop
    Else, move to the next element
```

_Pseudocode example #2:_
```
For i from 0 to n–1
    If i'th element is target_element
        Return true
Return false
```

_C example:_
```cpp
bool linearSearch(int arr[], int n, int target) 
{ 
    for (int i = 0; i < n; i++) 
        if (arr[i] == target) return true;
    return false; 
} 
```

_JavaScript example:_
```javascript
linearSearch = (arr, target) => {
    for (let i = 0; i < arr.length; i++)
        if (arr[i] === target) return true;
    return false;
}
```

**Linear Search algorithm's**

- **Worst case scenario:** 
Having to look through the entire array of `n` elements in the case where the target element is the last one or it is not in the array. 
In Big O notation, it translates to _O(n)_.

- **Best case scenario:**
The target element is the 1st element. 
In Big O notation, it translates to _Ω(1)_.


### Binary Search <a name="binary-search"></a>
To find the target element by reducing the search area by half each time. Make sure the array on which the binary search algorithm is used on is sorted, else it's impossible to make assumptions about its content.

_Pseudocode example #1:_
```
Repeat until the (sub)array is of size 0:
    Calculate the middle point of the current (sub)array
    If the target element is the middle element, stop
    Else if it's less than the middle: 
        End point is now just to the left of the current middle, repeat
    Else if it's greater than the middle: 
        Start point is now just to the right of the current middle, repeat
```

_Pseudocode example #2:_
```
If no items
    Return false
If middle item is target_element
    Return true
Else if target_element < middle item
    Update end point
    Search left half
Else if target_element > middle item
    Update start point
    Search right half
```

_C example (recursive):_
```cpp
int binarySearch(int arr[], int target, int start, int end) 
{ 
    if (end >= start) { 
        // instead of (start+end)/2 to avoid overflow
        int mid = start+(end-start)/2; 
        if (arr[mid] == target) return mid; 
        else if (arr[mid] > target) return binarySearch(arr, target, start, mid-1); 
        else return binarySearch(arr, target, mid+1, end); 
    } 
    return -1; 
}
```

_JavaScript example (recursive):_
```javascript
binarySearch = (arr, target, start, end) => {   
    if (end >= start) {
      	let mid = Math.floor((start+end)/2);
      	if (arr[mid] === target) return mid;
      	else if(arr[mid] > target) return binarySearch(arr, target, start, mid-1); 
      	else return binarySearch(arr, target, mid+1, end); 
	}
    return false;
} 
```

**Binary Search algorithm's**

- **Worst case scenario:** 
Having to divide a list of `n` elements in half repeatedly to find the target element because the target is found at the end of the last division or it is not in the array.
In Big O notation, it translates to _O(log n)_.

- **Best case scenario:**
The target element is at midpoint of the array, so we can stop searching immediately after we start.
In Big O notation, it translates to _Ω(1)_.


### Bubble Sort <a name="bubble-sort"></a>
To sort in a bubbling way: move higher values towards the right of the array and lower values, towards the left.

_Pseudocode example #1:_
```
Set swap counter to a non-zero value
Repeat until the swap counter is equal to 0:
    Reset swap counter to 0
    Look at each adjacent pair:
        If two adjacent elements are not in order:
            Swap them
            Add one to the swap counter
```

_Pseudocode example #2:_
```
Repeat until no swaps
    For i from 0 to n–2
        If i'th and i+1'th elements out of order
            Swap them
```

_C example:_
```cpp
void bubbleSort(int arr[], int n) 
{ 
    for (int i = 0; i < n-1; i++)
        for (int j = 0; j < n-i-1; j++)
            if (arr[j] > arr[j+1])
            {
                int temp = arr[j]; 
                arr[j] = arr[j+1]; 
                arr[j+1] = temp;
            }
} 
```

_JavaScript example:_
```javascript
bubbleSort = arr => {
    for (let i = 0; i < arr.length-1; i++)
        for (let j = 0; j < arr.length-i-1; j++)
            if (arr[j] > arr[j+1]) {
                let temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
    return arr;
}
```

Because comparing the `i`th and `i+1`th element, the sorting only needs to go up to `n-2` for `i` before swapping the two elements if they're out of order. Knowing the largest `n-1` elements will have bubbled to the right, the sorting can stop after `n-1` passes.
When re-going through the array, only consider the unsorted elements.
When the swap counter remains at `0`, there is nothing else to swap.

**Bubble sort algorithm's**

- **Worst case scenario:** 
Having to bubble each of the elements all the way across the array because the array is in reverse order. Since it's only possible to fully bubble one element into its sorted position per pass, the sorting must happen `n` times.
In Big O notation, it translates to _O(n²)_.

- **Best case scenario:**
The array is already perfectly sorted, resulting in no swapping on the first pass.
In Big O notation, it translates to _Ω(n)_.


### Selection Sort <a name="selection-sort"></a>
To find the smallest unsorted element and add it to the end of the sorted list. 

_Pseudocode example #1:_
```
Repeat until there is no unsorted elements remaining:
    Search unsorted part of data to find the smallest value
    Swap the found value with the first element of the unsorted part
```

_Pseudocode example #2:_
```
For i from 0 to n–1
    Find smallest item between i'th item and last item
    Swap smallest item with i'th item
```

_C example:_
```cpp
void selectionSort(int arr[], int n) 
{ 
    for (int i = 0; i < n-1; i++)
    {
        int min = i; 
        for (int j = i+1; j < n; j++) 
            if (arr[j] < arr[min]) min = j;
        int temp = arr[min];
        arr[min] = arr[i];
        arr[i] = temp;
    }
}
```

_JavaScript example:_
```javascript
selectionSort = arr => { 
    for (let i = 0; i < arr.length-1; i++) {
        let min = i; 
        for (let j = i+1; j < arr.length; j++)
            if (arr[j] < arr[min]) min = j;
        let temp = arr[min];
        arr[min] = arr[i];
        arr[i] = temp;
    }
    return arr;
}
```

**Selection sort algorithm's**

- **Worst case scenario:** 
Having to repeat the sorting process `n` times to iterate each of the `n` elements of the array to find the smallest unsorted element and sort it. Only one element gets sorted on each pass.
In Big O notation, it translates to _O(n²)_.

- **Best case scenario:**
The same as the worst case scenario as there is no way to guarantee the array is sorted until the sorting process iterates over all of the elements of the array.
In Big O notation, it translates to _Ω(n²)_.


### Insertion Sort <a name="insertion-sort"></a>
To build a sorted array in place; shifting elements out of the way to make room if necessary as the array is being built. 

_Pseudocode example #1:_
```
Call the first element of the array sorted
Repeat until all elements are sorted:
    Insert next unsorted item into sorted part shifting the required number of items
```

_Pseudocode example #2:_
```
For i from 1 to n–1
    Insert next unsorted item into sorted part shifting i items
```

_C example:_
```cpp
void insertionSort(int arr[], int n) 
{ 
    for (int i = 1; i < n; i++) { 
        int key = arr[i]; 
        int j = i-1; 
        while (j >= 0 && arr[j] > key) { 
            arr[j+1] = arr[j]; 
            j = j-1; 
        } 
        arr[j+1] = key; 
    } 
} 
```

_JavaScript example:_
```javascript
insertionSort = arr => { 
    for (let i = 1; i < arr.length; i++) { 
        let key = arr[i]; 
        let j = i-1; 
        while (j >= 0 && arr[j] > key) { 
            arr[j+1] = arr[j]; 
            j = j-1; 
        } 
        arr[j+1] = key; 
    } 
    return arr;
} 
```

**Insertion sort algorithm's**

- **Worst case scenario:** 
Having to shift each of the `n` elements from `n` positions each time to make an insertion because the array is in reverse order.
In Big O notation, it translates to _O(n²)_.

- **Best case scenario:**
The array is already sorted. Only gotta keep moving between unsorted and sorted elements as we iterate over each of them.
In Big O notation, it translates to _Ω(n)_.


### Recursion <a name="recursion"></a>
To code _elegantly_. 🌹

Recursion is related to how an algorithm or a function is implemented, it isn't an algorithm itself.

A recursive function invokes itself as part of its execution.

Detailed example using the factorial function:
- _n!_ is defined over all positive integers
- _n!_ equals all of the positive integers less than or equal to _n_, multiplied together
- _n!_ as `fact(n)`:

_Pseudocode example #1:_
```
fact(1) = 1
fact(2) = 2 * 1
fact(3) = 3 * 2 * 1
…
```

_Pseudocode example #2:_
```
fact(1) = 1
fact(2) = 2 * fact(1)
fact(3) = 3 * fact(2)
…
```

_The basis for a recursive definition of the factorial function:_
```
fact(n) = n * fact(n-1)
```

**Recursive function has two cases that can apply given any input:**
- **Base case:** terminates the recursive process when triggered
- **Recursive case:** where the recursion happens

```
int fact(int n) 
{
    // base case
    if (n == 1)
        return 1;
    // recursive case
    else
        return n * fact(n-1);
}
```

**There can be multiple base cases.**
Example the fibonacci number sequence where: 
- 1st element is `0`
- 2nd element is `1`
- `n`th element is the sum of `(n-1)+(n-2)`

**There can be multiple recursive cases.**
Example the collatz conjecture. 

The next C and JavaScript examples define a `collatz` function that calculates how many steps it takes to get "back to 1":

_C example:_
```cpp
int collatz(int steps) 
{
    // base case
    if (steps == 1) return 0;
    // recursive case: even numbers
    else if ((steps % 2) == 0) return 1+collatz(steps/2);
    // recursive case: odd numbers
    else return 1+collatz(3*steps+1);
}
```

_JavaScript example:_
```javascript
collatz = steps => {
    // base case
    if (steps == 1) return 0;
    // recursive case: even numbers
    else if ((steps % 2) == 0) return 1+collatz(steps/2);
    // recursive case: odd numbers
    else return 1+collatz(3*steps+1);
}
```


### Merge Sort <a name="merge-sort"></a>
To divide an array into smaller arrays to sort and then, combine those sorted arrays back together in sorted order.

_Pseudocode example #1:_
```
If only one element
  Return
Else
    Sort left half of elements
    Sort right half of elements
    Merge sorted halves
```

_Pseudocode example #2:_
```
Sort the left half of the array (assuming n > 1)
Sort right half of the array (assuming n > 1)
Merge the two halves together
```

_C example (recursive):_
```cpp
// merges two subarrays of arr[]
void merge(int arr[], int leftIndex, int mid, int rightIndex) 
{ 
    int n1 = mid-leftIndex+1; 
    int n2 =  rightIndex-mid; 
  
    // temp arrays
    int Left[n1], Right[n2]; 
  
    // copy data to temp arrays
    for (int i = 0; i < n1; i++) 
        Left[i] = arr[leftIndex+i]; 
    for (int j = 0; j < n2; j++) 
        Right[j] = arr[mid+1+j]; 
  
    // merge the temp arrays back into arr[]
    int i = 0; // init index of 1st subarray 
    int j = 0; // init index of 2nd subarray 
    int k = leftIndex; // init index of merged subarray 
    while (i < n1 && j < n2) 
    { 
        if (Left[i] <= Right[j]) 
        { 
            arr[k] = Left[i]; 
            i++; 
        } 
        else
        { 
            arr[k] = Right[j]; 
            j++; 
        } 
        k++; 
    } 
  
    // copy the remaining elements of Left[], if any
    while (i < n1) 
    { 
        arr[k] = Left[i]; 
        i++; 
        k++; 
    } 
  
    // copy the remaining elements of Right[], if any
    while (j < n2) 
    { 
        arr[k] = Right[j]; 
        j++; 
        k++; 
    } 
} 
  
void mergeSort(int arr[], int leftIndex, int rightIndex) 
{   
    if (leftIndex < rightIndex) 
    { 
        // instead of (l+r)/2 to avoid overflow
        int mid = leftIndex+(rightIndex-leftIndex)/2; 
        // sort first and second halves 
        mergeSort(arr, leftIndex, mid); 
        mergeSort(arr, mid+1, rightIndex); 
        // merge them back together
        merge(arr, leftIndex, mid, rightIndex); 
    } 
} 
```

_JavaScript example (recursive):_
```javascript
// to merge left subarray and right subarray
merge = (left, right) => {
    let resultArray = [], leftIndex = 0, rightIndex = 0;

    // concat values into the resultArray in order
    while (leftIndex < left.length && rightIndex < right.length) {
        if (left[leftIndex] < right[rightIndex]) {
            resultArray.push(left[leftIndex]);
            leftIndex++;
        } else {
            resultArray.push(right[rightIndex]);
            rightIndex++;
        }
    }
   
    // concat remaining element from either left OR right
    return resultArray
        .concat(left.slice(leftIndex))
        .concat(right.slice(rightIndex));
}

mergeSort = arr => {
    // if array has one element or is empty, no need to sort
    if (arr.length <= 1) return arr;

    const mid = Math.floor(arr.length/2);
    // divide the array into left and right
    const left = arr.slice(0, mid);
    const right = arr.slice(mid);

    // merge back together using recursion
    return merge(mergeSort(left), mergeSort(right));
}
```

**Merge sort algorithm's**

- **Worst case scenario:** 
Having to split `n` elements up before recombining them effectively, doubling the sorted sub-arrays as they are built.
In Big O notation, it translates to _O(n log n)_.

- **Best case scenario:**
The array is already sorted, but still gotta be split and recombined back together to know it is sorted.
In Big O notation, it translates to _Ω(n log n)_.

Resources: 
- [Comparison Sorting Algorithms (visualization)](https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html)
- [Sorting Algorithms on brilliant.org](https://brilliant.org/wiki/sorting-algorithms/)
- [Sorting Algorithms on geeksforgeeks.org](https://www.geeksforgeeks.org/sorting-algorithms/)
- [Sorting Algorithms Visualized](https://imgur.com/gallery/voutF)