JavaScript Algorithms - 21 - Bubble Sort Solution

preview_player
Показать описание

📱 Follow Codevolution

Bubble Sort Solution
JavaScript Algorithms
Algorithms with JavaScript
Рекомендации по теме
Комментарии
Автор

Bubble sort in Recursion:


function BubbleSort(arr, swapped=false){
swapped = false;
for(let i = 0; i < arr.length -1;i++){
if(arr[i] > arr[i+1]){
let temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
swapped = true
}
}
if(!swapped){
return arr;
} else {
return BubbleSort(arr, swapped)
}
}

HariPrasad-qehd
Автор

As a beginner it felt so good to come to this video with the solution I made in the last video when you asked us to try first, and see that I had nearly everything exactly similar, down to the variable names, save for a check I had to see if the last element was empty rather than stopping the loop at arr.length-1. Thank you so much for this course and your clear teaching style.

mindspray
Автор

I improved the efficiency by accounting for how this algorithm sorts right to left so to speak. There isn't a need to always compare until the end of the array every pass since we're sorting one number at a time essentially.

const bubbleSort = arr => {
let swapped;
let j = 0;
do {
swapped = false;
for (let i = 0; i < arr.length - j - 1; i++) {
if (arr[i] > arr[i + 1]) {
let temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = true;
}
} j++;
} while (swapped)
return arr;
}

console.log(bubbleSort([8, 20, -2, 4, -6]))

Herman_Hoffman
Автор

Thank you for the great content and your efforts.

nagendra
Автор

Good job, thanks!

The intresting fact, if you ask chatGPT or Cloud 3.5 generate the Bubble Sort algo, they will do it without the flag var but it's inefficient.

YuriiKratser
Автор

there is an optimisation you could do by making the -1 in the for loop a variable and increasing it by 1 every time you do a pass

djneils
Автор

There is a better, more optimised implementation to the bubble sort algorithm with iterating from the back of the array to beginning - after first iteration of the loop we are sure that smallest element is on the 0th index. After second iteration second smallest is on 1 and so on...

navuyi
Автор

Hi bro, can also decrease the length of array for each passes once loop completed
, it will also more efficient

pradeepkumaresan-trqy
Автор

your method is batter than mine
let array = [ 5, 2, 1, -6, 8]

function bubble(arr){
for(let i =0; i < arr.length; i++){
for(let j= i +1; j < arr.length; j++){
if(arr[i] > arr[j]){
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr
}


console.log(bubble(array))

rishabhk
Автор

Do we have to use do while loop ?because below code also returns in sorted order :-
function bubble(){
const array = [-5, -2, 20, 4, 6, 10]
for(let i=0;i<array.length-1;i++){
if(array[i]>array[i+1]){
const temp = array[i]
array[i] = array[i+1]
array[i+1] = temp

}

}
return array

}

console.log(bubble());

dokhangtseringnamgyal
Автор

Here's mine, it works I think but definitely isn't efficient/ has more lines of code than necessary: function bubble(arr) {
let sorted = 0;
function sort(arr) {
sorted = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > arr[i + 1]) {
sorted = 1;
let a = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = a;
}
}
return arr;
}
do {
sort(arr);
} while (sorted > 0);
console.log(arr);
return arr;
}

armoredchimp
Автор

is this method wrong:

function bubbleSort(arr){

let swapped = true
while(swapped) {
swapped = false
for(let i = 0; i < arr.length; i++){
let temporary= arr[i]
if(arr[i] > arr[i + 1]){
arr[i] = arr[i + 1]
arr[i + 1 ] = temporary
swapped = true

}

}
}
return arr
}
console.log(bubbleSort([-2, -4, 9, 2, 9, -19, 4]))

rockedwow
Автор

Hello Sir, the font size is too much small, it is very hard to read on small sized devices...

zafarali
Автор

Just wondering how the function returned nothing yet we got the result

vokeolomu
Автор

Interesting way to write the if statement:

if (arr[i] > arr[i + 1]) {
[arr[i], arr[i + 1]] = [arr[i + 1], arr[i]];
swapped = true;
}

richardtbohnen
Автор

there is an extra optimisation in each for loop the last element will be sorted you don't need to check it again so the best code is :
function swap(v1, v2) {
return [v2, v1];
}
// big-O=O(n^2)
function bubbleSort(arr) {
let isSwapped = false;

for (let i = 0; i <= arr.length; i++) {
isSwap = false;
for (let j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
isSwap = true;
[arr[j], arr[j + 1]] = swap(arr[j], arr[j + 1]);
}
}
if (!isSwapped) return;
}
}

Ayoubmajid-uuyv
Автор

My solution was different. Not sure if it's as efficient:

const bubbleSort = arr => {
for (let j = 0; j < arr.length; j++) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] > arr[i + 1]) {
const temp = arr[i]
arr[i] = arr[i + 1]
arr[i + 1] = temp
}
}
}
return arr
}

JonathanKila
Автор

I used to do this with an even less optimized solution by using nested for loop

md.mohaiminulislam
Автор

My ugly solution () :
function bubbleSort(arr){
let elementsToSwap = true;

while(elementsToSwap){
for(let i = 1; i < arr.length;++i){
if(arr[i-1] < arr[i]){
elementsToSwap = false;
}else if(arr[i-1] > arr[i]){
elementsToSwap = true;
break;
}
}
for(let i = 1; i < arr.length;++i){
if(arr[i-1] > arr[i]){
let temp = arr[i-1];
arr[i-1] = arr[i]
arr[i] = temp
}
}
}
return arr;
}

oscargm
Автор

I used recursion

function bubbleSort(arr, elementsSwapped = false) {
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
[arr[i], arr[i + 1]] = [arr[i + 1], arr[i]];
elementsSwapped = true;
}
}

if (!elementsSwapped) return arr;
return bubbleSort(arr);
}

console.log(bubbleSort([-5, 50, -3, 25, 0, 2, -5, -5, -5, 25, 44, 34, -2, -2, -2]));

Xraxus_