Four different sorting algorithms are:
-
Bubble Sort: It is a simple comparison-based sorting algorithm. It repeatedly compares adjacent elements and swaps them if they are in the wrong order. The largest unsorted element "bubbles" up to its correct position in each iteration.
-
Selection Sort: It sorts an array by repeatedly finding the minimum element from the unsorted part and placing it at the beginning. It divides the array into a sorted and an unsorted region, repeatedly selecting the smallest element and swapping it with the first unsorted element.
-
Insertion Sort: It builds the final sorted array one item at a time. It takes each element from the input and places it in its correct position within the sorted portion of the array. It shifts larger elements to the right to make room for the inserted element.
-
Merge Sort: It is a divide-and-conquer algorithm that divides the array into two halves, recursively sorts them, and then merges the two sorted halves to produce a sorted array. It repeatedly divides the array until it consists of individual elements, and then merges them in a sorted manner.
Big O notation is used to describe the upper bound or worst-case time complexity of an algorithm. It provides a way to analyze the scalability and efficiency of algorithms by focusing on the dominant term that grows fastest with the input size.
To evaluate the time complexity of an algorithm, you analyze how the algorithm's runtime increases as the input size grows. You consider the number of basic operations performed, typically expressed as a function of the input size. You focus on the most significant factors affecting the algorithm's efficiency, such as loops, recursive calls, and nested operations. By disregarding constants and lower-order terms, you can determine the algorithm's time complexity class, denoted using Big O notation.
When selecting the best sorting algorithm for a given input, several factors come into play:
-
Input size: Consider the size of the input array. Some algorithms perform better on smaller arrays, while others are more efficient for larger arrays.
-
Input characteristics: Take into account the initial order or randomness of the input. Certain algorithms handle partially sorted or nearly sorted inputs better than others.
-
Time complexity: Compare the time complexity of different algorithms. Consider the expected performance based on Big O notation and how it aligns with your input size and characteristics.
-
Space complexity: Evaluate the space requirements of each algorithm. Some sorting algorithms require additional memory, which can be a consideration if memory usage is limited.
A stable sorting algorithm maintains the relative order of elements with equal keys. In other words, if two elements have the same key and appear in a particular order before sorting, they should still appear in the same order after sorting. Stability is an important property for certain applications, such as sorting objects with multiple keys or when the original order needs to be preserved. Merge Sort and Insertion Sort are examples of stable sorting algorithms, while algorithms like Quick Sort and Heap Sort are not inherently stable (although modifications can be made to make them stable).