Created
October 13, 2013 17:15
-
-
Save CSEmbree/6964775 to your computer and use it in GitHub Desktop.
Insertion Sort in 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
#include "insert.h" | |
void InsertionSort(int *list, int listLength) | |
{ | |
int i, j, temp; | |
for( i = 0 ; i < listLength ; ++i ) | |
{ | |
temp = list[i]; // store the next element to sort | |
j = i; | |
//step back through the sorted list to find correct placement of current element | |
while( j > 0 && list[j-1] > temp) | |
{ | |
list[j] = list[j-1]; //shift each element up one along the way | |
j--; | |
} | |
list[j] = temp; // place element in it's sorted position | |
} | |
} |
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
#ifndef INSERT_H | |
#define INSERT_H | |
#include <stdlib.h> | |
#include <stdio.h> | |
//insertion sort method | |
void InsertionSort(int *list, int listLength); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment