Coding Challenge Practice – Question 36

The task is to implement a bubble sort which sorts an integer array in ascending order, modifying it in place.

The boilerplate code:

function bubbleSort(arr) {
// your code here
}

The BubbleSort is a sorting algorithm that sorts an array by…


This content originally appeared on DEV Community and was authored by Bukunmi Odugbesan

The task is to implement a bubble sort which sorts an integer array in ascending order, modifying it in place.

The boilerplate code:

function bubbleSort(arr) {
  // your code here
}

The BubbleSort is a sorting algorithm that sorts an array by comparing adjacent elements in an array, and swaps them if they are in the wrong order.

The outer loop ensures we go through the loop multiple times

for(let i = 0; i < arr.length - 1; i++) {
}

The inner loop compares adjacent elements

for(let j = 0; j < arr.length-1; j++) {
}

If the current element is larger than the next element, we swap them using a temporary variable

if (arr[j] > arr[j + 1]) {
  const temp = arr[j];
  arr[j] = arr[j + 1];
  arr[j + 1] = temp;
}

After every inner loop, one large element towards the end. The sorting is completed when the array loops one time less than the length of the array.

The final code:

function bubbleSort(arr) {
  // your code here
  for(let i = 0; i < arr.length - 1; i++) {
    for(let j = 0; j < arr.length - 1; j++) {
      if(arr[j] > arr[j + 1]) {
        const temp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = temp
      }
    }
  }
}

That's all folks!


This content originally appeared on DEV Community and was authored by Bukunmi Odugbesan


Print Share Comment Cite Upload Translate Updates
APA

Bukunmi Odugbesan | Sciencx (2025-10-25T21:26:31+00:00) Coding Challenge Practice – Question 36. Retrieved from https://www.scien.cx/2025/10/25/coding-challenge-practice-question-36/

MLA
" » Coding Challenge Practice – Question 36." Bukunmi Odugbesan | Sciencx - Saturday October 25, 2025, https://www.scien.cx/2025/10/25/coding-challenge-practice-question-36/
HARVARD
Bukunmi Odugbesan | Sciencx Saturday October 25, 2025 » Coding Challenge Practice – Question 36., viewed ,<https://www.scien.cx/2025/10/25/coding-challenge-practice-question-36/>
VANCOUVER
Bukunmi Odugbesan | Sciencx - » Coding Challenge Practice – Question 36. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/25/coding-challenge-practice-question-36/
CHICAGO
" » Coding Challenge Practice – Question 36." Bukunmi Odugbesan | Sciencx - Accessed . https://www.scien.cx/2025/10/25/coding-challenge-practice-question-36/
IEEE
" » Coding Challenge Practice – Question 36." Bukunmi Odugbesan | Sciencx [Online]. Available: https://www.scien.cx/2025/10/25/coding-challenge-practice-question-36/. [Accessed: ]
rf:citation
» Coding Challenge Practice – Question 36 | Bukunmi Odugbesan | Sciencx | https://www.scien.cx/2025/10/25/coding-challenge-practice-question-36/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.