showing results for - "javascript bubble sort"
Bronwen
13 Nov 2016
1bubbleSort(Array) {
2    let len = Array.length;
3    for (let i = 0; i < len; i++) { //you can also use "for in", so you don't need the variable "len"
4        for (let j = 0; j < len; j++) {
5            if (Array[j] > Array[j + 1]) {
6                let tmp = Array[j];
7                Array[j] = Array[j + 1];
8                Array[j + 1] = tmp;
9            }
10        }
11    }
12    return Array;
13};
Vishal
11 May 2020
1const bubbleSort = array => {
2  const arr = Array.from(array); // avoid side effects
3  for (let i = 1; i < arr.length; i++) {
4    for (let j = 0; j < arr.length - i; j++) { // starts up to length - 1
5      if (arr[j] > arr[j + 1]) {
6        [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
7      }
8    }
9  }
10  return arr;
11};
12
13console.log(bubbleSort([4, 9, 2, 1, 5]));
14
Myriam
25 Jul 2017
1const bubbleSort = (arr) => {
2  for (let i = 0; i < arr.length; i++) {
3    for (let j = 0; j < arr.length - i; j++) {
4      if (arr[j] > arr[j + 1]) {
5        let tmp = arr[j];
6        arr[j] = arr[j + 1];
7        arr[j + 1] = tmp;
8      }
9    }
10  }
11  return arr;
12}
Amy
03 Feb 2019
1function bubblesort(array) {
2    len = array.length;
3
4    for (let i = 0; i < len; i++) {
5        for (let j = 0; j < len - i; j++) {
6            let a = array[j];
7            if (a != array[-1]) {
8                var b = array[j + 1];
9                if (a > b) {
10                    array[j] = b;
11                    array[j + 1] = a;
12                }
13            }
14        }
15    }
16}
17
18let array = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
19bubblesort(array);
20console.log(array)
Nicole
25 Aug 2018
1function BubbleSort(arr) {
2      const sortedArray = Array.from(arr);
3      let swap;
4      do {
5        swap = false;
6        for (let i = 1; i < sortedArray.length; ++i) {
7          if (sortedArray[i - 1] > sortedArray[i]) {
8            [sortedArray[i], sortedArray[i - 1]] = [sortedArray[i - 1], sortedArray[i]];
9            swap = true;
10          }
11        }
12      } while (swap)
13      return sortedArray;
14    }
15
16    console.log(BubbleSort([7,99,1,88,34,2,90,7]));
Felix
29 Jun 2017
1// like using Array.reverse();
2function bubbleReverse(arr) {
3    for (let i = arr.length - 1; i > 0; i--) {
4        for (let j = arr.length - 1; j > 0; j--) {
5            if (arr[j] > arr[j - 1]) {
6                let temp = arr[j];
7                arr[j] = arr[j - 1];
8                arr[j - 1] = temp;
9            }
10        }
11    }
12    return arr;
13}
14
15// like using Array.sort();
16function bubbleSort(arr) {
17    for (let i = 0; i < arr.length - 1; i++) {
18        for (let j = 0; j < arr.length - 1; j++) {
19            if (arr[j] > arr[j + 1]) {
20                let temp = arr[j];
21                arr[j] = arr[j + 1];
22                arr[j + 1] = temp;
23            }
24        }
25    }
26    return arr;
27}
queries leading to this page
bubble event in javascriptjavascript bubblesortjs bubblesortadvantages of bubble sort in jsbubble sort question in javasccriptbubble sort javascript with comparebubble sort once jsbuble sort funciton javascriptbubble sort algorithm jsbubble sort in javasciptbubble in jsorder array bubble sortbubble up javascriptbubble in javascriptjavascript algorithms e2 80 94 bubble sort sidepointjavascript bubble sort last valuehow to do bubble sort javascriptarray bubble sort algorithmbubble sort javascript descriptionbubble sort htmlbubble sort using javascript gfgbubble sort method javascripthow to code in javascript for bubble sortjavascript button bubblebubble sort js quizbubble sorting in javascriptbubble sort with javascriptjavascript popup bubblejavascript bubblesort with functionbubblesort array jsperform swapping and sorting in javascriptbouble sort example in javascriptjavascript how to bubble eventwrite a javascript function to apply bubble sort algorithmbubble sort with jsbubble sort java scriptbubble sort algoritim javascriptbubble event jsreturn sorted list using bubble sort javascriptwrite bubble sort jsbubble sort once in javascriptbubble sort example javascriptbubble sort queue javabubble sort js examplejavascript bubleshort examplejavascript interactive bubblejs bubble sort forcebubble sort example by jshow to do a simple bubble sort in javascriptbubble sort javascript entire codehow to do bubble sort in javascripthow to bubble sort in javascriptbubble jsdoes javascript sort use bubble sort 3fjs buble sortbubble sort an array of arrays javascriptwhat is bubble in javascriptbubble sort in jsbubble sotrng jsbubble sort algorithm javascript codehow to write bubble sort function jshow to do bubble sort in jshow to buble sort an array of numbers in javascriptpseudocode of bubble sort javascriptbubble char algorithm javascriptjavascript sort bubblebubble sort as arrow function javascriptarray bubble sortbubble sort javasrciptbubble sort algorithm in javascriptreverse array javascript bubble sortbubble sort arrayjavascript bubble sort arraybubble sort with new arraybubble chart jsbubble sort function in javascriptbubble sort array javascriptbubble sort javaacriptwritting bublesort function jsbubble sort javascriptbubble short in javascripthow to bubble sort in jsbubble javascriptjavascript bubble osrthow bubble sort works in node jsbubble sort using javascriptunderstanding bubble sort javascriptjs buuble sortjavascript bubble sort algorithmsorting in javascript bubble sortbubble grouping algorithm javascriptsort array bubble 22bubble sort once in javascript 22array bubblesort javascriptgeneral formula for bubble sort in javascripthow do i do bubble sort in javascriptsort bubble sort javascript bubble sort in javascript inbuilt functionunderstanding bubble sort algorithm in jsboble sorting javascriptbubble sort js codewrite a program in javascript to implement a bubble sort algorithmjavascript bubble instructionsbubble sort jsbubble sort algorithm javascriptbubble sort javascript codehow to use bubble sort in javascriptbubble sort javascript algorithmbubble sort injsbubble sort techniques jsjavascript bubble sort numbersbubble sorting algorithm javascriptbubble sort jsbubble sort function javascripthow to use bubble in jsbubble sort nodewhat is bubble sort in jsbubble shooter javascriptsorting an array with swapping javascriptnode js bubble sortjs code bubble sortbubble sort steps javasacriptbubble sort an arraybubble sort js mdnbubble sot in javscriptbubble sort algorithm usin gjavscriptbuble sort in jsprogram to sort an array using bubble sort algorithm in javascriptjavascript bubble sort explainedhow to bubble sort using es6javascript program to apply bubble sort algorithm on any array sort bubble javascriptbubble short javascript sort 28 29 js bubble sortbubble sort descending order javascripthow is a bubble sort algorithm implemented 3f jsjavascripot bubble sortbubble sort function jsbubble sort javascript w3schoolsbuble sort jsjavascript bubble ofbubble srt jsbubblesort javascriptbubble sort javascript ascendingbubble sort in javascriptjs bubble sortbubble programa c3 a7 c3 a3o e javascriptbubble sort in javascript easybubble sorting jsbubble sorting javascriptbubble sort in node jsjs bubble orderbubble sort nodejsbubble sort loop jsbubble sort javascript structurebubble sort in javascript source codeimplement bubble sort in javascriptjavascript bubble sort function bubble sort in jsjs bubble sort algorithmbubble sort of array or array javascripteasy bobble sort javascriptbubble chart javascriptbubble sort javascript es6bubble in array javascript bubblebubble sort javacriptjavascript bubble sortbuble sort javascripthow is a bubble sort algorithm implemented in javascriptbubble sort code in javascriptjs bubble b 3bjavascript bubble soerhow to bubble sort jsjs bubblebubble sort mdncode to write bubblesort in javascripthow to make a bubble sort algorithm in jsbubble sort in java scriptbubble sort javascript examplehow to implement bubble sort javascriptbubble jsarray sort bubblebubble sorting code jsbubble sort javascript all iterationsbubble algorithm javascriptbubble sort node jsbubble sort program in javascriptbubble letters node jsbubble sort in javascript programjavascript method bubblebubble chart in jsbubblesort nums jsdemonstrate bubble sort javascript and explain when you might use it 3fvanilla javascript bubble sortbubble sort using javascript ilustationbubble sort vs sort method javascriptbubble sort javscriptjs bubble sort html elementsmy bubble sort in javascript did 27t workis js sort a bubble sortbubble sort why is it important jsbubble sort for in jsbubble language javascriptjavascript bubble sort examplebubble sort implementation jsbubble sort array with swap javascriptjavascript bubblespseudocode bubble sort javascriptbubble sort technique javascripthow to sort an array using bubble sort in javascriptbubble sort javascriotbubble short jsbasic bubble sort jsbuble sort in javascriptbubble sort algorithm js tutorialburble javascriptbubble sort code javascriptbubble sort javascript tutorialbuuble sort javascriptbubble srt is jsbubblesort in javascriptplain javascript bubble sort algorithmjavascript bubbles sortjavascript bubble sort algorithmsbubble sort javascript explanationbubble chart algorithm javascriptjavascript bubble sort an arraybubble sort array jsbubblesort jsjs on bubblebubble sort an array javascriptjavascript bubble sort