Quote (Minkomonster @ Apr 7 2014 10:51pm)
Well you just lost out on my dn8tion
What, I'll just take the i out of the loop or use n to reverse iterate, or increment the array pointer.
It's just a convenience, and the compiler will tell you what you did wrong anyway.
Code
void selection_sort(int* arr, unsigned int n) {
if (n) {
unsigned int i = 1, max = 0;
int temp;
// Could iterate backwards from n but some architectures do worse iterating backwards
// Either way an optimizer will pick up these sorts of simple loops and optimize them
for(; i<n; i++)
max = arr[i] > arr[max] ? i : max;
// Could do xor swap, but this reads cleaner and any optimizer will pick it up and turn it into xor swap anyway
temp = arr[max];
arr[max] = arr[n-1];
arr[n-1] = temp;
selection_sort(arr, n-1);
}
}
This post was edited by poodaH on Apr 7 2014 08:57pm