Skip to content

Instantly share code, notes, and snippets.

@nerohoop
Created January 11, 2018 01:22
Show Gist options
  • Save nerohoop/d4c2024eb0b4e1232e88a88dea4b8f75 to your computer and use it in GitHub Desktop.
Save nerohoop/d4c2024eb0b4e1232e88a88dea4b8f75 to your computer and use it in GitHub Desktop.
void Sort::insertionSort(int *arr, int size) {
int key = 0;
for (int i=1; i < size; i++) {
key = arr[i];
// Move elements of arr[0..i-1], that are greater than key,
// to one position ahead of their current position.
int j = i-1;
while (j>=0 && arr[j] > key) {
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment