Skip to content

Instantly share code, notes, and snippets.

@mshossain110
Last active October 5, 2024 12:20
Show Gist options
  • Save mshossain110/a9d7645bf8dd066cd984e0b7d9638502 to your computer and use it in GitHub Desktop.
Save mshossain110/a9d7645bf8dd066cd984e0b7d9638502 to your computer and use it in GitHub Desktop.
Bubble sort implement using JavaScript
var arr = [10, 55, 20, 4, 28, 69, 22, 85, 7, 37];
function bubbleSort(arr)
{
var temp, i, j;
for(i = 0; i<arr.length; i++)
{
for(j = 0; j< arr.length; j++)
{
if (arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
return arr;
}
console.log(bubbleSort(arr));
@mshossain110
Copy link
Author

Bubble Sort
Description: Repeatedly swaps adjacent elements if they are in the wrong order.
Time Complexity: O(n²)
Use Case: Simple but inefficient for large datasets.

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