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> |
|||
size_t i, j; |
|||
⚫ | |||
int |
int value; |
||
for (i = 1; i < n; i++) { |
for (i = 1; i < n; i++) { |
||
value = a[i]; |
|||
for (j = i; j > 0 && |
for (j = i; j > 0 && value < a[j - 1]; j--) { |
||
a[j] = a[j - 1]; |
a[j] = a[j - 1]; |
||
} |
} |
||
a[j] = |
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}; |
||
size_t n = sizeof a / sizeof a[0]; |
|||
insertion_sort(a, n); |
insertion_sort(a, n); |
||
return 0; |
return 0; |