Skip to content

Instantly share code, notes, and snippets.

@alimranahmed
Created February 10, 2016 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alimranahmed/120c9126ea38db82cca5 to your computer and use it in GitHub Desktop.
Save alimranahmed/120c9126ea38db82cca5 to your computer and use it in GitHub Desktop.
Implementation of insertion sort algorithm using C
/**
* Created by Al- Imran Ahmed
* Sorting an array using insertion sort algorithm
* @param int a[] is an array to be sorted
* @param int size is the size of the array
*/
void insertionSort(int a[], int size){
for(int i = 1; i < size; i++){
for(int j = i; j >= 0; j--){
if(a[j] < a[j-1]){
int temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
}
}
}
}
#include<stdio.h>
#include "insertionSort.c"
/**
* Created by Al- Imran Ahmed (-
* Client program to test insertion sort algoritm's implementation
*/
int main(){
int a[10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
insertionSort(a, 10);
for(int i = 0; i < 10; i++){
printf("%d ",a[i]);
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment