Q:

C Program for insertion sort

0

C Program for insertion sort

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h> 

int main()

{

     int i, j, num, temp, arr[1500];

 

     printf("Enter number of elements\n");

     scanf("%d", &num);

 

     printf("Enter %d integers\n", num);

 

     for (i = 0; i < num; i++)

     {

          scanf("%d", &arr[i]);

     }

 

     for (i = 1 ; i <= num - 1; i++)

     {

           j = i;

           while ( j > 0 && arr[j] < arr[j-1])

           {

                  temp = arr[j];

                  arr[j] = arr[j-1];

                  arr[j-1] = temp;

                  j--;

           }

     }

 

     printf("Insertion sorting in ascending order:\n");

 

     for (i = 0; i <= num - 1; i++)

     {

           printf("%d\n", arr[i]);

     }

 

     return 0;

}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now