Insertion Sort for Nearly Sorted Array

Insertion Sort for Nearly Sorted Array

23 Sep 2025
Beginner
294 Views
4 min read
Learn with an interactive course and practical hands-on labs

Free DSA Online Course with Certification

Insertion Sort for Nearly Sorted Array

The Insertion Sort Algorithm is a simple comparison-based sorting algorithm that builds the sorted array one element at a time by inserting each element into its correct position in the sorted portion of the array. It is particularly efficient for nearly sorted arrays, where elements are close to their final positions, requiring fewer comparisons and shifts. This makes it ideal for scenarios where the input is already partially sorted.

DSA skills can boost your tech salary by 25% in 2025. Enroll in our Free DSA Course with Certificate today!

Example:

  • Input: nums = [2, 1, 3, 5, 4, 6] (nearly sorted)
  • Output: [1, 2, 3, 4, 5, 6]
  • Explanation: The array is sorted in ascending order.

Logic

Enter an array of numbers (comma-separated) to sort it using the Insertion Sort algorithm.

Program (Python - Insertion Sort Implementation)

The following Python code implements the Insertion Sort algorithm with O(n²) time complexity in the worst case, but it performs close to O(n) for nearly sorted arrays, where n is the length of the array.


def insertionSort(nums):
    for i in range(1, len(nums)):
        key = nums[i]
        j = i - 1
        # Shift elements that are greater than key to the right
        while j >= 0 and nums[j] > key:
            nums[j + 1] = nums[j]
            j -= 1
        # Place key in its correct position
        nums[j + 1] = key
    return nums

# Example usage
nums = [2, 1, 3, 5, 4, 6]
print(insertionSort(nums))  # Output: [1, 2, 3, 4, 5, 6]

Output

For the input nums = [2, 1, 3, 5, 4, 6], the output is:


[1, 2, 3, 4, 5, 6]

Explanation:

The nearly sorted array is sorted in ascending order using the insertion sort algorithm.

Full-Stack Java Developers earn up to ₹38 LPA in India’s thriving tech market. Launch your high-paying career with our Full Stack Java Developer course today!

Take our Datastructures skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article
About Author
Amit Kumar (Software Engineer And Author)

Experienced Software Engineer with a demonstrated history of working in the professional training & coaching industry. Skilled in Asp.net MVC, Angular, Language Integrated Query (LINQ), SQL, C++, and HTML. Strong engineering professional graduated from Sathyabama University.
Live Training - Book Free Demo
ASP.NET Core Certification Training
25 Oct
08:00PM - 10:00PM IST
Checkmark Icon
Get Job-Ready
Certification
Advanced Full-Stack .NET Developer with Gen AI Certification Training
25 Oct
08:00PM - 10:00PM IST
Checkmark Icon
Get Job-Ready
Certification
.NET Solution Architect Certification Training
26 Oct
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
.NET Microservices Certification Training
26 Oct
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Advanced Full-Stack Java Developer Certification Training Course
01 Nov
05:30PM - 07:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Accept cookies & close this