Created
February 10, 2016 11:11
-
-
Save alimranahmed/120c9126ea38db82cca5 to your computer and use it in GitHub Desktop.
Implementation of insertion sort algorithm using C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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