Insertion Sort in C

Let us see how to implement Insertion Sort in C Language.

What is Insertion Sort?

The insertion sort is a sorting method that contains two sub arrays, one sorted and one unsorted. The first element from the unsorted sub array is put into its correct position in the sorted sub array in each pass. At the start of the sorting, the first element of the array is considered the sorted array and the rest of the elements form the unsorted array.

Given example explains the steps required in Selection Sort.

The array before sorting starts is:

In the above array the first element i.e. 9 is sorted sub array and the rest of the array is unsorted sub array. 3 is added to its correct position in the sorted sub array. The array obtained is:

Now, 3 and 9 form the sorted sub array. 5 is added to its correct position in the sorted sub array. The array obtained is:

3, 5 and 9 form the sorted sub array. 1 is added to its correct position in the sorted sub array. The array obtained is:

1, 3, 5 and 9 form the sorted sub array. 2 is the only remaining element in the unsorted sub array. It is added to its correct position in the sorted sub array. The final sorted array obtained is:

Source Code

The source code for Selection Sort is as follows:

Output

The output obtained for the above code is:

Insertion Sort in C

Selection Sort in C
Heap Sort in C
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading