Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 969deb551a
Fetching contributors…

Cannot retrieve contributors at this time

19 lines (17 sloc) 0.639 kb
#include "insertionsort.h"
void insertionsort(int * arr, int length)
{
int i;
/* Invariant: the array is sorted below the i'th element */
for (i = 0; i < length; i++) {
int new_elt = arr[i];
int j = i;
/* shift all elements below the new one up until we find the
* right spot to insert the new element */
while (--j >= 0 && arr[j] > new_elt) {
arr[j + 1] = arr[j];
}
/* j is now -1 or the first index s.t. arr[j] <= new_elt */
arr[j + 1] = new_elt;
}
}
Jump to Line
Something went wrong with that request. Please try again.