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

Threaded View

  1. #1

    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

Similar Threads

  1. bubble sort timer problem
    By JavaNoob82 in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 12th, 2010, 08:22 AM
  2. calendar javascript required
    By sagar13 in forum Paid Java Projects
    Replies: 0
    Last Post: February 4th, 2010, 11:57 PM
  3. Replies: 4
    Last Post: February 4th, 2010, 02: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