Algorithms

 Algorithms 

What is an algorithm? 

An algorithm is a set of instructions given to a computer to perform a task. For example, a sort algorithm loops through all items in a list and uses an array of logic statements to sort the items. 

Sorting algorithms 

    • Bubble sort – The first two values are compared to find out which one is bigger. If the value on the right is larger than the left (or opposite if you want a descending list) the values are swapped. Then it compares that value with the next one. This repeats for each value until the list are sorted. The bubble sort is not very efficient at all and only useful when dealing with small lists. It is not a complex algorithm to understand and is easy to program. 
    • Insertion sort – The first two values are again compared to find out which one is bigger. If the value on the right is larger than the left (or opposite if you want a descending list) the values are swapped. Then it checks the next value. It compares this value with everything else behind it so it can be inserted into the correct position. The insertion sort is fairly efficient for medium to large lists and not too complex. 
    • Merge sort – A merge sort first splits the list in half. It continues splitting the list until the values are individual. These values are compared with another to find out which one is bigger. The values are then swapped (if necessary) and merged back into the list. Every value goes through this process. The merge sort is very efficient for all sized lists however, it is quite complex to understand. 
    • Quick sortThe quick sort works by picking a pivot value. This could be the last value, the first value, a random value or the value in the middle of the list. This pivot is used to split the list so all values larger than it are grouped and all values smaller than it are grouped. These subarrays are sorted and grouped back together to form a sorted list. The quick sort (as the name suggests) is efficient, however it is complex and difficult to program. 
    • Bucket sort – A bucket sort works by grouping numbers into set groups each with a number range. For example, one bucket stores numbers between 0-5, another stores between 6-10, etc... These numbers are then ordered and merged back into the list. The bucket sort is very efficient and not too complex to understand. 

 

In my opinion, I believe bucket sort to be the best sorting algorithm I have covered. It is very fast and not too hard to understand, however, if you have a list with very similar numbers, it can be hard working out the bucket ranges. Bucket sort is particularly effective in a list with duplicates as they are all grouped together beforehand removing the need to sort them against other numbers. 

Comments

Popular posts from this blog

OSA Assignment 1 - Task 3 GUIDE

OSA Assignment 1 - Task 1 GUIDE

OSA Summer 2023 Mock - Task 1, Assignment 1