Write a C Code To implement Insertion Sort.
#include <stdio.h> int main() { int n, array[10], i,j,swap; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for (i = 0; i < n; i++) { scanf("%d", &array[i]); } for (i = 1 ; i <= n - 1; i++) { /* i cross over every element in array */ j = i; while ( j > 0 && array[j]<array[j-1] ) { /*compare between j element and j-1 and again tell 0 element */ swap= array[j]; array[j] = array[j-1]; array[j-1] = swap; j--; } } printf("Sorted list in ascending order:\n"); for (i = 0; i <= n - 1; i++) { printf("%d\t", array[i]); } return 0; }
0 Comment to "Insertion Sort"
Post a Comment