Computers are often used to sort large amounts of data (e.g. numerical order or alphabetical order). Though this may seem like a simple task to complete, a lot of research has focused on finding the most effective approach to sort data.
Two of the most basic algorithms used to sort data are the Bubble Sort Algorithm, and the Insertion Sort Algorithm.
Insertion Sort Algorithm
To start with, the algorithm considers the first value of a list as a sorted sub-list (of one value to start with). This iterative algorithm then checks each value in the remaining list of values one by one. It inserts the value into the sorted sub-list of the data set in the correct position, moving higher ranked elements up as necessary.
This algorithm is not always very efficient and is mostly recommended when sorting a small lists of values or a list that is already almost sorted.
You can check our Python implementation of this algorithm on this blog post.
Bubble Sort Algorithm
The algorithm starts at the beginning of the data set. It compares the first two value, and if the first is greater than the second, it swaps them. It continues doing this for each pair of adjacent values to the end of the data set. It then starts again with the first two elements, repeating until no swaps have occurred on the last pass.
This algorithm is particularly useful when you need to find the top x values of a list.
You can check our Python implementation of this algorithm on this blog post.
Your Task
Practise sorting lists of numbers using both the Insertion sort and the Bubble sort algorithms using the two tabs below: