Skip to content

Instantly share code, notes, and snippets.

@ManiruzzamanAkash
Created February 13, 2017 20:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ManiruzzamanAkash/cc2b1d681f96a8e2dd94abf43442337f to your computer and use it in GitHub Desktop.
Save ManiruzzamanAkash/cc2b1d681f96a8e2dd94abf43442337f to your computer and use it in GitHub Desktop.
Insertion Sort in C language->Insert value in the array and get the sorted array as insertion sort
/**
Insertion Sort in C language
@author: MANIRIZZAMAN AKASH
@description: Insert value in the array and get the sorted array as insertion sort
**/
#include<stdio.h>
#define MAX 100
int main(){
int i =0, j= 0, k = 0,
element = 0,
array[MAX],
totalElements = MAX;
printf("Enter an element [-1 for exit] : ");
scanf("%d", &element);
while(element != -1){
k = i - 1;
while((element < array[k]) && k >= 0){
array[k+1] = array[k];
--k;
}
array[k + 1] = element;
printf("After inserting value in the array :\n");
for(j = 0;j <= i; j++){
printf("%d\t", array[j]);
}
printf("\nEnter another element: ");
scanf("%d", &element);
++i;
}
printf("\n---------------------------\nFinal sorted array is : ");
for(j = 0;j <= i; j++){
printf("%d\t", array[j]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment