Notification texts go here Contact Us Click here

Insertion sorting in Data structure with program .

Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated




INSERTION SORTING 
The idea behind the insertion sort is that first take one element, iterate it through the sorted array. Although it is simple to use, it is not appropriate for large data sets as the time complexity of insertion sort in the average case and worst case is O(n2), where n is the number of items. Insertion sort is less efficient than the other sorting algorithms like heap sort, quick sort, merge sort .


Explanation of the Insertion Sort Algorithm:

The simple steps of achieving the insertion sort are listed as follows -

Step 1 - If the element is the first element, assume that it is already sorted. Return 1.

Step2 - Pick the next element, and store it separately in a key.

Step3 - Now, compare the key with all elements in the sorted array.

Step 4 - If the element in the sorted array is smaller than the current element, then move to the next element. Else, shift greater elements in the array towards the right.

Step 5 - Insert the value.

Step 6 - Repeat until the array is sorted.

Working of Insertion sort Algorithm

Now, let's see the working of the insertion sort Algorithm.

To understand the working of the insertion sort algorithm, let's take an unsorted array. It will be easier to understand the insertion sort via an example.

Let the elements of array are -

Insertion Sort Algorithm

Initially, the first two elements are compared in insertion sort.

Insertion Sort Algorithm

Here, 31 is greater than 12. That means both elements are already in ascending order. So, for now, 12 is stored in a sorted sub-array.

Insertion Sort Algorithm

Now, move to the next two elements and compare them.

Insertion Sort Algorithm

Here, 25 is smaller than 31. So, 31 is not at correct position. Now, swap 31 with 25. Along with swapping, insertion sort will also check it with all elements in the sorted array.

Insertion Sort Algorithm



For now, the sorted array has only one element, i.e. 12. So, 25 is greater than 12. Hence, the sorted array remains sorted after swapping.

Now, two elements in the sorted array are 12 and 25. Move forward to the next elements that are 31 and 8.

Insertion Sort Algorithm

Both 31 and 8 are not sorted. So, swap them.

Insertion Sort Algorithm

After swapping, elements 25 and 8 are unsorted.

Insertion Sort Algorithm

So, swap them.

Insertion Sort Algorithm

Now, elements 12 and 8 are unsorted.

Insertion Sort Algorithm

So, swap them too.

Insertion Sort Algorithm

Now, the sorted array has three items that are 8, 12 and 25. Move to the next items that are 31 and 32.

Insertion Sort Algorithm

Hence, they are already sorted. Now, the sorted array includes 8, 12, 25 and 31.

Insertion Sort Algorithm

Move to the next elements that are 32 and 17.

Insertion Sort Algorithm

17 is smaller than 32. So, swap them.

Insertion Sort Algorithm

Swapping makes 31 and 17 unsorted. So, swap them too.

Insertion Sort Algorithm

Now, swapping makes 25 and 17 unsorted. So, perform swapping again.

Insertion Sort Algorithm

Now, the array is completely sorted


➤ Program for Insertion sorting using C language . 

#include <stdio.h>

void insertionSort(int arr[], int n) {
    int i, j, key;

    // Traverse through all array elements starting from the second element
    for (i = 1; i < n; i++) {
        key = arr[i]; // Element to be inserted into the sorted portion

        // Move elements of the sorted portion that are greater than the key to the right
        j = i - 1;
        while (j >= 0 && key < arr[j]) {
            arr[j + 1] = arr[j];
            j--;
        }

        // Insert the key into its correct position in the sorted portion
        arr[j + 1] = key;
    }
}

int main() {
    int arr[] = {64, 25, 12, 22, 11};
    int n = sizeof(arr) / sizeof(arr[0]);
    int i;

    printf("Unsorted array: ");
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    insertionSort(arr, n);

    printf("Sorted array: ");
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}


OUTPUT :

Unsorted array: 64 25 12 22 11 
Sorted array: 11 12 22 25 64



 Program for Insertion sorting using Python language .

def insertion_sort(arr):
    n = len(arr)
    # Traverse through all array elements starting from the second element
    for i in range(1, n):
        key = arr[i]  # Element to be inserted into the sorted portion
        # Move elements of the sorted portion that are greater than the key to the right
        j = i - 1
        while j >= 0 and key < arr[j]:
            arr[j + 1] = arr[j]
            j -= 1
        # Insert the key into its correct position in the sorted portion
        arr[j + 1] = key
    return arr

arr = [64, 25, 12, 22, 11]
sorted_arr = insertion_sort(arr)
print(sorted_arr)

OUTPUT :

[11, 12, 22, 25, 64]


Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.