/* Insertion Sorting */
#include <stdio.h>
int main()
{
int n, i, j, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
int a[n];
for(i = 0; i < n; i++){
printf("Enter element %d: ", i+1);
scanf("%d", &a[i]);
}
printf("Array you entered is .....\n");
for(i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nArray in ascending order is .....\n");
for(i = 1; i < n; i++){
temp = a[i];
j = i - 1;
while(j >= 0 && temp <= a[j]){
a[j+1] = a[j];
j--;
}
a[j+1] = temp;
}
for(i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}
Comments
Post a Comment