Skip to content

Files

Latest commit

 

History

History

0008 BubbleSort

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

0008 BubbleSort ( L-I )

Problem

Implement a function that sorts an array in increasing order using bubble sort.

Testcases

  • Input: [0, 4, 2, 3, 7, 3, 6, 2, 9, 10, 2, 6]
    Output: [0, 2, 2, 2, 3, 3, 4, 6, 6, 7, 9, 10]

  • Input: [45, 32, 12, 67, 86, 92, 29, 24, 53, 1, 6, 32, 56]
    Output: [1, 6, 12, 24, 29, 32, 32, 45, 53, 56, 67, 86, 92]

Solution

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

How it works

  • The function takes an array of integers arr as an argument.
  • The function runs a for loop in i until the second last element. This is due to the fact that on every iteration of the i loop, the last i elements are sorted. Therefore, running the i loop for the first element is unnecessary.
  • The inner loop runs in j and will traverse arr from the start to the n - ith position since the last i elements are sorted.
  • The function terminates with the sorted arr.
  • The function will also work for those arrays that have duplicate elements.

References

Problem Added By

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.