showing results for - "bubble sort javascript"
Cristóbal
10 Apr 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};
Elias
05 Jan 2021
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
Sarah
22 Jan 2018
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}
Irene
18 Sep 2018
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)
Anthony
17 Jan 2021
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]));
Monica
26 Feb 2017
1function bubbleSort(array) {
2  const len = array.length;
3  const retArray = array;
4  for (let i = 0; i < len; i++) {
5    for (let j = 0; j < len - i; j++) {
6      const a = array[j];
7      if (a !== array[-1]) {
8        const b = array[j + 1];
9        if (a > b) {
10          retArray[j] = b;
11          retArray[j + 1] = a;
12        }
13      }
14    }
15  }
16  return retArray;
17}
18bubbleSort([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
queries leading to this page
buuble sort javascriptjavascript bubblejavascript bubble sort numbershow do i do bubble sort in javascriptplain javascript bubble sort algorithmpseudocode bubble sort javascriptbubble sort javascript explanationjavascript implement bubble sortjavascript bubble sort algorithmjavascript button bubblejavascript bubblesbubble sorting js sort 28 29 js bubble sortbubble sort javascript examplemy bubble sort in javascript did 27t workbubble sort javascript all iterationsjavascript bubleshort examplejs bubble sort least movedsbubble sotrng jsprogram to sort an array using bubble sort algorithm in javascriptbubble sort in node jsbuuble sort jswrite a javascript function to apply bubble sort algorithmhow to implement bubble sort javascripthow to code in javascript for bubble sortbubble sort js codearray bubblesort javascriptbubble sort why is it important jsjavascript bubble sortjavascript bubblesort with functionbubble sort javascript tutorialbubble event jsbubble sort in javascript inbuilt functionhow to do a simple bubble sort in javascriptbubble sot in javscriptbubble sort techniques jsbubble sort in javasciptbubble sort algoritim javascripthow to bubble sort jshow is a bubble sort algorithm implemented in javascriptbubblesort javascriptimplement bubble sort in javascriptbubble sort js quizbubble sort once in javascriptbubble sort javascript entire codebubblesort in javascriptbasic bubble sort jsjavascript bubble osrtbubble sort algorithm js tutorialbubble sort algorithm javascriptbubble sort example javascriptbubble sort vs sort method javascriptbubble chart jsbubble algorithm javascriptbubble sort in javascript source codeburble javascriptjavascript bubble sort functioneasy bobble sort javascriptbuble sort in jsadvantages of bubble sort in jsbubble sort array javascriptjs bubble sort forcebubble sort question in javasccriptbubble sort in java scriptsorting in javascript bubble sortbubble sort javaacriptunderstanding bubble sort algorithm in jsbubble up javascriptbubblesort jshow to make a bubble sort algorithm in jsjavascript bubble sort arraybubble sort mdnjavascript how to bubble eventarray sort bubblejs bubble sort reversiblebubble srt jsbubble sort javascript algorithmhow to buble sort an array of numbers in javascriptunderstanding bubble sort javascriptdoes javascript sort use bubble sort 3fbubble sort javascript descriptionjavascript bubble sort algorithmsbubble short javascriptbubble jsjavascript bubble instructionsbubble sort once jsjs on bubblepseudocode of bubble sort javascripthow bubble sort works in node jsbubble sort javascript ascendingsort array bubblebubble sort for in jsis js sort a bubble sortbubble language javascriptbubble letters node jsbubble sort js mdnjavascript bubble soerjavascript bubble sort examplebubble sort with jswritting bublesort function jsbubble sort javscriptbubble sort with javascriptbubble sort method javascriptbubble sort algorithm in javascriptbubble shooter javascripthow to do bubble sort in jsbubble sort in javascripthow to do bubble sort javascriptbubble sort array with swap javascripthow to bubble sort in jssorting in js codehow to sort an array using bubble sort in javascripthow to use bubble in jsbubble sort with new arraybubble sort as arrow function javascriptsort bubble sort javascript bubble sort function in javascriptbubble sort in javascript programreturn sorted list using bubble sort javascriptbubble sort in javascript easybubble sort javasrciptbubble sort algorithm javascript examplebubble sort jsbubble sort descending order javascriptbubble sort java scriptbubble short jsbubble sort javascriptjavascript bubblesortjs bubblesortbuble sort javascriptjs buuble sortboble sorting javascriptbubblesort array jsjs bubble sort html elementsjavascript bubbles sortbubble in array bubble sort function jsbubble sort js examplereverse array javascript bubble sortarray bubble sort algorithmjavascript algorithms e2 80 94 bubble sort sidepointhow is a bubble sort algorithm implemented 3f jsbubble javascriptbubble sort injsbubble sort an array of arrays javascriptwhat is bubble sort in jsjavascript program to apply bubble sort algorithm on any array bubble sort nodejsbubble sort jsbubble chart javascriptbubble sort javascriotbubble sorting algorithm javascriptbubble sort javascript structurearray bubble sortbubble sort loop jsbuble sort jsbubble char algorithm javascriptbubble sort javascript codebubble sort of array or array javascriptbuble sort funciton javascriptjavascript method bubblejavascript bubble ofdemonstrate bubble sort javascript and explain when you might use it 3fbubble sort technique javascriptbubble event in javascriptbubble sort javascript w3schoolsbubble sort example by jsbuble sort in javascripthow to do bubble sort in javascriptbubble sort code in javascriptbubble srt is jsjs buble sortbubble jsjavascript popup bubblebubble sort queue javabubble chart algorithm javascriptjavascript interactive bubblenode js bubble sorthow to bubble sort using es6bubble sort javascript es6bubble sort javascript with comparebubble sort node jsbubble sort an arraysort bubble javascriptbubble sorting code jssorting an array with swapping javascriptbubble sort using javascriptwhat is bubble in javascriptbubble sort steps javasacriptbubble sort using javascript gfgjs bublesortingbubble sorting javascriptjavascript bubble sort explainedbubble chart in jsorder array bubble sortjs code bubble sortjs bubble sortbubble in javascriptbubble sort algorithm usin gjavscriptbubble sort algorithm javascript codebubble grouping algorithm javascriptbubble sort htmlwrite bubble sort jsbubble short in javascriptbubble sort nodehow to use bubble sort in javascriptvanilla javascript bubble sortbubble sort code javascriptbubble sort in jsjs bubble orderbubble sorting in javascriptperform swapping and sorting in javascriptgeneral formula for bubble sort in javascriptjavascript bubble sort an arrayjavascripot bubble sortjs bubble b 3bbubble in js 22bubble sort once in javascript 22write a program in javascript to implement a bubble sort algorithmjs bubblebubble sort function javascriptbubble sort using javascript ilustationcode to write bubblesort in javascripthow to bubble sort in javascriptbubble sort program in javascriptjavascript sort bubblebubble sort arraybubble programa c3 a7 c3 a3o e javascripthow to write bubble sort function jsbubblesort nums jsbubble sort algorithm jsjs bubble sort algorithmbouble sort example in javascript bubble sort in jsbubble sort implementation jsbubble sort javascript