Program for Insertion Sort In C

The insertion sort algorithm selects elements one at a time and positions each one in the appropriate position within the sorted list of elements. The same logic is implemented in the following C program.
Before running the program, let’s look at the insertion sort’s steps with the aid of an example.
input components 89 17 8 12 0
Step 1: 89 17 8 12 0 (the bold elements are sorted list and non-bold unsorted list)
Step 2: 17 89 8 12 0 (each element will be removed from unsorted list and placed at the right position in the sorted list)
Step 3: 8 17 89 12 0
Step 4: 8 12 17 89 0
Step 5: 0 8 12 17 89

Implementation of Insertion Sort in C

int main(){

   /* Here i & j for loop counters, temp for swapping,
    * count for total number of elements, number[] to
    * store the input numbers in array. You can increase
    * or decrease the size of number array as per requirement
   int i, j, count, temp, number[25];

   printf("How many numbers u are going to enter?: ");

   printf("Enter %d elements: ", count);
   // This loop would store the input numbers in array

   // Implementation of insertion sort algorithm

   printf("Order of Sorted elements: ");
      printf(" %d",number[i]);

   return 0;

As you can see in the output, after entering 6 integer numbers in a seemingly random manner, the aforementioned C program used the logic of the insertion sort method to sort the numbers in ascending order.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button