Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Javascript Bubble Sort

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Red face 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>
    Last edited by helloworld922; May 23rd, 2010 at 06:40 PM. Reason: Please wrap html code in [html] tags


  2. #2
    Junior Member
    Join Date
    May 2010
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Javascript Bubble Sort

    Quote Originally Posted by Fidelacchius View Post
    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?
    Last edited by helloworld922; May 23rd, 2010 at 06:40 PM.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Javascript Bubble Sort

    There's a difference between Java script and Java

    However, the algorithm is the same. See Bubble sort for the pseudo-code.

  4. #4
    Junior Member
    Join Date
    May 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?

  5. #5
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Javascript Bubble Sort

    Nomisco pointed out an error:

    if (returnArray[value)> returnArray[value+1])

  6. #6
    Junior Member
    Join Date
    May 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy 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!

  7. #7
    Junior Member
    Join Date
    May 2010
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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;

  8. The Following User Says Thank You to nomisco For This Useful Post:

    Fidelacchius (May 24th, 2010)

  9. #8
    Junior Member
    Join Date
    May 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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];

    // 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>

  10. #9
    Junior Member
    Join Date
    May 2010
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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
    Last edited by nomisco; May 24th, 2010 at 11:25 AM.

  11. #10
    Junior Member
    Join Date
    May 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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));

    }

  12. #11
    Junior Member
    Join Date
    May 2010
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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.
    Last edited by nomisco; May 24th, 2010 at 02:35 PM.

  13. #12
    Junior Member
    Join Date
    May 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  14. #13
    Junior Member
    Join Date
    May 2010
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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?

  15. #14
    Junior Member
    Join Date
    May 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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];


    // 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>

  16. #15
    Junior Member
    Join Date
    May 2010
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Javascript Bubble Sort

    for (counter = 0; counter < length; counter + 1)

    counter = counter + 1

  17. #16
    Junior Member
    Join Date
    May 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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!

  18. #17
    Junior Member
    Join Date
    May 2010
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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...

  19. #18
    Junior Member
    Join Date
    May 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Talking 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

Similar Threads

  1. bubble sort timer problem
    By JavaNoob82 in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 12th, 2010, 09:22 AM
  2. calendar javascript required
    By sagar13 in forum Paid Java Projects
    Replies: 0
    Last Post: February 5th, 2010, 12:57 AM
  3. Replies: 4
    Last Post: February 4th, 2010, 03:21 AM
  4. How can i put session in Javascript?
    By rajani in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: July 8th, 2009, 12:46 PM
  5. Replies: 3
    Last Post: May 18th, 2009, 03:45 AM