Sorting Algorithms (AS)
Sorting Algorithms (AS)
Understanding Sorting Algorithms
-
Sorting algorithms are sequences of instructions that order a list of items or values in a certain pattern.
-
Two common types of sorting algorithms you’ll face are Bubble Sort and Quick Sort.
Bubble Sort
-
Bubble sort works by repeatedly swapping adjacent elements if they are in the wrong order.
-
Key feature is its worst-case and average complexity, which are both O(n^2), hence it is not suitable for large data sets.
-
However, bubble sort has the advantage of being simple to understand and implement.
Quick Sort
-
Quick sort is a divide and conquer algorithm which works by selecting a ‘pivot’ element from an array and partitioning the other elements into two sub-arrays.
-
In quick sort, elements are partitioned according to whether they are less than or equal to or greater than the pivot element.
-
The pivot element is then placed in its correct position, and this process is repeated for sub-arrays.
-
Quick sort is noticeably more efficient than bubble sort with its average and worst-case time complexity of O(n log n) and O(n^2) respectively.
Key Considerations
-
Understanding how different types of sorting algorithms work can equip you to select the most efficient algorithm for a given task.
-
Remember, the efficiency of a sorting algorithm is often given in terms of time complexity, using Big O notation.
-
The type and arrangement of data can also affect the speed efficiency of a sorting algorithm.