dcsimg
www.webdeveloper.com
Results 1 to 5 of 5

Thread: Change a result of BubbleSort

  1. #1
    Join Date
    May 2017
    Posts
    7

    Change a result of BubbleSort

    Hi! After I used the BubbleSort algorithm to sort by 10 previously generated random numbers, I would like to display them but with a space between the comma and a number. But is it even possible to change the result after the algorithm?
    Thanks for answers!
    Code:
    /*i've created these random numbers before*/
    var a = [number1, number2, number3, number4, number5, number6, number7,number8, number9, number10];
     
    			function bubbleSort(a)
    			{
    				var swapped;
    				do {
    					swapped = false;
    					for (var i=0; i < a.length-1; i++) {
    						if (a[i] > a[i+1]) {
    							var temp = a[i];
    							a[i] = a[i+1];
    							a[i+1] = temp;
    							swapped = true;
    						}
    					}
    				} while (swapped);
    			}
    			bubbleSort(a);
    	
    				
    				document.getElementById('container1').innerHTML = 'The random numbers are: ' + a;
    			}
    The result looks p.e: 23,34,657,5643...
    But it should look like: 23, 34, 567, 5643...

  2. #2
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    542
    Try adding something like
    Code:
    var resultStr="";
    for (var i=0; i < a.length; i++) {
      resultStr+= a[i];
      resultStr+=(i < (a.length - 1)): ", ":""; }
    ...
    ...     'The random numbers are: ' + resultStr
    HTH

  3. #3
    Join Date
    May 2017
    Posts
    7
    Are you sure about this code? I recive an error message because of your 4. line (because of the ':') but I do not learn JavaScript long enough to see where the mistake is.

  4. #4
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    542
    Sorry 'bout that. The first colon on line 4 should be a question mark.

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    4,800
    Why you not using the .sort() method of an array?

    Code:
    var a = [9, 1, 8, 2, 7, 3, 6, 4, 5, 10];
    a.sort( function(a,b){ return a - b; } ); // sorts 0 to nth, using b-a reverses that < and > can be used in alpha sorts too.
    a.join(", ");
    console.log("array = "+a);
    Will sort the array in order and also the final part joins the array in to a string and puts a comma and a space after the comma.

    Job done, no bubble sorting needed (which is costly in processing time)
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles