Skip to content

Instantly share code, notes, and snippets.

@galElmalah
Last active June 11, 2020 09:52
Show Gist options
  • Save galElmalah/f2c6708d5ecbd3f58edf42a0bf0fd84f to your computer and use it in GitHub Desktop.
Save galElmalah/f2c6708d5ecbd3f58edf42a0bf0fd84f to your computer and use it in GitHub Desktop.
bubble sort with actions
const bubbleSort = (array, onAction) => {
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length; j++) {
onAction({ type: ACTIONS.COMPARE, data: [j, j + 1] });
if (array[j] > array[j + 1]) {
let tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
onAction({ type: ACTIONS.SWAP, data: [j, j + 1] });
}
}
onAction({ type: ACTIONS.SORT, data: array.length - i - 1 });
}
return array;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment