Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

XOR bubble sort

Name: Anonymous 2010-05-22 13:14

Is there any useful application of a xor bubble sort as opposed to having spare temp variable?


#include <iostream>

using namespace std;

int main()
{
    int x[5]={1, 5, 2, 4, 3};

    for (int c1=0; c1<5; c1++) {
        cout<<x[c1]<<endl;
    }
    cout<<endl;
    for (int c1=0; c1<6; c1++) {
        for (int c2=c1+1; c2<6; c2++) {
            if (x[c1]>x[c2]) {
                x[c1]=x[c1]^x[c2];
                x[c2]=x[c1]^x[c2];
                x[c1]=x[c1]^x[c2];
            }
        }
    }
    for (int c1=0; c1<5; c1++) {
        cout<<x[c1]<<endl;
    }
    return 0;
}

Name: Anonymous 2010-05-22 13:16

No. The temp variable is typically eliminated by the compiler during its optimization stage. Modern CPUs like x86 have instructions like xchg for switching exchanging/switching data.

Name: Anonymous 2010-05-22 13:33

>>1
No, there is no useful application of a bubble sort.

Name: Anonymous 2010-05-22 13:39

OP doesn't understand the importance of the big-oh.

Name: Anonymous 2010-05-22 13:43

>>2
Modern CPU
x86

Oh IHBT

Name: Anonymous 2010-05-22 14:14

>>2

That sounds reasonable, though reading a bit XCHG also looks to have its downsides.

>>3
>>5

I bet neither of you can reasonably explain yourselves.

>>4

"O"?

Name: >>2 2010-05-22 14:20

>>6
What >>4 means is that bubble sort is an inefficient sort whose only redeeming quality is its ease of implementation. qsort, heap sort, shell sort and so on, perform much better than bubble sort.

Name: Anonymous 2010-05-22 14:28

>>7
You're responding to someone who doesn't optimise his post references and has never heard of big-O notation. You're wasting your time.

Name: Anonymous 2010-05-22 14:30

>>7

However, not every set of numbers that need to be sorted is huge. Assuming the XOR bubble sort I posted was just a standard bubble sort what benefits would be gained from implementing a more complicated algorithm?

Name: >>3 2010-05-22 14:33

>>6
Sure can, there is no reasonable usage of a bubble sort, because virtually EVERY sorting algorithm is better than it. I say virtually every, only because we have stupid stuff like bogosort, which is meant as a joke. The only possible reason for the possible reason for the continued existence of the Bubble sort is as a teaching mechanism, and I would suggest that even then it shouldn't be taught.

Name: Anonymous 2010-05-22 14:35

s/for the possible reason//
That's what happens when I get distracted and don't bother to proof read :(

Name: Anonymous 2010-05-22 14:45

>>9
I can't remember the last time I wrote my own sorting routine. qsort and other sorting routines usually come with the standard library of most languages, and you only have to provide a comparator lambda/function to it (the reason there's usually multiple routines is that for certain data structures switching elements may be more costly or certain distribution of the values may be better handled by other sort algorithms).

Name: Anonymous 2010-05-22 15:17

>>10
with a small enough n, bubble sort can outperform algorithms that better asymptotic performance for arbitrary ns because of its lower constant cost. It might be optimal to (for example) use a bubble sort on merge sort's split arrays once they are sufficiently small, rather than splitting all the way down to n = 1.

Name: Anonymous 2010-05-22 15:23

>>6
>>5

I bet neither of you can reasonably explain yourselves.

x86 is a over 30 years old peace of garbage that has had shit stacked all over it until it became so bloated that, its instruction set looks worse then perl.

besides, its slow and powerhungry.

the only reason people use x86 is for legacy reasons and because it became a pseudostandard.

Name: qwv !jodSh4MSQs 2010-05-22 15:31

@@

Name: green !Tea/kpXOWk 2010-05-22 15:32

west

Name: Anonymous 2010-05-22 15:37

>>13
This sounds like an idea I heard for parallel computing in general. It would probably work.

Name: Anonymous 2010-05-22 15:50

>>13
A decent quicksort implementation has a better constant cost on nearly-sorted arrays than bubblesort. There's definitely value in switching to a different algorithm for different input sizes (most optimised sorting implementations do this, including qsort in any half-way decent libc), but bubblesort is always a terrible choice.

Name: Anonymous 2010-05-23 6:42

Speaking of O(n2) algorithms, selection sort is my favorite. It's easiest algorithm to implement imho.

Name: Anonymous 2010-05-23 9:59

On modern superscalar OOO architectures the XOR trick creates pipeline hazards and should be avoided.

Name: Anonymous 2010-05-23 11:00

AVOID MY ANUS

Name: Anonymous 2010-05-23 17:03

If you use a temp variable you'll hit some memory cell too much and it can end up burned or damaged.

Name: Anonymous 2010-05-23 17:18

>>22
With a sane compiler, I'd imagine that its all handled in the registers.

Name: Anonymous 2010-05-23 17:34

>>23
With a sane compiler, I'd imagine YHBT

Name: Anonymous 2010-05-23 17:40

>>23
x86 only has two registers, so the XOR swap is the only viable option.

Name: Anonymous 2010-05-23 17:50

>>25

          ORLY                

Name: Anonymous 2010-05-23 17:54

>>25
Xchg.

Name: Anonymous 2010-05-23 18:26

>>27
XCHG is just syntactic sugar for the XOR swap.

Name: Anonymous 2010-05-23 18:33

>>28
No, it's not, read your Intel processor manuals.

Name: Anonymous 2010-05-23 19:28

>>29
Thanks to /prog/ I have the complete set!

Name: Anonymous 2010-11-28 13:49

Name: Anonymous 2010-12-06 10:04

Back to /b/, ``GNAA Faggot''

Don't change these.
Name: Email:
Entire Thread Thread List