Skip to content

Instantly share code, notes, and snippets.

@elleryq
Created March 31, 2015 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save elleryq/305b308fc98d72ace9ae to your computer and use it in GitHub Desktop.
Save elleryq/305b308fc98d72ace9ae to your computer and use it in GitHub Desktop.
Insertion Sort in C++
#include <cstdio>
#include <cstdlib>
void insertionSort(int arr[], int length) {
int i, j, tmp;
for (i = 1; i < length; i++) {
j = i;
while (j > 0 && arr[j - 1] > arr[j]) {
tmp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tmp;
j--;
}
}
}
int displayArray( int arr[], int length ) {
printf("{");
for( int i=0; i<length; i++ )
printf("%d, ", arr[i] );
printf("}\n");
}
int main( int argc, char* argv[] )
{
int array[10] = { 2,1,7,4,3,5,9,6,8,0 };
size_t length = sizeof(array)/sizeof(int);
displayArray( array, length );
insertionSort( array, length );
displayArray( array, length );
}
@FDuda354
Copy link

FDuda354 commented Mar 9, 2022

doesn't work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment