Created
January 9, 2016 21:42
-
-
Save ayushgoel/b365a69b858c1e7a67ee to your computer and use it in GitHub Desktop.
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
// https://www.hackerrank.com/challenges/insertionsort1 | |
#include <stdio.h> | |
#include <string.h> | |
#include <math.h> | |
#include <stdlib.h> | |
#include <assert.h> | |
void print_ar(int size, int *ar) { | |
for (int i = 0; i < size; ++i) { | |
printf("%d ", ar[i]); | |
} | |
printf("\n"); | |
} | |
void insertionSort(int ar_size, int * ar) { | |
int num_to_move = ar[ar_size - 1]; | |
for (int i = ar_size - 1; i >= 0; --i) { | |
if (ar[i - 1] > num_to_move) { | |
ar[i] = ar[i - 1]; | |
print_ar(ar_size, ar); | |
} else { | |
ar[i] = num_to_move; | |
print_ar(ar_size, ar); | |
return; | |
} | |
} | |
} | |
int main(void) { | |
int _ar_size; | |
scanf("%d", &_ar_size); | |
int _ar[_ar_size], _ar_i; | |
for(_ar_i = 0; _ar_i < _ar_size; _ar_i++) { | |
scanf("%d", &_ar[_ar_i]); | |
} | |
insertionSort(_ar_size, _ar); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment