The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Javascript Bubble Sort

Hey guys, OK first of all this IS an assignment question so although I really appreciate your help please try to point me in the right direction as I don't want to get into trouble!

I am trying to write a bubblesort function and test, so far I have constructed the following but it is not working when run in a browser, would anyone mind having a quick read to see if im missing something obvious? thanks!

HTML Code:

<HTML><HEAD><TITLE>
A program to sort an array using a bubble sort
</TITLE><SCRIPT>
/*
A function to sort an array
Function takes an array of numbers as an argument.
Function returns a new array with the same
elements as the argument array, sorted
into ascending order
*/
function bubbleSort(arrayToSort)
{
// declare and initialise a variable to hold the length of the argument array
var length = arrayToSort.length;
//declare an array to be returned by the function
var returnArray = new Array(length);
//copy each element of the argument array to the return array
for (var i = 0; i < length; i = i + 1)
{
returnArray[i] = arrayToSort[i];
}
// PLACE YOUR CODE FOR THE FUNCTION HERE
var counter, value, tempStore;
/*the counter sets the number of passes using the total length
*/of the array, this way it can deal with an array of any length
for (counter = 0; counter < returnArray.length; counter++)
{
//here the starting position of the array is set
for (value = 0; value < (returnArray.length-1); value++;
{
if (returnArray[value) > returnArray[value+1])
{
/*the value to be replaced is put in a temp holder
*/to prevent the value from bing lost
holder = returnArray[returnArray+1];
returnArray[value+1] = returnArray[value];
returnArray[value] = holder;
}
}
return returnArray;
}
/* a function for testing the bubbleSort() function.
Function assigns an array to a variable
Displays elements of unsorted array in order
Invokes bubbleSort() function with the array as the argument
Displays elements of sorted array in order
Function takes no arguments.
Function returns no value.
*/
function bubbleTest()
{
var unsortedArray;
var sortedArray;
// the array of values to be sorted
unsortedArray = [9,7,2,10,1,4,8,6,5,3];
// PLACE YOUR FUNCTION CODE HERE
document.write('Unsorted Array: ' + unsortedArray + 'BR');
arrayToSort = unsortedArray;
bubbleSort();
document.write('Sorted Array: ' returnArray);
}
// invoke bubbleTest() to test the bubbleSort() function
bubbleTest();
</SCRIPT></HEAD><BODY></BODY></HTML>

Re: Javascript Bubble Sort

Originally Posted by Fidelacchius

HTML Code:

// PLACE YOUR CODE FOR THE FUNCTION HERE
var counter, value, tempStore;
/*the counter sets the number of passes using the total length
*/of the array, this way it can deal with an array of any length
for (counter = 0; counter < returnArray.length; counter++)
{
//here the starting position of the array is set
for (value = 0; value < (returnArray.length-1); value++;
{
if (returnArray[value[COLOR="Red"][B])[/B][/COLOR] > returnArray[value+1])
{
/*the value to be replaced is put in a temp holder
*/to prevent the value from bing lost
holder = returnArray[returnArray+1];
returnArray[value+1] = returnArray[value];
returnArray[value] = holder;
}
}
return returnArray;
}

Re: Javascript Bubble Sort

OK guys thx to Nomisco I have rejigged my code to get rid of all the schoolboy errors (and there were LOADS) never knew firefox was so helpful

so to Nomisco

my code now reads as follows:
getting an error in firefox that states arrayToSort is undefined... thought I had done that? any advice as to what its saying?

<HTML>
<HEAD>
<TITLE>
A program to sort an array using a bubble sort
</TITLE>
<SCRIPT language = "JavaScript">

/*
A function to sort an array
Function takes an array of numbers as an argument.
Function returns a new array with the same
elements as the argument array, sorted
into ascending order
*/
function bubbleSort(arrayToSort)
{
// declare and initialise a variable to hold the length of the argument array
var length = arrayToSort.length;

//declare an array to be returned by the function
var returnArray = new Array(length);
//copy each element of the argument array to the return array
for (var i = 0; i < length; i = i + 1)
{
returnArray[i] = arrayToSort[i];
}

// PLACE YOUR CODE FOR THE FUNCTION HERE

var counter, value, tempStore;
/*the counter sets the number of passes using the total length
of the array, this way it can deal with an array of any length
*/
for (counter = 0; counter < length; counter + 1)
{
//here the starting position of the array is set
for (value = 0; value < (returnArray.length-1); value++);
{
if (returnArray[value] > returnArray[value+1])
{
/*the value to be replaced is put in a temp holder
to prevent the value from bing lost
*/
holder = returnArray[value+1];
returnArray[value+1] = returnArray[value];
returnArray[value] = holder;
}
}
}
return returnArray;
}

/* a function for testing the bubbleSort() function.
Function assigns an array to a variable
Displays elements of unsorted array in order
Invokes bubbleSort() function with the array as the argument
Displays elements of sorted array in order
Function takes no arguments.
Function returns no value.
*/
function bubbleTest()
{
var unsortedArray;
var sortedArray;

// the array of values to be sorted
unsortedArray = [9,7,2,10,1,4,8,6,5,3];

Re: Javascript Bubble Sort

OK, check these things - I won't directly give you the answer

1. Near the bottom you have 'length = unsortedArray' why?

2. The line document.write('Sorted Array: ' + returnArray); is wrong, but, I made the same error so you can be forgiven. It should be in the form document.write('Sorted Array: ' function(argument)); Just think about it for a moment...

3. You're cutting it close!

4. arrayToSort is the argument for the function. It is not a variable or an array per se

Re: Javascript Bubble Sort

Ok I think I am getting close, my thinking behind the length = unsortedArray' thing was that the bubbleTest function needed to tell the bubbleSort function that the values within unsortedArray were to be used by 'length'. Is this the wrong thinking or am I just not doing it right?

the end of my code now looks like this;
function bubbleTest()
{
var unsortedArray;
var sortedArray;

// the array of values to be sorted
unsortedArray = [9,7,2,10,1,4,8,6,5,3];

Re: Javascript Bubble Sort

For the life of me I am not seeing this even though the hints are literally poking me in the eyes... when I have declared the variable unsorted and sorted do they need to be inside or outside the function. Cant believe im being this thick sorry and thanks for your patience/help

Re: Javascript Bubble Sort

yes and no (sorry)... Yes because it makes perfect sense I gave that a go after your advice and I tried it... No because everytime I run it with those changes firefox crashes.... (facepalm)

heres my code after I tried the changes...

<HTML>
<HEAD>
<TITLE>
A program to sort an array using a bubble sort
</TITLE>
<SCRIPT language = "JavaScript">

/*
A function to sort an array
Function takes an array of numbers as an argument.
Function returns a new array with the same
elements as the argument array, sorted
into ascending order
*/
function bubbleSort(arrayToSort)
{
// declare and initialise a variable to hold the length of the argument array
var length = arrayToSort.length;

//declare an array to be returned by the function
var returnArray = new Array(length);
//copy each element of the argument array to the return array
for (var i = 0; i < length; i = i + 1)
{
returnArray[i] = arrayToSort[i];
}

// PLACE YOUR CODE FOR THE FUNCTION HERE

var counter, value, tempStore;
/*the counter sets the number of passes using the total length
of the array, this way it can deal with an array of any length
*/
for (counter = 0; counter < length; counter + 1)
{
//here the starting position of the array is set
for (value = 0; value < (returnArray.length-1); value++);
{
if (returnArray[value] > returnArray[value+1])
{
/*the value to be replaced is put in a temp holder
to prevent the value from bing lost
*/
holder = returnArray[value+1];
returnArray[value+1] = returnArray[value];
returnArray[value] = holder;
}
}
}
return returnArray;
}

/* a function for testing the bubbleSort() function.
Function assigns an array to a variable
Displays elements of unsorted array in order
Invokes bubbleSort() function with the array as the argument
Displays elements of sorted array in order
Function takes no arguments.
Function returns no value.
*/

function bubbleTest()
{
var unsortedArray;
var sortedArray;

// the array of values to be sorted
unsortedArray = [9,7,2,10,1,4,8,6,5,3];

Re: Javascript Bubble Sort

Thanks mate, changed it and the program runs now HOHOO!! Only issue is that the function must not be working correctly as it doesn't sort [despair]. However I have noticed that for some reason in the bubbleSort function I was using a var called tempStore but using holder instead which I didn't define. Have changed that now and am looking for other reasons why it doesn't sort! I know I'm cutting this close been seriously ill for past 3 weeks . Thats why it looks like an all nighter for me

var counter, value, tempStore;
/*the counter sets the number of passes using the total length
of the array, this way it can deal with an array of any length
*/
for (counter = 0; counter < length; counter + 1)
{
//here the starting position of the array is set
for (value = 0; value < (returnArray.length-1); value++);
{
if (returnArray[value] > returnArray[value+1])
{
/*the value to be replaced is put in a temp holder
to prevent the value from bing lost
*/holder = returnArray[value+1];
returnArray[value+1] = returnArray[value];
returnArray[value] = holder;

Re: Javascript Bubble Sort

Finally got it to work!!! COMON YOU BEAUTY!! Eended up going through function and killing half my stupid variables, think I massively overcomplicated my function but got there in the end! Thanks for all your help MUCH appreciated, couldn't have figured it out without you!! good luck on the assignment results and on the test!!