bidirectional bubble sort in javascript....again

please check my bidirectional bubble sort in javascript

Page 1 of 1

2 Replies - 713 Views - Last Post: 30 October 2008 - 04:40 PM

#1 student_br  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-October 08

bidirectional bubble sort in javascript....again

Posted 30 October 2008 - 03:31 PM

<HTML>
</head>
<script>

function biDirectional(arrayName,length) 
	{
		if(var i < (length-1))
		{
			if(var j < length)
			{	
				for(i=0; i<=(length-1);i++)
				{
					for(j=i+1; j<=length; j++)
					{
						if(arrayName[j] <arrayName[i]) 
						{
							var dummy=arrayName[i];
							arrayName[i]=arrayName[j]
							arrayName[j]=dummy;
						}
					}			
		
				}
			}
		}
		if(i = (length-1))
		{
			if(j = length)
			{
				for(i=(length-1); i>=0;i--)
				{
					for(j=(length); j>=1; j--)
					{
						if(arrayName[j] <arrayName[i]) 
						{
							var dummy=arrayName[i];
							arrayName[i]=arrayName[j]
							arrayName[j]=dummy;
						}
					}			
		
				}
			}
		}
	}

var myArray= new Array(5,2,7,9,1);
var myArrayLength=5;
document.write('Unsorted: ',myArray);
myBubbleSort(myArray,myArrayLength);
document.write('------------>Sorted: ',myArray);

</script>
</head>
</HTML>



i tried to make a bidirectional bubblesort by myself and this is what i got.
it says that there is a syntax error on the line with the code:

if(var i < (length-1))

but i really dont know why

is it really a syntax error, or does this code simply not work??

please help!!

Is This A Good Question/Topic? 0
  • +

Replies To: bidirectional bubble sort in javascript....again

#2 mocker  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 50
  • View blog
  • Posts: 466
  • Joined: 14-October 07

Re: bidirectional bubble sort in javascript....again

Posted 30 October 2008 - 03:42 PM

what are you trying to do with if(var i < (length-1)) ? The comparision makes no sense. Putting "var i" in there means you are declaring a new variable, i, which by default is undefined and comparing this new undefined variable to something else.

If I is already declared and assigned somewhere, drop the "var" bit.

I have to ask.. did you just copy and paste this code? Most of your if's don't make any sense. You have to declare a variable BEFORE you use, not after. You don't have i or j declared anywhere before you start trying to compare them to something else.
Was This Post Helpful? 0
  • +
  • -

#3 student_br  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-October 08

Re: bidirectional bubble sort in javascript....again

Posted 30 October 2008 - 04:40 PM

mocker
thanx for ur reply.

what i had the i and the j declared as a variable in the for loop bracket(the initialization part) like this:

for(var i=0;.....)

but since i thought that i should use the "i" before that in the if statement , i tried to declare what "i" is in the if statement.

well i guess that that is one of my problems.

should i declare what "i" and "j" is like this?

function bidirectional()
{
var i=0;
var j=i+1;
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1