Sorting algorithms/Insertion sort: Difference between revisions

→‎{{header|C}}: use t for temporary variable to fit in with the other C examples
(→‎{{header|C}}: use t for temporary variable to fit in with the other C examples)
Line 226:
 
=={{header|C}}==
<lang c>#include <stddefstdio.h>
 
static void insertion_sort (int *a, const size_tint n) {
size_tint i, j, t;
int value;
for (i = 1; i < n; i++) {
valuet = a[i];
for (j = i; j > 0 && valuet < a[j - 1]; j--) {
a[j] = a[j - 1];
}
a[j] = valuet;
}
}
 
void print_array (int *a, int n) {
int main(void) {
int valuei;
for (i = 0; i < n; i++) {
printf(" %d", a[i]);
}
printf("\n");
}
 
int main (void) {
int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
insertion_sort(a,int n = sizeof a / sizeof a[0]);
print_array(a, n);
insertion_sort(a, n);
print_array(a, n);
return 0;
}</lang>
{{out}}
<pre>
4 65 2 -31 0 99 2 83 782 1
-31 0 1 2 2 4 65 83 99 782
</pre>
 
=={{header|C++}}==
Anonymous user