Skip to content

Instantly share code, notes, and snippets.

@CSEmbree
Created October 13, 2013 17:15
Show Gist options
  • Save CSEmbree/6964775 to your computer and use it in GitHub Desktop.
Save CSEmbree/6964775 to your computer and use it in GitHub Desktop.
Insertion Sort in C
#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
}
}
#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