Bubble Sort

  • Run a nested for loop to traverse the input array using two variables i and j, such that 0 ≤ i < n-1 and 0 ≤ j < n-i-1
  • If arr[j] is greater than arr[j+1] then swap these adjacent elements, else move on
  • Print the sorted array

Insertion Sort

  • Iterate from arr[1] to arr[N] over the array.
  • Compare the current element (key) to its predecessor.
  • If the key element is smaller than its predecessor, compare it to the elements before. Move the greater elements one position up to make space for the swapped element.

Merge Sort

  • step 1: start
  • step 2: declare array and left, right, mid variable
  • step 3: perform merge function. if left > right return mid= (left+right)/2 mergesort(array, left, mid) mergesort(array, mid+1, right) merge(array, left, mid, right)
  • step 4: Stop

Quick Sort

                Pseudo Code for recursive QuickSort function:

                /* low  –> Starting index,  high  –> Ending index */
                
                
                
                quickSort(arr[], low, high) {
                
                    if (low < high) {
                
                        /* pi is partitioning index, arr[pi] is now at right place */
                
                        pi = partition(arr, low, high);
                
                        quickSort(arr, low, pi – 1);  // Before pi
                
                        quickSort(arr, pi + 1, high); // After pi
                
                    }
                
                }
                
                Pseudo code for partition()  
                
                /* This function takes last element as pivot, places the pivot element at its correct position in sorted array, and places
                 all smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot */
                
                partition (arr[], low, high)
                {
                    // pivot (Element to be placed at right position)
                    pivot = arr[high];  
                
                    i = (low – 1)  // Index of smaller element and indicates the 
                    // right position of pivot found so far
                
                    for (j = low; j <= high- 1; j++){
                
                        // If current element is smaller than the pivot
                        if (arr[j] < pivot){
                            i++;    // increment index of smaller element
                            swap arr[i] and arr[j]
                        }
                    }
                    swap arr[i + 1] and arr[high])
                    return (i + 1)
                }
            

Selection Sort

  • Initialize minimum value(min_idx) to location 0.
  • Traverse the array to find the minimum element in the array.
  • While traversing if any element smaller than min_idx is found then swap both the values.
  • Then, increment min_idx to point to the next element.
  • Repeat until the array is sorted.

Size Speed