The following code which implements a simple insertion sort,

yields, when built with Pelles C for Windows V10.00.6,

a) in debug mode: correct results

b) in release mode without any optimization: correct results

c) in release mode with any speed optimization: *INcorrect* results

d) in release mode with any size optimization: correct results

`int main(void)`

{

int a_arr[] = {5, 2, 1};

int i;

int j;

int n;

int temp;

n = sizeof(a_arr) / sizeof(a_arr[0]);

puts("Before sorting");

for (i = 0; i < n; ++i)

printf("%i ", a_arr[i]);

putchar('\n');

/*

insertion sort

compare each element to the right (i) with all elements to the left (j)

*/

for (i = 1; i < n; ++i) // start outer loop with second array element to sort

for (j = 0; j < i; ++j) // inner loop always starts with the first of the already sorted array elements

{

if (a_arr[j] > a_arr[i]) // if already sorted element to the left is greater than the element to sort, ...

{

temp = a_arr[i]; // ... save element to sort, ...

memmove(&a_arr[j + 1], &a_arr[j], (i - j) * sizeof(int)); // ... shift all already sorted greater elements 1 postion to the right, ...

a_arr[j] = temp; // ... insert element to sort to the left of the shifted elements, and ...

break; // ... start over with next element to sort

}

}

puts("After sorting");

for (i = 0; i < n; ++i)

printf("%i ", a_arr[i]);

putchar('\n');

}