bidirectional bubble sort in javascript

help with bidirectional bubble sort in javascript

Page 1 of 1

3 Replies - 2364 Views - Last Post: 30 October 2008 - 06:03 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

Posted 29 October 2008 - 10:19 PM

i really need help with creating a bidirectioinal bubble sort function in javascript
i searched all over the internet and i can only find it in java, which i have no clue about since i am in grade 10 and i just started this course.

i do understand how it works as well as the advantage over the normal bubble sort, etc.
i just need help writing it in code!!!

i have this as my bubblesort function:


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

		}
	}




i am guessing that i just need to convert that into bidirectional bubble sort, replace it with the normal bubble sort,
and fit it into the whole code.

please teach me how to write the bidirectional bubble sort function!!!!!

Is This A Good Question/Topic? 0
  • +

Replies To: bidirectional bubble sort in javascript

#2 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 828
  • Joined: 25-October 08

Re: bidirectional bubble sort in javascript

Posted 30 October 2008 - 05:03 AM

If you mean by 'bidirectional' the information sorted in ascending and decending order,
just add a flag to your call (like 'up' or 'down') and put these extra lines in your code.

var changeorder = false;
if (direction_flag == 'up') {  // parameter passed to myBubbleSort funciton
  if (arrayName[j] <arrayName[i]) { changeorder = true; } 
}  else {
  if  (arrayName(j] > arrayName[i])  { changeorder = true; }
}
if (changeorder = true)  {  // following could be a separate function if desired.
				var dummy=arrayName[i];
				arrayName[i]=arrayName[j]
				arrayName[j]=dummy;
}


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

Posted 30 October 2008 - 04:32 PM

hey thanx for your reply.

what i mean by bidirectional sort is cocktail sort, where the direction of bubbling keeps switching from right-left to left-right

my code for the bidirectional bubble sort is:

<HTML>
</head>
<script>

function biDirectional(arrayName,length) 
	{
		if(var i = 0)
		{
			if(var j = 1)
			{	
				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]) 
						{
							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 know that there is something wrong on this code because it says that there is a syntax error on the code:

if(i = (length-1))

is it really just a syntax error? or is it just that my whole code does not make any sense?
if it is a syntax error, how do i fix it??

please help!!
Was This Post Helpful? 0
  • +
  • -

#4 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 828
  • Joined: 25-October 08

Re: bidirectional bubble sort in javascript

Posted 30 October 2008 - 06:03 PM

It is a syntax error as it should be

if (i == (length-1))

if that is the intent of your logic
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1