
-----------------------------------
GoodbyeSoberDay1
Thu Mar 27, 2008 7:26 am

Bubble Sorting Help
-----------------------------------
Hi,

 If anyone could direct me to a good tutorial on how to do bubble sorting or explain to me how to do it that would be great.

Thanks.

-----------------------------------
Tallguy
Thu Mar 27, 2008 7:28 am

Re: Bubble Sorting Help
-----------------------------------
what do you mean by 'bubble sorting'?

-----------------------------------
fishtastic
Thu Mar 27, 2008 7:42 am

RE:Bubble Sorting Help
-----------------------------------
wikipedia is always good. and so is google, it takes you to wikipedia 
http://en.wikipedia.org/wiki/Bubble_sort

-----------------------------------
GoodbyeSoberDay1
Thu Mar 27, 2008 7:49 am


-----------------------------------
In most of the pseudocode i found, it had a variable called swap.... what do I declare this variable as?

procedure bubbleSort 
    for j : 1 .. upper(text) - 1 
        const last := upper(text) - j + 1 
        for k : 1 .. last - 1 
            if text (k) > text (k + 1) then 
                swap (text, k, k + 1) %What does the swap here do? Or what should I call it
            end if 
        end for 
    end for 
end bubbleSort [/syntax]

-----------------------------------
Carey
Thu Mar 27, 2008 8:02 am

RE:Bubble Sorting Help
-----------------------------------
judging by the code it looks like swap is a procedure that switches element k and k+1 in an array called text.

-----------------------------------
GoodbyeSoberDay1
Thu Mar 27, 2008 8:13 am

Re: RE:Bubble Sorting Help
-----------------------------------
judging by the code it looks like swap is a procedure that switches element k and k+1 in an array called text.

Yeah... That doesn't answer much. I meant something like would swap be another procedure or something.

-----------------------------------
Nyrd
Thu Mar 27, 2008 9:09 am

Re: Bubble Sorting Help
-----------------------------------
A swap would look something like this :



var a :int := 4
var b :int := 2
var temp : int   %this is going to be a "holder" variable 

temp := a %temp = 4
a := b       % a=2
b := temp  %b=4


%%Or alternatively, at a more advanced level . . .

var a :int := 4
var b:int := 2

a := a xor b
b := b xor a 
a:= a xor b


 

The second more memory efficient as no third variable has to be declared. Though it only works on integers.
