Assignment 2

Implementation of Sorting Algorithms

By Isaac K. Attuah

This program sorts numbers using the following algorithms:

Only the sorted array (comparisons) will be shown for input sizes above 16 elements. Total number of steps are always indicated at the end of the output.

Press Clear after each iteration of Quick and Merge Sort to reset their respective counters.













==========>







Comparison Of Algorithms With Different Input Sizes








The aim of this lab was to test the number of comparisons made by different algorithms based on the number of input values that were passed to each algorithm. The input values were 50, 100, 150, 200, 250, 300, 350, 400, 450, 500. By observation, Bubble Sort, Insertion Sort and Quick Sort had a noticeable delay in displaying output however Merge Sort remained the most consistent throughout the testing process. Insertion, Merge Sort and Bubble Sort were the stable sorting techniques whereas Quick Sort was not. Merge Sort was the only algorithm that split the problems into multiple arrays hence it had a better advantage over the other algorithms in terms of working with the console.log output of JavaScript.

The graph above confirms that the average time complexities of each of the algorithms. Bubble Sort and Insertion Sort where both O(n^2) and Merge Sort and Quick Sort were both O(nlogn). The behavior of the graph showed that Quick Sort was the fastest for all the input values provided.

The individual sorting algorithms work in their own unique ways. The list below shows how each sorting technique works including some key differences in their operations

  1. Bubble Sort: Bubbles largest elements to the end of list
  2. Insertion Sort: Inserts elements in their rightful position creating sorted array portion till entire array is sorted
  3. Merge Sort: Uses Divide and Conquer technique to sort arrays in smaller arrays and returns a sorted array as output
  4. Quick Sort: A random element is made a pivot and elements on the left- and right-hand side are compared to this pivot. Elements smaller than the pivot go to the left-hand side and elements larger than pivot go to the right-hand side.