Skip to content

Commit 19c1653

Browse files
authored
Add files via upload
1 parent 0956543 commit 19c1653

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<h1>Problem Solving 6</h1>
10+
<script src="bubbleSort.js"></script>
11+
</body>
12+
</html>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const bubbleSort = (array) => {
2+
let swapped;
3+
do {
4+
swapped = false;
5+
for (let i = 0; i < array.length; i++) {
6+
if (array[i] > array[i + 1]) {
7+
8+
let temp = array[i];
9+
array[i] = array[i + 1];
10+
array[i + 1] = temp;
11+
swapped = true;
12+
}
13+
}
14+
} while (swapped);
15+
return array;
16+
};
17+
18+
const a = [34, 203, 3, 746, 200, 984, 198, 764, 9];
19+
console.log(bubbleSort(a));
20+
//The worst case is O(n^2) when array not sorted
21+
//The best case is O(n) when array is sorted

0 commit comments

Comments
 (0)