• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C » Insertion Sort in C
Next →
← Prev

What is Insertion Sort in C with Example

By Dinesh Thakur

Insertion Sort in C is a comparison-based sorting algorithm that arranges numbers of an array in order. It is stable, adaptive, in-place and incremental in nature. The insertion sort is useful for sorting a small set of data. It sorts smaller arrays faster than any other sorting algorithm. But, it is impractical to sort large arrays.

Note : We use two basic loops, an outer for loop and an inner while loop to perform an insertion sort.

We’ll be covering the following topics in this tutorial:

  •  What is insertion sort?
  • How insertion sort works in C
  • The time complexity of insertion sort
  • Insertion Sort Algorithm
  • Insertion Sort Program in C

 What is insertion sort?

Insertion sort is a sorting algorithm that places an unsorted element at its correct position in each iteration. During insertion sort, the relative order of elements not changed. Therefore, it is a stable sorting algorithm. And insertion sort requires only O(1) of additional memory space. Consequently, it sorts in-place.

Insertion Sort works in the same way as we set up a card deck. We also introduced it in the following Program code.

insertion sort in cHow insertion sort works in C

In every iteration, it compares the current element with all the values in the sorted array. If the current element is higher than the array element, it leaves the element and iterates into the next array element. But, if the current element is smaller than the array element, it shifts the remaining part of the element in the array by one place and makes space for the present in the sorted array.

The way insertion sort takes one input elements at a time, iterates through the sorted sub-array. With every iteration, it inserts one element at its correct position—that why the algorithm is known as Insertion sort.

Insertion sort is an example of an incremental algorithm.

In the incremental algorithms, the complicated structure on n items is built by first building it on n − 1 item. And then, we make the necessary changes to fix things in adding the last item. Insertion sort builds the sorted sequence one element at a time. Therefore, it is an example of an incremental algorithm.

The time complexity of insertion sort

The complexity of an algorithm is the measure of the number of comparisons made in the algorithm—an algorithm’s complexity measure for the worst case, best case, and the average case.

Worst-Case Complexity (Reverse sorted array): O(n2)

In the case of insertion, the worst case occurs when the array is already in reverse order. In the first pass, it compares a maximum of one element. The second pass compares a maximum of two elements and so on, up to a maximum of (n-1) comparisons in the last pass. Hence, the total number of comparisons made will be O(n2).

• If an array is in ascending order, and you want to sort it in descending order. In this situation, the worst-case complexity occurs.
• Each element has to compare with every other element, so the number of comparisons made for every nth element (n-1).
• The worst-case for an insertion sorting algorithm is a reverse ordered array, and its time is quadratic.
• Thus, the total number of comparisons = n*(n-1) ~ n2

For example:
 If the input array has elements as {9, 8, 7, 4, 2}
 then the output array will have data as {2, 4, 7, 8, 9}

Best Case Complexity: O(n)

The best-case running time of the insertion sort is O(n). The best-case occurs when the array is already sorted. We have to make a minimum number of swaps. In that case, only one comparison is made on each pass, so that the time required is O(n).

For example:
 If the input array has data as {-4, 41, 76} 
 then the expected output array will have data as {-4, 41, 76}

Average Case Complexity: O(n2)
It occurs when the elements of an array are in random distribution.

For example:
If the input array is {5, 7, 2, 3, 6, 4}
the expected output array will have data as {2, 3, 4, 5, 6, 7}

Space Complexity

Space complexity is O(1) because an extra variable key is used.

Insertion Sort Algorithm

insertionSort(array)
 mark first element as sorted
 for each unsorted element X
 'extract' the element X
  for j <- lastSortedIndex down to 0
   if current element j > X
    move sorted element to the right by 1
    break loop and insert X here
    end insertionSort

Insertion Sort Program in C

/* C Program for Insertion Sort */
#include<stdio.h>
int main() {
 int a[100], n, i, j, element; 
 printf("\nEnter the total Number of Elements : ");
 scanf("%d", &n);
 printf("\nEnter the Array Elements : ");
 for(i = 0; i < n; i++)
  scanf("%d", &a[i]);
  for(i = 1; i <= n - 1; i++) {
   for(j = i; j > 0 && a[j - 1] > a[j]; j--) {
     element = a[j];
     a[j] = a[j - 1];
     a[j - 1] = element;
   }
}
  printf("\n Insertion Sort Result : ");
  for(i = 0; i < n; i++) {
    printf(" %d \t", a[i]);
  }
  printf("\n");
  return 0;
}

insertion sort program in c

You’ll also like:

  1. Array C++ Insertion Sort
  2. C Program sorting of an int array using Insertion Method
  3. What is bubble sort in C with example?
  4. Selection Sort in C
  5. Merge Sort in C
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C Programming

C Programming Tutorials

  • C - History
  • C - Anatomy
  • C - Constants
  • C - Identifiers
  • C - Data Types
  • C - Libraries File
  • C - Header Files
  • C - Basic Language
  • C - Data Types Sizes
  • C - Header Files Importance
  • C - Escape Sequences
  • C - Main() Purpose
  • C - Program Procedure
  • C - Control Statements
  • C - Enumeration Constant
  • C - Add numbers
  • C - Return Statement
  • C - Avoid Goto
  • C - Command Line Arguments
  • C - Switch Case
  • C - Switch Case Limitations
  • C - getchar() and putchar()
  • C - Iteration Statements
  • C - Pass by Value and Reference
  • C - Structures and Unions
  • C - Structure
  • C - Dynamic Memory
  • C - Fgets and Fputs Functions
  • C - Gets() and Puts() Functions
  • C - Armstrong Number
  • C - Storage Classes
  • C - Fibonacci Series
  • C - Precision Setting
  • C - const Parameters

C - Variable & It's Type

  • C - Variables
  • C - Variable Lifetime
  • C - Static Variable
  • C - Register Variable
  • C - Global Variables
  • C - Auto Variables
  • C - Local Variables

C - Operator & Expressions

  • C - Operator
  • C - Boolean Operators
  • C - Bitwise Operator
  • C - Arithmetic Operators
  • C - Modulus Operator
  • C - Ternary Operator
  • C - Expressions
  • C - Arithmetic Expressions

C - Array

  • C - Arrays
  • C - Array Types
  • C - Array Characteristics
  • C - Static Arrays
  • C - Global Arrays
  • C - 3D Arrays
  • C - Dynamic Arrays
  • C - Pointer to 3D Arrays
  • C - Array Elements Hold
  • C - Arrays as Function Parameters
  • C - Accessing Matrix Elements
  • C - File Handling
  • C - Matrix Multiplication
  • C - Dynamic Memory Allocation

C - Searching & Sorting

  • C - Data Structures
  • C - Linear Search
  • C - Bubble Sort
  • C - Merge Sort
  • C - Linked List
  • C - Insertion Sort
  • C - Binary Search
  • C - Selection Sort
  • C - Quick Sort

C - Functions

  • C - Functions
  • C - Functions Advantages
  • C - Void Functions
  • C - Function Call
  • C - Default Return Value
  • C - String functions

C - Pointer

  • C - Pointers
  • C - Type Casting Of Pointers
  • C - Pointer Advantages
  • C - Pointers Initialization
  • C - Vectors and Pointers

C - Differences

  • C - C Vs C++
  • C - Formal Args. Vs Actual Args.
  • C - Keywords Vs Identifiers
  • C - Strings Vs Character Arrays
  • C - Address Vs Dereference Operator
  • C - Goto Vs longjmp
  • C - Declaring Vs Defining Variable
  • C - String Vs Array
  • C - Call by Value Vs Reference
  • C - Structure Vs Union
  • C - For Vs While loops
  • C - Compiler Vs Interpreter

C - Programs

  • C Program Standard Deviation
  • C Program Calculate Tax
  • C Program Sum Series
  • C Program Merge Arrays
  • C Program Euclid’s Algorithm
  • C Program Print Weekdays
  • C Program Sum of Digits
  • C Program Print a list
  • C Program Print Pythagorean
  • C Program Quiz program
  • C Program Display Table
  • C Program Print Comma-Separated
  • C Program Prints Prime Numbers
  • C Program for Print Integer
  • C Program Count Number
  • C Program Print Color Name
  • C Program Print Odd Numbers
  • C Program Calculate area
  • C Program for a Menu
  • C Program Add Two Vectors
  • C Program Array Addresses
  • C Program Division by Zero Error
  • C Program Compare two Dates
  • C Program Tower of Hanoi
  • C Program return 3 Numbers
  • C Program for Prime Numbers
  • C Program for Factorial
  • C Program for Palindrome

Other Links

  • C Programming - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW