Skip to content

Commit

Permalink
refactor: optimize bubble sort
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinhermawan committed Sep 12, 2023
1 parent 548b03d commit af41896
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/bubble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ import type { CompareFn } from "./types";

export function bubbleSort<T>(arr: T[], compareFn: CompareFn<T>): T[] {
const sortedArray = arr.slice();
const n = sortedArray.length;

let sorted = false;
for (let i = 0; i < n; i++) {
let swapped = false;

while (!sorted) {
sorted = true;
for (let j = 0; j < n - i - 1; j++) {
if (compareFn(sortedArray[j], sortedArray[j + 1]) > 0) {
[sortedArray[j], sortedArray[j + 1]] = [
sortedArray[j + 1],
sortedArray[j],
];

for (let i = 0; i < sortedArray.length - 1; i++) {
if (compareFn(sortedArray[i], sortedArray[i + 1]) > 0) {
const temp = sortedArray[i];
sortedArray[i] = sortedArray[i + 1];
sortedArray[i + 1] = temp;

sorted = false;
swapped = true;
}
}

if (!swapped) break;
}

return sortedArray;
Expand Down

0 comments on commit af41896

Please sign in to comment.