Hello everyone,
at first I didn't care about the speed of my bubble sort function - a working program was/is enough ... but today I found out that some newbie was able to write a faster bubble sort function than me. ;) So, I decided to tweak the sourcecode a little bit.
I tried to reach the performance of the bubble sort function (sourcecode still undisclosed) written by one of our tutors at my university. My function is still about 30% slower than his and I don't know why.
Here is my current sourcecode - I hope you can help me to make it faster and find possible bugs (I guess there are some but I am too tired at the moment to think clearly):
Thank you for your help.Code:#define N 10240
...
int main()
{
int array[N];
...
return 0;
}
...
int adv_bubble(int array[])
{
// strange but register makes a difference on cygwin gcc 3.2
register int i, tmp, tmp1, test, *pointer;
counter = N-2;
do
{
test = 1;
i = 0;
pointer = array;
do
{
// new code ~25% faster
if (*pointer > *(pointer+1))
{
tmp = *pointer;
*pointer = *(pointer+1);
*(pointer+1) = tmp;
test = 0;
}
// old code -> slow
/*if (pointer[i] > pointer[i+1])
{
tmp = pointer[i+1];
pointer[i+1] = pointer[i];
pointer[i] = tmp;
test = 1;
}*/
++pointer;
// i++ and counter = N-2 is faster than ++i and counter = N-1
// I guess I am making something wrong here ... though the array is sorted
// correctly ...
}while(i++ < counter);
if(test) return 1;
}while(--counter >= -1);
return 1;
}