0% found this document useful (0 votes)
6 views

Insertion Sorting

used in data structures
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
6 views

Insertion Sorting

used in data structures
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 3
Insertion sort ic simple and infuitive sorting algorithm that builds a corted array (or list) one element af a time. It is particularly efective for small datasets or nearly sorted data. The algorithm works similarly $o how one might sort playing cards in their hands How Insertion Sort Works Initial Sep Start with the second element of the array, assuming the first element is already sorted. The array is divided into two parts: a sorted part and an unsorted part: Terative Process: For each element in the unsorted part, compare it with the elements in the sorted part Shift all larger elervents in the sorted part 4o the right to make space for the current element. Insert the current element into its correct position in the sorted part. Repeat: Continue this process until all elements are sorted. Step-by-Step Example Lets consider an example array: [5, 2,4 |, 5, 6]. Tnitial State: Sorted: [5] Unsorted: [2, 4, |, S, 6] First Pass (Insert 2): Compare 2 with 5. Since 2 <5, shift $ 4o the right and insert 2 Sorted: [2, S] Unserted: [4 |, S, 6 Second Pass (Insert 4): is greater than 5, se it stays in place Sorted: [2, 5, Unsorted: [], S, 6] Third Pass (Insert 1): Compare | with 4, S, and 2. Shift all to the right and insert |. Sorted: (I, 2,5, Unsorted: [5, 6] Fourth Pass (Insert S): Compare 5 with 4, Shift 4-40 the right and insert 5. Sorted: [, 2, 5, 5, Unsorted: [6] Fifth Pass (Insert 6): Compare 6 with 4. Shift 4 4o the right and insert 6. Sorted: 1,2,5,5,6,4 Unsorted: 0 Time Complexity Best Case: O(n) - when the array is already sorted. Average Case: On?) ~ for random order Worst Case: O(n’) ~ when the array is sorted in reverse order Space Complexity O() = Insertion sort is an in-place sorting algorithm, weaning it requires @ constant amount of additional space Advantages and Disadvantages ‘Advantages: Simple 4o irwplernent and understend Efficient for small datasets or nearly sorted deta. Stable (does not change the relative order of equal elements). Disadvantages: Inefficient for large datasets compared fo more advanced algorithms like quicksort or mergesort Diagram of Insertion Sort Here's o visual representation of how insertion sort works: Initial Array: (5, 2, 4,1, 5, 6) Step k (5) 112.415 6 Step 2: (2, SJI[4.1,5, 6] Step 3: (2,5, 41,5, 6] Step 412,35, DIS, 6] Step $025,501 (6) Step 60L25,56410 Code Implementation Here's a simple implementation of insertion sort in Python: def insertion_sortare): for i in rangell len(ore) key = arrfl geist ‘# Move elements of arr{0.i-1], that are greater than key, # to one position ahead of their current position while j >= 0 and key < are(j: arrfj + 1] = orl jo! arrlj + = key # Example usage arr = (5,

You might also like