Last active
June 11, 2020 09:52
-
-
Save galElmalah/f2c6708d5ecbd3f58edf42a0bf0fd84f to your computer and use it in GitHub Desktop.
bubble sort with actions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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