Skip to content
ExamHope Logo

examhope

Primary Menu
  • Digital Logic
    • Arithmetic Operations
    • Asynchronous/Ripple Counters
    • Basic Gates
    • Boolean Algebraic Theorems
    • Codes
  • Data Structures
    • Binary Heaps
    • Binary Search
    • Binary Search Trees
    • Binary Tree
    • Binary Tree Sort
    • Bipartite Graphs
    • Complete Graph
  • Theory of Computation
    • Finite Automata
    • Finite Automaton First Example
  • Current Affairs
    • Sports News
    • Tech News
    • Bollywood News
    • Daily News
  • Database
  • Computer Network
  • Computer Organization and Architecture
  • C Language
  • Operating Systems
  • Software Engineering
  • Theory of Computation
  • About us
  • Contact Us
  • Privacy Policy
  • DMCA Policy
  • Terms and Conditions
  • Home
  • IT
  • Data Structures
  • Insertion Sort(Sorting) — Data Structures
  • Insertion Sort
  • Data Structures

Insertion Sort(Sorting) — Data Structures

examhopeinfo@gmail.com November 12, 2025 2 minutes read
Insertion Sort

Insertion Sort

🌟 What Is Insertion Sort?

Insertion Sort is a simple algorithm that builds the sorted list one element at a time.
It starts from the second element and compares it backward with the elements before it, placing it in the correct spot — just like sorting cards in your hand.

It’s great for small lists or for lists that are already almost sorted.


🧩 How It Works — Step-by-Step

Let’s take an example array:

[5, 3, 4, 1, 2]

Step 1: Start with the first element

  • The first element (5) is considered sorted by itself.

Step 2: Take the next element (3)

  • Compare 3 with 5.
  • Since 3 < 5, move 5 one position to the right and insert 3 before it.
[3, 5, 4, 1, 2]

Step 3: Next, take 4

  • Compare 4 with 5 → 4 < 5 → shift 5 right.
  • Compare 4 with 3 → 4 > 3 → stop.
  • Insert 4 after 3.
[3, 4, 5, 1, 2]

Step 4: Take 1

  • Compare 1 with 5, 4, 3 → all are greater than 1, so shift them right.
  • Insert 1 at the beginning.
[1, 3, 4, 5, 2]

Step 5: Finally, take 2

  • Compare 2 with 5, 4, 3 → all greater, shift them right.
  • Insert 2 after 1.
[1, 2, 3, 4, 5]

And now the list is sorted! 🎉


🖼️ Diagram: Insertion Sort in Action

Initial Array: [5, 3, 4, 1, 2]

Step 1: [3, 5, 4, 1, 2]   → Insert 3 before 5
Step 2: [3, 4, 5, 1, 2]   → Insert 4 between 3 and 5
Step 3: [1, 3, 4, 5, 2]   → Insert 1 at the beginning
Step 4: [1, 2, 3, 4, 5]   → Insert 2 between 1 and 3

Here’s a simple visual:

Unsorted → [5, 3, 4, 1, 2]
               ↓
Compare and Insert 3 → [3, 5]
Compare and Insert 4 → [3, 4, 5]
Compare and Insert 1 → [1, 3, 4, 5]
Compare and Insert 2 → [1, 2, 3, 4, 5]
Sorted! ✅

🧠 Real-Life Analogy

Imagine organizing a line of students by height.
You start with one student (already “sorted”).
Then each new student joins the line, and you look for where they belong — maybe between two taller ones or at the very start.
Everyone shifts slightly to make space.
That’s how Insertion Sort “inserts” each new item in order.


💡 Algorithm (Plain English)

for i = 1 to n-1:
    key = arr[i]
    j = i - 1
    while j >= 0 and arr[j] > key:
        arr[j + 1] = arr[j]   // Shift larger elements
        j = j - 1
    arr[j + 1] = key          // Place key at the right spot

⏱️ Time and Space Complexity

CaseTime Complexity
Best Case (Already Sorted)O(n)
Average CaseO(n²)
Worst Case (Reverse Sorted)O(n²)
Space ComplexityO(1)

It’s not the fastest, but for small or nearly-sorted data, it’s very efficient and easy to code.


💪 Advantages

  • Very simple and easy to understand.
  • Works well for small or nearly sorted datasets.
  • It’s an in-place algorithm (no extra memory needed).
  • Stable sort — equal elements stay in their original order.

⚠️ Disadvantages

  • Slow for large datasets (because of O(n²) time).
  • Lots of shifting when elements are in reverse order.

🧩 Quick Summary

FeatureDescription
TypeComparison-based
Time ComplexityO(n²)
Space ComplexityO(1)
Sorting TypeIn-place
StabilityStable
Best Used ForSmall or nearly sorted data

About the Author

examhopeinfo@gmail.com

Administrator

Visit Website View All Posts

Post navigation

Previous: Heap Sort(Sorting) — Data Structurea
Next: Shell Sort — Sorting

Related News

Linked Representation
  • Linked Representation of a Graph
  • Data Structures

Linked Representation of a Graph

examhopeinfo@gmail.com November 14, 2025 0
Path Matrix
  • Path Matrix
  • Data Structures

Path Matrix

examhopeinfo@gmail.com November 14, 2025 0
Adjacency Matrix
  • Adjacency Matrix
  • Data Structures

Adjacency Matrix

examhopeinfo@gmail.com November 14, 2025 0

Recent Posts

  • Vivo X200: जाने कितनी कम कीमत पर मिल रहा ये 9400 मिडिया टेक प्रोसेसर वाला स्मार्टफोन
  • Samsung Galaxy S25 Plus पर मिल रही भारी छूट ,जाने सेल प्राइस
  • AI के इस ज़माने में कैसे बिजली बचा रहे हैं यह स्मार्ट प्लग?
  • क्या है यह GhostPairing Scam और बिना पासवर्ड और सिम के क्यों हो रहा है व्हाट्सप्प अकाउंट हैक
  • Leica कैमरे के साथ जल्द लॉन्च हो सकता है Xiaomi Ultra 17

At ExamHope, we understand that preparing for exams can be challenging, overwhelming, and sometimes stressful. That’s why we are dedicated to providing high-quality educational resources, tips, and guidance to help students and aspirants achieve their goals with confidence. Whether you are preparing for competitive exams, school tests, or professional certifications, ExamHope is here to make your learning journey smarter, easier, and more effective.

Quick links

  • About us
  • Contact Us
  • Privacy Policy
  • Terms and Conditions
  • Disclaimer
  • DMCA Policy

Category

  • Computer Network
  • Computer Organization and Architecture
  • Data Structures
  • C Language
  • Theory of Computation
  • Database

You may have missed

Vivo X200 Price Drop
  • IT
  • Current Affairs
  • Tech News

Vivo X200: जाने कितनी कम कीमत पर मिल रहा ये 9400 मिडिया टेक प्रोसेसर वाला स्मार्टफोन

examhopeinfo@gmail.com December 23, 2025 0
Samsung Galaxy S25 Plus
  • IT
  • Current Affairs
  • Tech News

Samsung Galaxy S25 Plus पर मिल रही भारी छूट ,जाने सेल प्राइस

examhopeinfo@gmail.com December 22, 2025 0
Electricity bill saving Smart Plug
  • IT
  • Current Affairs
  • Tech News

AI के इस ज़माने में कैसे बिजली बचा रहे हैं यह स्मार्ट प्लग?

examhopeinfo@gmail.com December 21, 2025 0
Ghost Pairing Scam on Whatsapp
  • IT
  • Current Affairs
  • Tech News

क्या है यह GhostPairing Scam और बिना पासवर्ड और सिम के क्यों हो रहा है व्हाट्सप्प अकाउंट हैक

examhopeinfo@gmail.com December 21, 2025 0
Copyright © All rights reserved for ExamHope. | MoreNews by AF themes.
Go to mobile version