DEV Community

Sudharsanan Ravichandran
Sudharsanan Ravichandran

Posted on

Bubble Sort

Hello Dev Community,

Happy to share my very first article. I recently started with #100DaysOfCode on Twitter. I got to meet awesome developers who were very good & unique.

Often saw many people sharing useful information for free to help fellow newbies & developers. This got me thinking, I haven't done much for the dev community and this article is just a brainchild.

I will try to go through one of the sorting algorithms (easiest to catch) that I learnt so far in the past few days.

Before we move on any further, I just want to say, it's easy!

We will start with the classic Bubble Sort algorithm

You can go through this video from HackerRank to get a hint on how it works?

https://www.youtube.com/watch?v=6Gv8vg0kcHc

In Bubble Sort, we compare two of the adjacent element and swap their places. Upon one successful iteration over the elements, you will get the largest of the number on the right-hand side of the array. We will not touch the sorted element (right-hand side) on the successive iteration.
You have to repeat the same until your array is sorted.

Below is a pure function based implementation

function bubbleSort(iterarr){

    if(!Array.isArray(iterarr)){ // Checking given input is a valid array
        console.error('Not an array!');
        return;
    }

    let arr = [...iterarr]; // Copied the array elements using spread operator

    if(arr.length == 1){
        return arr; //If array has only one element it's already sorted
    }

    let isSorted = false; //Assuming the array is not sorted

    while(!isSorted){ //Loop breaks if the array is sorted
        isSorted = true; //Assuming the array is sorted
        for(let i = 0; i < arr.length - 1; i++){ //Loop for iterating through elements
            if(arr[i] > arr[i+1]){ //Trying to check which of the element is bigger current(i) or the next element(i+1)
                let temp = arr[i];
                arr[i] = arr[i+1];
                arr[i+1] = temp; //Above 3 lines does the swapping using the temp variable
                isSorted = false; //If swap happens that means array is not sorted yet
            }
        }
    }
    return arr;
}

Though this is not an optimal sorting method, it's one of the easiest.

I hope you like this article. Please share any comments if you have, I will try to address them one step at a time.

Thanks,
Sudharsanan Ravichandran

Top comments (2)

Collapse
 
pentacular profile image
pentacular

A little abstraction can go a long way.

const order = (array, index) => {
  if (array[index] > array[index + 1]) {
    // These two elements are unordered, so swap them.
    [array[index], array[index + 1]] = [array[index + 1], array[index]];
    // Note that a swap occurred.
    return 1;
  } else {
    // Note that no swap occurred here.
    return 0;
  }
}

const bubbleSort = (array) => {
  for (;;) {
    let swapped = 0;
    for (let index = 0; index < array.length - 1; index++) {
      // Attempt to order each pair in turn, noting if a swap occurred.
      swapped |= order(array, index);
    }
    if (swapped === 0) {
      // If no swaps occurred the array is totally ordered.
      return array;
    }
  }
}
Collapse
 
sudharsanansr profile image
Sudharsanan Ravichandran • Edited

Thanks a lot for sharing your code, I can see a lot of improvements needed on my code and modified my version too 👍🏾😊