Insertion Sort in C|Learnerspage

0
65
Insersion Sort

#include<stdio.h>
#include<conio.h>
void main(){
int i, j, count, temp, number[25];
printf(“How many numbers you 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]);
// 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 are: “);
for(i=0;i<count;i++)
{
printf(” %d”,number[i]);
}
getch();
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here