notes_stuff

This repo is snapshots of differencet book to have them accessible in nicely organized manner.

View on GitHub

Insertion Sort – Data Structure and Algorithm Tutorials

**Insertion sort** is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is a **stable sorting** algorithm, meaning that elements with equal values maintain their relative order in the sorted output.

**Insertion sort** is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. Then, you pick a card from the unsorted group and put it in the right place in the sorted group.

Insertion Sort Algorithm:

**Insertion sort** is a simple sorting algorithm that works by building a sorted array one element at a time. It is considered an “**in-place**” sorting algorithm, meaning it doesn’t require any additional memory space beyond the original array.

**Algorithm:**

To achieve insertion sort, follow these steps:

Working of Insertion Sort Algorithm:

Consider an array having elements**: {23, 1, 10, 5, 2}**

Insertion-Sort

**First Pass:**

  • Current element is **23**
  • The first element in the array is assumed to be sorted.
  • The sorted part until **0th** index is : **[23]**

**Second Pass:**

  • Compare **1** with **23** (current element with the sorted part).
  • Since **1** is smaller, insert **1** before **23**.
  • The sorted part until **1st** index is: **[1, 23]**

**Third Pass:**

  • Compare **10** with **1** and **23** (current element with the sorted part).
  • Since **10** is greater than **1** and smaller than **23, insert **10** between **1** and **23**.
  • The sorted part until **2nd** index is: **[1, 10, 23]**

**Fourth Pass:**

  • Compare **5** with **1, **10**, and **23** (current element with the sorted part).
  • Since **5** is greater than **1** and smaller than **10, insert **5** between **1** and **10**.
  • The sorted part until **3rd** index is**:** **[1, 5, 10, 23]**

**Fifth Pass:**

  • Compare **2** with **1, 5, 10, and **23** (current element with the sorted part).
  • Since **2** is greater than **1** and smaller than **5** insert **2** between **1** and **5**.
  • The sorted part until **4th** index is: **[1, 2, 5, 10, 23]**

**Final Array:**

  • The sorted array is: **[1, 2, 5, 10, 23]**

C++

// C++ program for insertion sort

#include <bits/stdc++.h>
using namespace std;

// Function to sort an array using
// insertion sort
void insertionSort(int arr[], int n)
{
    int i, key, j;
    for (i = 1; i < n; i++) {
        key = arr[i];
        j = i - 1;

        // Move elements of arr[0..i-1],
        // that are greater than key, 
        // to one position ahead of their
        // current position
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }
}

// A utility function to print an array
// of size n
void printArray(int arr[], int n)
{
    int i;
    for (i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << endl;
}

// Driver code
int main()
{
    int arr[] = { 12, 11, 13, 5, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);

    insertionSort(arr, N);
    printArray(arr, N);

    return 0;
}
// This is code is contributed by rathbhupendra

C

Java

Python

C#

Javascript

PHP

Output

5 6 11 12 13 

**Time Complexity:** O(N^2) 
**Auxiliary Space:** O(1)

Complexity Analysis of Insertion Sort:

**Time Complexity of Insertion Sort**

Space Complexity **of Insertion Sort**

Advantages **of Insertion Sort:**

Disadvantages **of Insertion Sort:**

Applications **of Insertion Sort:**

Insertion sort is commonly used in situations where:

Frequently Asked Questions on Insertion Sort

**Q1. What are the Boundary Cases of the Insertion Sort algorithm?**

Insertion sort takes the maximum time to sort if elements are sorted in reverse order. And it takes minimum time (Order of n) when elements are already sorted.

**Q2. What is the Algorithmic Paradigm of the Insertion Sort algorithm?**

The Insertion Sort algorithm follows an incremental approach.

**Q3. Is Insertion Sort an in-place sorting algorithm?**

Yes, insertion sort is an in-place sorting algorithm.

**Q4. Is Insertion Sort a stable algorithm?**

Yes, insertion sort is a stable sorting algorithm.

**Q5. When is the Insertion Sort algorithm used?**

Insertion sort is used when number of elements is small. It can also be useful when the input array is almost sorted, and only a few elements are misplaced in a complete big array.

Next

C Program For Insertion Sort