Insertion Sort | Sorting Algorithms

 Insertion Sort is a simple sorting algorithm that works by building up a sorted list one item at a time. It is an in-place comparison-based algorithm and is often used as an alternative to more complex sorting algorithms such as quicksort or merge sort. The basic idea behind insertion sort is to iterate through an array, and for each element in the array, insert it into its correct position in a subarray that is already sorted.

In C++, the insertion sort algorithm can be implemented using a for loop. The outer loop iterates through the array, and the inner loop iterates through the subarray that is already sorted. The element at the current position of the outer loop is then compared to each element in the subarray, and if it is smaller, it is inserted into the correct position.

Here is an example implementation of the insertion sort algorithm in C++:

void insertionSort(int arr[], int n) {
    int i, key, j;
    for (i = 1; i < n; i++) {
        key = arr[i];
        j = i - 1;
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }
}

 

 The time complexity of the insertion sort algorithm is O(n^2) in the worst case, and O(n) in the best case (when the array is already sorted). The best-case scenario occurs when the array is already sorted, in that case we can observe that the inner while loop will not run and the time complexity will be O(n).

There are many other sorting algorithms that can be used in C++, including:

Bubble sort, which repeatedly swaps adjacent elements that are in the wrong order
quicksort, which uses a divide-and-conquer approach to sort an array
merge sort, which also uses a divide-and-conquer approach to sort an array
heapsort, which uses a binary heap data structure to sort an array
radix sort, which sorts elements based on their individual digits
selection sort, which repeatedly selects the smallest element from the unsorted portion of the array and moves it to the sorted portion of the array.
Each sorting algorithm has its own strengths and weaknesses, and the best choice of algorithm will depend on the specific requirements of the problem at hand. Factors to consider when choosing a sorting algorithm include the size of the array to be sorted, whether the array is already partially sorted, and the memory constraints of the system.

In general, when the size of the array is small, insertion sort is a good choice, as it has a best-case time complexity of O(n) and a worst-case time complexity of O(n^2). However, as the size of the array increases, more efficient algorithms such as quicksort or merge sort should be used, as their time complexity is O(n log n).

Another important aspect of sorting algorithms is the stability, which means the relative order of elements with equal values is preserved. Insertion sort is a stable sorting algorithm because it preserves the relative order of elements with equal values.

In conclusion, Insertion Sort is a simple and easy-to-understand sorting algorithm that is well-suited to small arrays and partially-sorted data. It has a best-case time complexity of O(n) and a worst-case time complexity of O(n