Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Implement Insertion Sort Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Implement Insertion Sort | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given an array, sort it using insertion sort.

Approach

In insertion sort, the basic idea is to divide the array into a sorted and an unsorted part. Elements from the unsorted part are picked one by one and placed at their correct position in the sorted part until the whole array is sorted.

  • Iterate the array from i = 1 to i < n. Compare the i-th element to its previous elements one by one until it is less than its previous elements and keep moving the greater elements forward.
  • Once the correct position for the i-th element is found (i.e. i-th element is greater than its previous element), place it at that position.

Analysis

  • Time Complexity: O(n2)
  • Auxiliary Space Complexity: O(1)

Implementation

C++
void insertionSort(vector<int> &arr) {
   int n = arr.size();
   for (int i = 1; i < n; i++) {
       int currentElement = arr[i];
       int position = i - 1;
       while (position >= 0 && arr[position] > currentElement) {
           arr[position + 1] = arr[position];
           position--;
       }
       arr[position + 1] = currentElement;
   }
}
Java
class Solution {
   void insertionSort (int[] arr) {
       int n = arr.length;
       for (int i = 1; i < n; i++) {
           int currentElement = arr[i];
           int position = i - 1;
           while (position >= 0 && arr[position] > currentElement){
               arr[position + 1] = arr[position];
               position--;
           }
           arr[position + 1] = currentElement;
       }
   }
}
Related Content
Arithmetic Sequence
Implement Counting Sort
Implement Merge Sort
Implement Quicksort
Inversion Count
Kth Largest Element
Merge Overlapping Intervals
Merge Sorted Subarrays
Square sorted array
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet