Insertion SortInsertion sort is a comparisonbased sorting algorithm that builds the final sorted array one element at a time. It works by dividing the input array into two regions: a sorted region and an unsorted region. Initially, the sorted region consists of only the first element, and the rest of the array is considered unsorted. The algorithm iterates through the unsorted region, one element at a time, and compares each element with the elements in the sorted region. It finds the correct position for the current element in the sorted region by shifting the elements greater than it to the right. This shifting creates a space for the current element to be inserted. To perform the insertion, the algorithm starts from the rightmost element of the sorted region and compares it with the current element. If the element from the sorted region is greater, it is shifted one position to the right. This process continues until the appropriate position for the current element is found. Once the correct position is determined, the algorithm inserts the current element into the sorted region. This insertion step is repeated for all the elements in the unsorted region, gradually expanding the sorted region until the entire array is sorted. Insertion sort is an inplace algorithm, meaning it does not require additional memory to perform the sorting. It is also considered a stable sorting algorithm, as it preserves the relative order of elements with equal values. The time complexity of insertion sort is O(n^2) in the worstcase scenario, where n is the number of elements in the input array. However, in practice, it can have better performance for small or partially sorted arrays. In the bestcase scenario, when the input array is already sorted, the time complexity reduces to O(n). The averagecase time complexity is also O(n^2), making it less efficient compared to more advanced sorting algorithms like quicksort or mergesort. Despite its relatively slower performance for large arrays, insertion sort has some advantages. It is easy to understand and implement, making it a suitable choice for small datasets or situations where simplicity is preferred over efficiency. Additionally, it performs well on partially sorted or nearly sorted arrays, requiring fewer comparisons and swaps compared to other algorithms. In conclusion, insertion sort is a simple and intuitive sorting algorithm that builds the final sorted array by repeatedly inserting elements into their correct positions within the already sorted region. While it may not be the most efficient choice for large datasets, it has its merits and can be a useful tool in certain scenarios. HistoryThe insertion sort algorithm has a long history and can be traced back to the early days of computer science. It was first mentioned by the computer scientist and mathematician John von Neumann in 1945, although the algorithm was likely known before that time. The basic concept of insertion sort can be found in card games like playing cards or bridge. Players often sort their cards by continuously inserting a new card into its correct position in the already sorted hand. This manual sorting technique inspired the development of the insertion sort algorithm. The algorithm gained prominence with the advent of computers and the need for efficient sorting methods. In the early days of computing, memory was limited, and complex sorting algorithms were not feasible. Insertion sort, with its simplicity and ability to sort small arrays efficiently, became a popular choice. One of the earliest references to the insertion sort algorithm in the context of computer programming can be found in the book "The Preparation of Programs for an Electronic Digital Computer" by Maurice Wilkes in 1951. Wilkes described the algorithm as a technique to sort a list of numbers in a computer program. Over the years, insertion sort has been analyzed and studied extensively. Its time complexity and performance characteristics have been wellunderstood. Researchers have compared insertion sort with other sorting algorithms and identified its strengths and weaknesses. While insertion sort may not be the most efficient algorithm for large datasets, it has found its applications in various scenarios. It is particularly useful when dealing with small or partially sorted arrays, where its simplicity and ability to perform well make it an attractive choice. Despite the development of more advanced sorting algorithms, insertion sort continues to be taught and studied as an important sorting technique. Its straightforward implementation and intuitive nature make it a valuable tool for learning sorting algorithms and understanding the fundamental principles of computer science. Algorithm for Insertion Sort
Below is the program for insertion sort algorithm in C++:Output: 5 6 11 12 13 ............ Process executed in 1.11 seconds Press any key to continue. Explanation: Here's a stepbystep explanation of the given code:
Time Complexity Analysis: The time complexity of the Insertion Sort algorithm implemented in this code is O(n2) in the worst and average cases and O(n) in the best case. The main loop runs for 0(n) iterations (where O(n) is the size of the array), and for each iteration, the inner while loop may run up to i times, where i is the current index in the outer loop. In the worst case, when the array is in reverse order, the inner loop will run its maximum number of times, resulting in O(n2) time complexity. In the best case, when the array is already sorted, the inner loop won't execute, and the time complexity is O(n). Space Complexity Analysis: The space complexity of the code is O(1). The reason is that the algorithm performs sorting inplace, without using any additional data structures whose space requirements depend on the input size. The only extra space used is for variables like i, key, and j, which are of constant size, regardless of the input array size. In summary, the time complexity is O(n2) in the worst and average cases and O(n) in the best case, while the space complexity is O(1) as the algorithm uses a constant amount of extra space. Advantages of Insertion Sort
Disadvantages of Insertion Sort:
In summary, while insertion sort has simplicity, space efficiency, and performs well on small inputs or partially sorted arrays, its quadratic time complexity and lack of adaptiveness make it less suitable for large data sets or already sorted arrays. Consider the characteristics of the input data and the desired performance requirements when choosing a sorting algorithm. Applications of Insertion SortInsertion sort is a simple yet efficient sorting algorithm that can be applied to various scenarios. Here are some common applications of insertion sort:
Overall, insertion sort may not be the most efficient sorting algorithm for largescale or highly unsorted data sets. However, its simplicity, suitability for specific scenarios, and educational value make it a valuable algorithm in various applications.
Next TopicNeedlemanWunsch Algorithm
