Sorting algorithms/Insertion sort: Difference between revisions

Content added Content deleted
(→‎{{header|C}}: make function declaration valid)
(→‎{{header|C}}: size should be an unsigned int, size_t is even better because it uses the proper size for your architecture (32-bit on x86, 64-bit on x86_64))
Line 190: Line 190:


=={{header|C}}==
=={{header|C}}==
<lang c>void insertion_sort(int *a, const size_t n) {
<lang c>
size_t i, j;
void insertion_sort(int *a, int n) {
int i, j, k;
int value;
for (i = 1; i < n; i++) {
for (i = 1; i < n; i++) {
k = a[i];
value = a[i];
for (j = i; j > 0 && k < a[j - 1]; j--) {
for (j = i; j > 0 && value < a[j - 1]; j--) {
a[j] = a[j - 1];
a[j] = a[j - 1];
}
}
a[j] = k;
a[j] = value;
}
}
}
}
Line 204: Line 204:
int main(void) {
int main(void) {
int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
int n = sizeof a / sizeof a[0];
size_t n = sizeof a / sizeof a[0];
insertion_sort(a, n);
insertion_sort(a, n);
return 0;
return 0;