-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
26 lines (22 loc) · 688 Bytes
/
index.ts
File metadata and controls
26 lines (22 loc) · 688 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Bubble Sort
// Time: O(n^2),
// Space: O(1)
export function bubbleSort(arrOfValues: number[]) {
let swap = true;
// start loop
while (swap) {
// assume we are not done
swap = false;
// loop through all numbers in the array
for (let i = 1; i < arrOfValues.length; i += 1) {
// if the current index value is greater than the next index value
if (arrOfValues[ i - 1 ] > arrOfValues[ i ]) {
// we have more work to do
swap = true;
// swap the current index value with the next index value
[ arrOfValues[ i - 1 ], arrOfValues[ i ] ] = [arrOfValues[ i ], arrOfValues[ i - 1 ] ];
}
}
}
return arrOfValues;
}