Javatpoint Logo
Javatpoint Logo

Insertion Sort in C

Insertion sort is a simple sorting algorithm that iteratively constructs a sorted section of an array one element at a time. It is an in-place comparison-based method with an average time complexity of O(n2).

The array is divided into two halves by the method: sorted and unsorted. The first element of the array is first considered the sorted component, whereas the remaining items are originally considered the unsorted component.

The algorithm subsequently compares each unsorted element to the elements in the sorted segment, beginning at the end, and adjusts the bigger elements one position to the right until the unsorted element is found in the correct location.

After determining the proper location, the unsorted element is placed into the sorted component at that location.

This procedure is repeated until all of the members of the unsorted part have been inserted into the sorted part, resulting in a fully sorted array.

Here is the insertion sort pseudocode:

In this pseudocode, n represents the number of elements in the array, arr represents the array to be sorted, i and j are loop variables, and key is the element to be inserted into the array's sorted section.

Usage:

Insertion sort is useful for sorting small arrays and has decent speed when the input array is already partially sorted. It is frequently used as a building piece in more advanced sorting algorithms such as merge sort and quicksort.

Here's an illustration of insertion sort in C:

Output:

5 6 11 12 13

This code defines the insertionSort function, which takes an integer array arr and its size n as inputs. The function employs a for loop to run through each element of the array, beginning with the second element (since the first is already sorted).

The function stores the value of each element in a variable named key and sets a variable called j to the index of the preceding element. The code then employs a while loop to compare the key to each previous element until the correct position for the key is found.

Each element greater than the key one position is moved to the right, and j is decremented until it reaches the correct position. The key is subsequently inserted in the proper position by assigning it to arr[j + 1].

Finally, the main function uses an example array and its size to run insertionSort, which then produces the sorted array.

Advantages:

  • Easy implementation: Because the approach is simple to build and understand, it is a great option for teaching basic sorting concepts.
  • Because it sorts the array in place, insertion sort does not require any additional memory space.
  • Insertion sort is an adaptive sorting algorithm that works best with partially sorted input arrays.
  • Stable sorting means that the method keeps equal elements in the input array in the same relative order.

Disadvantages:

  • Slow for large arrays: With an average time complexity of O(n2), the algorithm is slow for large arrays.
  • Insertion sort is inefficient for random input arrays because it involves a large number of comparisons and shifts.
  • The algorithm is not suited for parallelization since each iteration is dependent on the outcome of the preceding iteration.

Conclusion:

Insertion sort provides a straightforward and efficient approach for tiny or partially sorted arrays. It is not ideal for huge arrays or random input, but it can be used to construct more complex sorting algorithms. Overall, insertion sort is a solid solution for efficiently sorting small or partially sorted arrays with little memory utilisation.


Next TopicQueue in C





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA