Add 'PracticingAlgorithms/' from commit '0b723f2ae0a20ab8c90c33db8c1d9d9acd76a86f'
git-subtree-dir: PracticingAlgorithms git-subtree-mainline:0a9428093c
git-subtree-split:0b723f2ae0
This commit is contained in:
8
PracticingAlgorithms/src/Makefile
Normal file
8
PracticingAlgorithms/src/Makefile
Normal file
@@ -0,0 +1,8 @@
|
||||
all: $(BINDIR)/insertion-sort
|
||||
|
||||
|
||||
$(BINDIR)/insertion-sort: insertion-sort.c
|
||||
gcc -o $@ $(CFLAGS) $^
|
||||
|
||||
|
||||
.PHONY: all
|
38
PracticingAlgorithms/src/insertion-sort.c
Normal file
38
PracticingAlgorithms/src/insertion-sort.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void insertion_sort(int * a, int len_a);
|
||||
|
||||
|
||||
int
|
||||
main(int argc, char ** argv)
|
||||
{
|
||||
int i;
|
||||
int l[] = {1, 4, 8, 9, 2, 3};
|
||||
|
||||
insertion_sort(l, 6);
|
||||
|
||||
for (i = 0; i < 6; i++) {
|
||||
printf("%d\n", l[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
insertion_sort(int * a, int len_a)
|
||||
{
|
||||
int i, j, key;
|
||||
|
||||
for (j = 1; j < len_a; j++) {
|
||||
key = a[j];
|
||||
i = j - 1;
|
||||
while ((i >= 0) && (a[i] > key)) {
|
||||
a[i + 1] = a[i];
|
||||
i = i - 1;
|
||||
}
|
||||
a[i + 1] = key;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* vim:filetype=c
|
||||
*/
|
Reference in New Issue
Block a user