From b6fd393b906511146551f4505f3a8926f0a86531 Mon Sep 17 00:00:00 2001 From: Luke Schlangen Date: Thu, 19 Oct 2017 16:32:06 -0500 Subject: [PATCH] Create bubbleSort.js --- Algorithms/bubbleSort.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Algorithms/bubbleSort.js diff --git a/Algorithms/bubbleSort.js b/Algorithms/bubbleSort.js new file mode 100644 index 0000000000..b2ae08178a --- /dev/null +++ b/Algorithms/bubbleSort.js @@ -0,0 +1,20 @@ +var a = [33, 103, 3, 726, 200, 984, 198, 764, 9]; + +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); +console.log(a);