Posted: Thu Mar 27, 2008 7:49 am Post subject: (No subject)
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
Posted: Thu Mar 27, 2008 8:02 am Post subject: 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
Posted: Thu Mar 27, 2008 8:13 am Post subject: Re: RE:Bubble Sorting Help
Carey @ Thu Mar 27, 2008 8:02 am wrote:
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
Posted: Thu Mar 27, 2008 9:09 am Post subject: Re: Bubble Sorting Help
A swap would look something like this :
Turing:
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.