< Nafees />
github
  • Assignments - DSA
  • Long Integer Operations
  • OOP
    • Introduction
    • Implementation
  • Sorting Algorithms
    • Selection Sort
    • Bubble Sort
    • Insertion Sort
    • Shell Sort
    • Shuffle
    • Merge Sort
    • Convex Hull
    • Quick sort
    • System Sort
    • Heap-Sort
  • Binary Search
  • Binary Search By Recursion
  • Two-Pointer
  • String
  • LeetCode 75
    • Array/String
    • Hash Map
    • BST
    • Binary Search
  • Top Interview 150
    • Linked-List
  • Leetcode Programming Skill
    • Math
  • Leetcode 75
  • 🤡Leet Code Extra
  • Arrays
    • 1-D Array
    • 2-D Arrays
  • 👨‍🍳Basic Math - Codechef
  • ☠️Recursion
  • 😁Public Member Functions
    • Strings Functions
  • 👾Linked List
    • What's Linked List & Implementation?
      • Singly Linked List
      • Doubly Linked List
    • Problems
  • 📚Stack
    • What's Stack & Implementation?
      • Stack Using Array
      • Stack using LL
    • Problems
  • 🏧Queue
    • What's Queue & Implementation?
      • Simple Queue
      • Queue using LL
      • Circular Queue
      • Deque using Linked List
      • STL Deque
    • Problems
  • 🏧Priority Queue
    • What's Priority Queue & Implementation
      • OrderedArrayMaxPQ.Java
      • Maximum-Oriented PQ using Binary Heap
      • Minimum-Oriented PQ using Binary Heap
    • Problems
  • 🗓️Hash Table
    • What's Hash Table & Implementation
      • ST - Seperate Chaining
      • ST - Linear Probing
    • Problems
  • 🎴Symbol Table
    • What's Symbol Table and implementation
      • ST Using Binary search (ordered array)
      • ST Using Binary Search Tree
      • ST Using Left-Leaning Red-Black Tree
      • ST Using Hash Table
    • Problems
  • 🔗Union-Find (Dynamic Connectivity problem)
    • What is a Union Find Data Structure?
    • Implementation
  • 🎋Binary Tree
    • What's Binary Tree & Implementation?
      • Traversal
      • Red-Black BST
  • 🌴Trie
    • What's Trie & Implementation?
    • Problems
  • 😎Project
    • Expression Evaluation
Powered by GitBook
On this page

Was this helpful?

  1. Sorting Algorithms

Insertion Sort

This sorting algorithm works by repeatedly inserting elements into their correct position in an array.

PreviousBubble SortNextShell Sort

Last updated 10 months ago

Was this helpful?

Insertion sort is a simple sorting algorithm that works similarly to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed in the correct position in the sorted part.

Question statement:

Notes:

void InsertionSort(int n, vector<int> &arr){
    for(int i = 1; i < n; i++){
        // for rounds 1 to n-1
        //Here we assume 0 index value as sorted
        
        int temp = arr[i];
        int j = i-1;

        for(; j >= 0; j--){
            if(arr[j] > temp) // {1, 4, 2, 3} 
                // Shifting j index value forward by 1
                arr[j+1] = arr[j]; // now array becomes {1, 4, 4, 3} 2 is already stored in temp 
            else 
                break;
        }
        swap(arr[j+1], temp);
    }
}

Space Complexity: constant O(1)

Time complexity:O(n^2)

// 1st round - 1 Comp
// 2nd round - 2 Comp...
// (nth - 1) Round - (n-1) comp 

Best case: Already Sorted = {1, 2, 3, 4,} T.C = O(n)

// total comparisons (n-1) 

Worst Case:Reversed Array = {4, 3, 2, 1} T.C = O(n^2)

https://www.codingninjas.com/studio/problems/insertion-sort_3155179
https://drive.google.com/file/d/10zLQIWEn55nwhFOrHybqnxexf96AX1LE/view
Page cover image