Re: Javascript Bubble Sort
Quote:
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;
}
I guess you're doing M150 as well?
Re: Javascript Bubble Sort
There's a difference between Java script and Java :P
However, the algorithm is the same. See Bubble sort for the pseudo-code.
Re: Javascript Bubble Sort
yeah this is killing me, I get the concept but just cant see why this bad boy isn't working. am I on the right track or have I gone in completely the wrong direction?:o
Re: Javascript Bubble Sort
Nomisco pointed out an error:
if (returnArray[value)> returnArray[value+1])
Re: Javascript Bubble Sort
Thanks, Iv just caught that one and thanked him/her. still wont run though.... [dispair].
two followup questions....
1. I am on the right path right? im not missing the mark completely am I?
2. Anything else jumping out at you people?
Really appreciate the help! awesome forum cant believe I have only just found it!^:)^
Re: Javascript Bubble Sort
For starters, although not an error, the length of the array (arrayToSort.length) has already been assigned to the variable 'length'.
So, you should replace 'for (counter = 0; counter < returnArray.length; counter++)'
with
for (counter = 0; counter < length; counter++)
and also replace
counter ++
with counter = counter + 1
This is purely because of the syntax the course uses (M150 I assume).
EDIT:
This bit is also wrong
holder = returnArray[returnArray+1];
returnArray[value+1] = returnArray[value];
returnArray[value] = holder;
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 :D
so to Nomisco :-bd
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];
// TO DO TASK 3 (iv)
// PLACE YOUR FUNCTION CODE HERE
document.write('Unsorted Array: ' + unsortedArray + '<BR>');
length = unsortedArray;
bubbleSort();
document.write('Sorted Array: ' + returnArray);
// invoke bubbleTest() to test the bubbleSort() function
bubbleTest();
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
Re: Javascript Bubble Sort
OK, check these things - I won't directly give you the answer :rolleyes:
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! :eek:
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];
// TO DO TASK 3 (iv)
// PLACE YOUR FUNCTION CODE HERE
document.write('Unsorted Array: ' + unsortedArray + '<BR>');
document.write('Sorted Array: ' + bubbleSort(arrayToSort));
}
Re: Javascript Bubble Sort
Well, bubbleSort(arrayToSort) is the argument for the bubbleSort function. So, you need bubbleSort(name of the array you are sorting).
Hint: it's declared outside of the that function.
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
OK.
Your final output will be document.write('Unsorted array ' + unsortedArray);
Then you want to output the sorted array, so the next line will be document.write('Sorted array ' + bubbleSort(unsortedArray));
In other words. ... + bubbleSort(unsortedArray) is the unsortedArray when parsed (run through / executed) by bubbleSort
Any clearer?
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)8-}
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];
// TO DO TASK 3 (iv)
// PLACE YOUR FUNCTION CODE HERE
document.write('Unsorted Array: ' + unsortedArray + '<BR>');
document.write('Sorted Array: ' + bubbleSort(unsortedArray));
}
// invoke bubbleTest() to test the bubbleSort() function
bubbleTest();
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
Re: Javascript Bubble Sort
for (counter = 0; counter < length; counter + 1)
counter = counter + 1
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;
Thanks again!
Re: Javascript Bubble Sort
Again, you don't need to use this here: for (value = 0; value < (returnArray.length-1); value++); as length was already declared and assigned (arrayToSort.length) eariler.
If it makes you feel any better (no pun intended), I've struggled badly with task 05, got it working now, but fudged it to do so...
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!!
ps. now the fun begins as I try to confirm a pass with some of Task 4