Insertion Sort In C

Introduction
  • In this blog, I am going to explain how to insert the sort. It is a simple sorting algorithm, which is relatively efficient for a small list.
  • Compared to Bubble sort, Insertion sort is more efficient, though it has more constraints.
Software Requirements
  • Turbo C++ or C. 
Programming
  1. #include < stdio.h >   
  2. int main()  
  3. {  
  4.     int n, array[1000], c, d, t;  
  5.     printf("Enter number of elements\n");  
  6.     scanf("%d", & n);  
  7.     printf("Enter %d integers\n", n);  
  8.     for (c = 0; c < n; c++)  
  9.     {  
  10.         scanf("%d", & array[c]);  
  11.     }  
  12.     for (c = 1; c <= n - 1; c++)  
  13.     {  
  14.         d = c;  
  15.         while (d > 0 && array[d] < array[d - 1])  
  16.         {  
  17.             t = array[d];  
  18.             array[d] = array[d - 1];  
  19.             array[d - 1] = t;  
  20.             d--;  
  21.         }  
  22.     }  
  23.     printf("Sorted list in ascending order:\n");  
  24.     for (c = 0; c <= n - 1; c++)   
  25.     {  
  26.         printf("%d\n", array[c]);  
  27.     }  
  28.     return 0;  
  29. }  
Explanation
  • In the programming mentioned above, it is clearly understood that it can be arranged in an ascending order and it can be more efficient compared to Bubble sort.

     programming

    programming
 
Output

Output

Conclusion:
 Thus, it can be executed and printed successfully. 
Ebook Download
View all
Learn
View all