Write a C Program for Insertion Sort

Write a C Program for Insertion Sort


 #include <stdio.h>
 int main(){
  
   int i, j, count, temp, number[25];

   printf("How many numbers u are going to enter?: ");
   scanf("%d",&count);

   printf("Enter %d elements: ", count);
   // This loop would store the input numbers in array
   for(i=0;i<count;i++)
      scanf("%d",&number[i]);

   // Implementation of insertion sort algorithm
   for(i=1;i<count;i++){
      temp=number[i];
      j=i-1;
      while((temp<number[j])&&(j>=0)){
         number[j+1]=number[j];
         j=j-1;
      }
      number[j+1]=temp;
   }

   printf("Order of Sorted elements: ");
   for(i=0;i<count;i++)
      printf(" %d",number[i]);

   return 0;
 }
          

Output:

 How many numbers u are going to enter?: 6
 Enter 6 elements: 1
 3
 5
 6
 4
 9
 Order of Sorted elements:  1 3 4 5 6 9
 --------------------------------