Skip to content

Instantly share code, notes, and snippets.

@tacigar
Last active November 5, 2017 03:59
Show Gist options
  • Save tacigar/09e77cbac8e1383ee9beef09fa17ff49 to your computer and use it in GitHub Desktop.
Save tacigar/09e77cbac8e1383ee9beef09fa17ff49 to your computer and use it in GitHub Desktop.
普通の挿入ソートなり.
#include <stdio.h>
void print_array(int * array, int size) {
int i = 0;
printf("{ ");
while (1) {
printf("%d", array[i]);
if (++i >= size) {
printf("}\n");
break;
} else {
printf(",");
}
}
}
void insert_sort(int *array, int size) {
int i;
for (i = 1; i < size; i++) {
int temp = array[i];
if (array[i - 1] > temp) {
int j = i;
do {
array[j] = array[j - 1];
j--;
} while (j > 0 && array[j - 1] > temp);
array[j] = temp;
}
}
}
int main(void) {
int i;
int tests[4][5] = {
{ 1, 2, 3, 4, 5 },
{ 2, 3, 4, 5, 1 },
{ 4, 5, 3, 2, 3 },
{ 5, 4, 3, 1, 1 }
};
for (i = 0; i < 4; i++) {
insert_sort(tests[i], 5);
print_array(tests[i], 5);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment