Sorting algorithms/Insertion sort: Difference between revisions

m
size_t & comment
m (some comments)
m (size_t & comment)
Line 1,074:
<lang c>#include <stdio.h>
 
void insertion_sort(int *a, int nsize_t); {
 
void insertion_sort(int *a, size_t n) {
for(size_t i = 1; i < n; ++i) {
int tmpkey = a[i];
size_t j = i;
while( (j > 0) && tmp(key < a[j - 1]) ) {
a[j] = a[j - 1];
--j;
}
a[j] = tmpkey;
}
}
 
int main (int argc, char** argv) {
int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
 
intconst size_t n = sizeof (a) / sizeof (a[0]) ; // array extent
int i;
 
for (i = 0; i < n; i++)
for printf("%d%s",size_t a[i], i == n0; -i 1< ? "\n"; : " "i++);
printf("%d%s", a[i], (i == (n - 1))? "\n" : " ");
 
insertion_sort(a, n);
 
for (i = 0; i < n; i++)
for printf("%d%s",size_t a[i], i == n0; -i 1< ? "\n"; : " "i++);
printf("%d%s", a[i], (i == (n - 1))? "\n" : " ");
 
return 0;
}