Last active
October 5, 2024 12:20
-
-
Save mshossain110/a9d7645bf8dd066cd984e0b7d9638502 to your computer and use it in GitHub Desktop.
Bubble sort implement using JavaScript
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
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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.