Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and work.

aviboots(AT)netvision.net.il

Instant Grammar Checker - Correct all grammar errors and enhance your writing

BlueHost Web Hosting Black Friday
Teach Your Child To Read

BlueHost Web Hosting Black Friday

Bonsai Boy of New York Bonsai Trees and Accessories

DreamCloud The Comfortable Luxury Mattress Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


Disclosure We are a professional Programming & Software Q&A website, that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

16,402 questions

21,888 answers

573 users

How to sort a numeric array in descending order using bubble sort with JavaScript

1 Answer

0 votes
function bubbleSortDescending(arr) { 
    let i = 0, j;  
      
    while (i < arr.length) {  
        j = i + 1;  
        while (j < arr.length) { 
            if (arr[j] > arr[i]) { 
                var tmp = arr[i];  
                arr[i] = arr[j];  
                arr[j] = tmp;  
            }  
            j++;  
        }  
        i++;  
    }  
}  

let arr = [4, 8, 2, 11, 9, 7, 3]; 

bubbleSortDescending(arr);
  
console.log(arr);


/*
run:

11,9,8,7,4,3,2 

*/

 




answered May 20, 2020 by avibootz
edited May 20, 2020 by avibootz
...