bubble sort javascript

bubbleSort(Array) {
    let len = Array.length;
    for (let i = 0; i < len; i++) { //you can also use "for in", so you don't need the variable "len"
        for (let j = 0; j < len; j++) {
            if (Array[j] > Array[j + 1]) {
                let tmp = Array[j];
                Array[j] = Array[j + 1];
                Array[j + 1] = tmp;
            }
        }
    }
    return Array;
};

4
4
Jsoteeln 80 points

                                    const bubbleSort = array =&gt; {
  const arr = Array.from(array); // avoid side effects
  for (let i = 1; i &lt; arr.length; i++) {
    for (let j = 0; j &lt; arr.length - i; j++) { // starts up to length - 1
      if (arr[j] &gt; arr[j + 1]) {
        [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
      }
    }
  }
  return arr;
};

console.log(bubbleSort([4, 9, 2, 1, 5]));

4 (4 Votes)
0
3.86
7
Wren 115 points

                                    function bubbleSort(array) {
  const len = array.length;
  const retArray = array;
  for (let i = 0; i &lt; len; i++) {
    for (let j = 0; j &lt; len - i; j++) {
      const a = array[j];
      if (a !== array[-1]) {
        const b = array[j + 1];
        if (a &gt; b) {
          retArray[j] = b;
          retArray[j + 1] = a;
        }
      }
    }
  }
  return retArray;
}
bubbleSort([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);

3.86 (7 Votes)
0
4.4
5
Blue Genie 95 points

                                    function BubbleSort(arr) {
      const sortedArray = Array.from(arr);
      let swap;
      do {
        swap = false;
        for (let i = 1; i &lt; sortedArray.length; ++i) {
          if (sortedArray[i - 1] &gt; sortedArray[i]) {
            [sortedArray[i], sortedArray[i - 1]] = [sortedArray[i - 1], sortedArray[i]];
            swap = true;
          }
        }
      } while (swap)
      return sortedArray;
    }

    console.log(BubbleSort([7,99,1,88,34,2,90,7]));

4.4 (5 Votes)
0
5
2
StuR 120 points

                                    function bubblesort(array) {
    len = array.length;

    for (let i = 0; i &lt; len; i++) {
        for (let j = 0; j &lt; len - i; j++) {
            let a = array[j];
            if (a != array[-1]) {
                var b = array[j + 1];
                if (a &gt; b) {
                    array[j] = b;
                    array[j + 1] = a;
                }
            }
        }
    }
}

let array = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
bubblesort(array);
console.log(array)

5 (2 Votes)
0
3.3
10
Sarnesjo 115 points

                                    const bubbleSort = (arr) =&gt; {
  for (let i = 0; i &lt; arr.length; i++) {
    for (let j = 0; j &lt; arr.length - i; j++) {
      if (arr[j] &gt; arr[j + 1]) {
        let tmp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = tmp;
      }
    }
  }
  return arr;
}

3.3 (10 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
js bubble how to write bubble sort function js bubble sort javascript code how to bubble sort in js bubble sort steps javasacript bubble short in javascript bubble sort node how to sort an array using bubble sort in javascript sorting in javascript bubble sort bubble sort in javascript inbuilt function return sorted list using bubble sort javascript implement bubble sort in javascript program to sort an array using bubble sort algorithm in javascript sort bubble sort javascript bubble algorithm javascript sort array bubble bubble js bubble sort in javascript easy Demonstrate bubble Sort javascript and explain when you might use it? bubble sort in javascipt sort bubble javascript javascript bubble instructions bubble sort using javascript my bubble sort in javascript did't work how to code in javascript for bubble sort bubble sort algorithm javascript code javascripot bubble sort js on bubble bubble sort javascript es6 bubble sort using javascript gfg how to do bubble sort in js array sort bubble bubble sort of array or array javascript Bubble sort algorithm in JavaScript bubble in javascript bubble sort function javascript what is bubble sort in js javascript bubble of bubble sort an array does javascript sort use bubble sort? bubble short javascript javascript bubble soer bubble sort algoritim javascript array bubble sort algorithm understanding bubble sort algorithm in js JS CODE BUBBLE SORT bubble sort in node js bubble sort techniques js bubble sort javascript w3schools bubble sort code in javascript bubble sort using javascript ilustation array bubble sort bubble sort js code javascript sort bubble javascript bubble sort explained bubble sort an array of arrays javascript bubble sort javascript with compare bubble sort nodejs bubble sot in javscript bubble language javascript javascript popup bubble bubble sort algorithm js tutorial bubble sorting code js bubble sort mdn bubble sort javascript structure How to do a simple bubble sort in JavaScript bubble sort array bubble js understanding bubble sort javascript bubble sort node js how do i do bubble sort in javascript plain javascript bubble sort algorithm vanilla javascript bubble sort bubble javascript bubble sorting javascript sorting in js code how to use bubble sort in javascript pseudocode bubble sort javascript javascript bubble sort example bubble sort function in javascript js bubble sort algorithm bubble letters node js how to buble sort an array of numbers in javascript bubble in js javascript bubble sort function bubble srt is js bubble sort for in js javascript bubble sort algorithm pseudocode of bubble sort javascript javascript bubble bubble shooter javascript bubble sort js quiz bubble sorting in javascript bubble sort javascriot javascript implement Bubble Sort bouble sort example in javascript bubble chart algorithm javascript bubble char algorithm javascript bubble grouping algorithm javascript bubble event in javascript burble javascript js bubble sort reversible js bubble sort least moveds bubble sort algorithm usin gjavscript bubble sort example javascript bubble sort injs buuble sort js js bublesorting .sort() js bubble sort JavaScript program to apply Bubble Sort algorithm on any array. bubble sort question in javasccript bubble srt js general formula for bubble sort in javascript bubble sort in javascript program perform swapping and sorting in javascript js buuble sort buble sort js boble sorting javascript bubble sort as arrow function javascript javascript bubblesort with function javascript method bubble Javascript Algorithms &mdash; Bubble Sort sidepoint js buble sort javascript bubble sort numbers bubble sort program in javascript bubblesort in javascript buble sort in javascript array bubblesort javascript bubble sort array with swap javascript bubble sort code javascript bubble sort in javascript source code bubble sort javascript algorithm bubble sort loop js sorting an array with swapping javascript node js bubble sort bubble short js Write a JavaScript function to apply Bubble Sort algorithm code to write bubblesort in javascript buble sort funciton javascript bubble sorting algorithm javascript bubble sort vs sort method javascript easy bobble sort javascript bubble sort technique javascript bubblesort nums js bubblesort array js javascript bubble sort an array js bubblesort is js .sort a bubble sort bubble sort javascript entire code bubble sort javascript DESCRIPTION bubble sort algorithm javascript bubble sort in js buuble sort javascript bubble sort javascript explanation bubble sort html bubble sort why is it important js how to bubble sort in javascript javascript bubleshort example how to do bubble sort in javascript bubble sort queue java bubble sort array javascript bubble sort in javascript javascript bubble sort buble sort javascript bubble sort js example bubblesort js buble sort in js bubble sort algorithm js bubblesort javascript basic bubble sort js bubble sort js writting bublesort function js js bubble sort bubble sort javascript
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source