Discussion Forums

New User? Please read instructions!

C program for insertion sort  

  RSS

thiyagarajan.s
Active Member
Joined: 1 month  ago
Posts: 14
27/01/2017 10:04 am  

C program for insertion sort


ReplyQuote
admin
Eminent Member
Joined: 5 years  ago
Posts: 26
17/02/2017 10:05 am  

C program for insertion sort:

 

#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;

}

 

Output:

Please enter the number of elements

4

Enter 4 numbers

2

1

8

4

Insertion sorting in ascending order:

1

2

4

8


ReplyQuote
  
Working

Please Login or Register

Ask your questions or clarify your/others doubts from,

✅ Online Discussion Forums by Fresh2Refresh.com





Share this website !!!
Facebook Twitter Linkedin

Close
We Support JALLIKATTU !!!
We Support JALLIKATTU!
By Fresh2Refresh.com
Show Your Support as well. Thanks!