PDA

View Full Version : bubble sort function query


racybabeuk
09-12-2005, 07:28 PM
I need to sort a list of numbers using the bubble sort function but cannot get my code to work - can anyone tell me what I am missing please as the javascript console gives no errors at all!!




<HEAD>

<SCRIPT LANGUAGE="JavaScript">
var sortArray = new Array ();
sortArray[0] = 9;
sortArray[1] = 7;
sortArray[2] = 2;
sortArray[3] = 6;
sortArray[4] = 1;
sortArray[5] = 4;
sortArray[6] = 8;
sortArray[7] = 10;
sortArray[8] = 5;
sortArray[9] = 3;
function bubbleSort(sortArray){
var n = sortArray.length;
for (var i = n - 1; i > 0; i--){
for (var j = 0; j < i; j++){
if (sortArray[j] < sortArray[j+1]){
var t = sortArray[j];
sortArray[j] = sortArray[j+1];
sortArray[j+1] = t;
return sortArray;
}
}
}
}
//
// A FUNCTION FOR TESTING THE bubbleSort() FUNCTION. IT IS CALLED
// FROM THE HTML BODY AND NEITHER RECEIVES NOR RETURNS A VALUE
//
function bubbleTest() {
// The array of values to be sorted
var sortArray = new Array (9);
sortArray[0] = 9;
sortArray[1] = 7;
sortArray[2] = 2;
sortArray[3] = 6;
sortArray[4] = 1;
sortArray[5] = 4;
sortArray[6] = 8;
sortArray[7] = 10;
sortArray[8] = 5;
sortArray[9] = 3;

// CALL THE FUNCTION bubbleSort.
bubbleSort(sortArray);


// CLOSE THE DOCUMENT (NEEDED IN MOZILLA BROWSERS)
document.close();
}

</script>
</HEAD>


<BODY>
<input type="button" name="" value="Sort!" onClick="bubbleTest();">
</BODY>
</HTML>

TheShaner
09-12-2005, 08:05 PM
This code has been tested and works. Now, the way you have your bubble sort set up, it sorts from greatest to least, just so you know. I have it outputing to a textarea field so that you can see the results. If you have questions about what I did, feel free to ask.

<HTML>
<HEAD>
<title>Bubble Sort</title>

<SCRIPT LANGUAGE="JavaScript">
<!--
function bubbleSort(sortArray){

var n = sortArray.length;
for (var i = n - 1; i > 0; i--){
for (var j = 0; j < i; j++){
if (sortArray[j] < sortArray[j+1]){
var t = sortArray[j];
sortArray[j] = sortArray[j+1];
sortArray[j+1] = t;
}
}
}
// Display Sort
for(i=0; i<10; i++) {
document.test.mydisplay.value += sortArray[i] + "\n";
}
}

function bubbleTest() {
// The array of values to be sorted
var sortArray = new Array (9,7,2,6,1,4,8,10,5,3);

// Clear text area
document.test.mydisplay.value = ""

// CALL THE FUNCTION bubbleSort.
bubbleSort(sortArray);

// CLOSE THE DOCUMENT (NEEDED IN MOZILLA BROWSERS)
document.close();
}
-->
</script>
</HEAD>


<BODY>
<form name="test">
<input type="button" name="mybutton" value="Sort!" onClick="bubbleTest()">
<br><br>
<textarea name="mydisplay" cols="20" rows="10"></textarea>
</form>
</BODY>
</HTML>

-Shane

Pyth007
09-12-2005, 08:09 PM
I see a few problems off the bat:
1) you've got two sortArray's, one as a global and one as a part of bubbletest().
2) no alert statements or other ways of seeing your results... Perhaps have bubbleTest() be:
function bubbleTest()
{
var myArray = bubbleSort(sortArray);
var returnText = "";
for (var m = 0; m<myArray.length.m++)
{
returnText += "myArray["+m+"] = "+myArray[m] +"\n";
alert(returnText);
}
}
3) Your return statement is too soon in bubbleSort... as written, the array will be returned as soon as one change is made. Put the return after both for-loops!

Edit: simu-post... Shane's should work too

TheShaner
09-12-2005, 08:19 PM
Pyth007 described the changes I made to your code perfectly, hehe. I almost used an alert to show the results. You could change my output to:

document.test.mydisplay.value += "sortArray[" + i + "] = " + sortArray[i] + "\n";

to get the result looking like Pyth007's, since his output is more detailed than my original one.

-Shane

racybabeuk
09-13-2005, 12:20 AM
Shane and Pyth007 - thank you very much for your swift and extensive responses. I did need to output in ascending numerical order (least to greatest) but its late and I am not sure if I swap the variable j with t (using Shane's original code)? Please excuse my ignorance - I really am finding it very difficult to grasp javascript :o

I don't need to show detailed output - just the list of numbers in correct order and I could just use a document.write statement to output but I can only get that to work by writing in the onclick call in the body as per my first post.

Thanks for your patience.

Suzanne

racybabeuk
09-13-2005, 10:43 AM
Result!!! After a few hours sleep I realised I needed to change the operator symbol < to > in the "if" statement to sort the Array - and it worked!!!! :D


for (var i = n - 1; i > 0; i--){
for (var j = 0; j < i; j++){
if (sortArray[j] > sortArray[j+1]){
var t = sortArray[j];
sortArray[j] = sortArray[j+1];
sortArray[j+1] = t;


So I think I have finally sussed this one but I know I will never make a natural programmer - still at least I know my weakness LOL

Thanks for your help.

Suzanne

TheShaner
09-13-2005, 02:29 PM
Glad you figured it out :thumbsup: What always helped me was to step through the code on paper and just see what would happen on each iteration and write down the results.

So good luck with the rest of your studies. It'll come more natural later as you begin to grasp the concepts. Once that happens, the sky's the limit and there won't be one programming language safe from you, haha.

-Shane

racybabeuk
09-13-2005, 05:06 PM
Thanks for the vote of confidence :thumbsup:

I now need to find one simple way of improving this bubblesort program but I am not sure where to start - I have read on wikopedia about an insertion method of bubblesort but have not got a clue what it means!!! I guess the one weakness of my program is the slow method of swapping and sorting but can anyone suggest a minor improvement please?




<HTML>
<HEAD>
<title>Bubble Sort</title>

<SCRIPT language = "JavaScript">

//
// A FUNCTION TO SORT AN ARRAY OF NUMBERS
//

function bubbleSort(sortArray) {

// DEFINE THE NECESSARY VARIABLE(S)

var n = sortArray.length;

//THE CODE THAT MAKES PASSES THROUGH ANY SIZE ARRAY AND FOR
// EACH PASS COMPARES ALL PAIRS OF ELEMENTS IN THE ARRAY

for (var i = n - 1; i > 0; i--){
for (var j = 0; j < i; j++){
if (sortArray[j] > sortArray[j+1]){
var t = sortArray[j];
sortArray[j] = sortArray[j+1];
sortArray[j+1] = t;
}
}
}
// Display Sort
for(i=0; i<10; i++) {
document.test.mydisplay.value += sortArray[i] + "\n";
}
}

//
// A FUNCTION FOR TESTING THE bubbleSort() FUNCTION. IT IS CALLED
// FROM THE HTML BODY AND NEITHER RECEIVES NOR RETURNS A VALUE
//
function bubbleTest() {
// The array of values to be sorted
var sortArray = new Array (9, 7, 2, 6, 1, 4, 8, 10, 5, 3);

// CALL THE FUNCTION bubbleSort.

document.test.mydisplay.value = ""
bubbleSort(sortArray);

// CLOSE THE DOCUMENT (NEEDED IN MOZILLA BROWSERS)
document.close();
}
</SCRIPT>
</HEAD>
<BODY>
<form name="test">
<input type="button" name="" value="Sort!" onClick="bubbleTest();">
<br><br>
<textarea name="mydisplay" cols="20" rows="10"></textarea>
</form>
</BODY>
</HTML>

TheShaner
09-13-2005, 05:27 PM
The insertion form of the bubble sort refers to when you start with an empty array and then insert the values one at a time and sorting as you insert using the bubble sort method. If you're starting with your array already filled like you have, then there's no way of improving the bubble sort that I know of (it's a naturally slow sorting algorithm). However, there are many other sorts that yield better results, like QuickSort. This link (http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html) provides a little explanation of sorts and a whole slew of different sort algorithms and their code. If you google (http://www.google.com/search?hl=en&q=sort+algorithm), you'll find a lot of help about sorting.

Anyway, not sure if I helped you out at all. If your assignment wants you to only stick with bubble sorting and to try to improve your previous method, then I don't know what to tell you, because really, any changes you make to the sort will not make it a bubble sort, hehe.

-Shane

racybabeuk
09-15-2005, 04:22 PM
Shane - you definitely helped me understand my program better thank you :thumbsup:

I see what you are saying about how I could not improve the bubblesort program without changing the whole concept so I used some lateral thinking and used a javascript function library for the bubblesort program as an improvement and then used the function statement in the bubbletest program. I thought that this improved the usability of the program - well thats my theory anyway :D