Untitled
unknown
javascript
2 years ago
477 B
8
Indexable
// bubble Sort
function bubbleSort(arr){
n=arr.length
for(let i=0; i<n; i++){
let isSwap=false
for(let j=0; j<n-i-1;j++){
if(arr[j]>arr[j+1]){
let temp = arr[j]
arr[j]=arr[j+1]
arr[j+1]=temp
console.log("1")
console.log(arr)
isSwap=true
}
}if (isSwap==false)return;
}
}
arr = [1,2,3,4,5]
bubbleSort(arr)
console.log(arr)Editor is loading...
Leave a Comment