Skip to content

Instantly share code, notes, and snippets.

@McLarenCollege
Last active September 28, 2022 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McLarenCollege/7537bd312cfa497bd2eb09af58b0ba98 to your computer and use it in GitHub Desktop.
Save McLarenCollege/7537bd312cfa497bd2eb09af58b0ba98 to your computer and use it in GitHub Desktop.
Exercise : Bubble Sort Algorithm

Write a function that sorts a given array using the Bubble Sort Algorithm and returns the sorted Array. For eg.

bubbleSort([1,5,3,0])

should return [0,1,3,5]

bubbleSort([-1,0,-5,5,8])

should return [-5,-1,0,5,8]

function bubbleSort(arr){
// write your code here
}
console.log(bubbleSort([1,5,3,0])); // should return `[0,1,3,5]`
console.log(bubbleSort([-1,0,-5,5,8])); // should return `[-5,-1,0,5,8]`

Note: You must use break keyword to break out of the loop if there is no swap happening in current iteration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment