Implement Bubble sort with negative and positive numbers – JavaScript?

Bubble sort is a simple sorting algorithm that works by repeatedly comparing adjacent elements and swapping them if they're in the wrong order. It works equally well with arrays containing both negative and positive numbers.

Let's say the following is our unsorted array with negative and positive numbers:

var arr = [10, -22, 54, 3, 4, 45, 6];

How Bubble Sort Works

Bubble sort compares each pair of adjacent elements and swaps them if the first element is greater than the second. This process continues until no more swaps are needed, meaning the array is sorted.

Implementation

Following is the code to implement Bubble Sort:

function bubbleSort(numberArray, size) {
    for (var lastIndex = size - 1; lastIndex > 0; lastIndex--) {
        for (var i = 0; i < lastIndex; i++) {
            if (numberArray[i] > numberArray[i + 1]) {
                var temp = numberArray[i];
                numberArray[i] = numberArray[i + 1];
                numberArray[i + 1] = temp;
            }
        }
    }
    return numberArray;
}

var arr = [10, -22, 54, 3, 4, 45, 6];
console.log("Original array:", arr);
console.log("Sorted array:", bubbleSort(arr, arr.length));
Original array: [ 10, -22, 54, 3, 4, 45, 6 ]
Sorted array: [ -22, 3, 4, 6, 10, 45, 54 ]

Step-by-Step Process

Here's how the algorithm sorts our array with negative and positive numbers:

function bubbleSortWithSteps(arr) {
    console.log("Initial array:", arr);
    var n = arr.length;
    
    for (var i = 0; i < n - 1; i++) {
        console.log(`\nPass ${i + 1}:`);
        for (var j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                // Swap elements
                var temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                console.log(`  Swapped ${temp} and ${arr[j]}: [${arr.join(', ')}]`);
            }
        }
    }
    return arr;
}

var numbers = [10, -22, 54, 3, 4, 45, 6];
bubbleSortWithSteps(numbers);
Initial array: [ 10, -22, 54, 3, 4, 45, 6 ]

Pass 1:
  Swapped 10 and -22: [-22, 10, 54, 3, 4, 45, 6]
  Swapped 54 and 3: [-22, 10, 3, 54, 4, 45, 6]
  Swapped 54 and 4: [-22, 10, 3, 4, 54, 45, 6]
  Swapped 54 and 45: [-22, 10, 3, 4, 45, 54, 6]
  Swapped 54 and 6: [-22, 10, 3, 4, 45, 6, 54]

Pass 2:
  Swapped 10 and 3: [-22, 3, 10, 4, 45, 6, 54]
  Swapped 10 and 4: [-22, 3, 4, 10, 45, 6, 54]
  Swapped 45 and 6: [-22, 3, 4, 10, 6, 45, 54]

Pass 3:
  Swapped 10 and 6: [-22, 3, 4, 6, 10, 45, 54]

Pass 4:

Pass 5:

Pass 6:

Key Points

  • Negative numbers naturally sort to the left since they're smaller than positive numbers
  • The algorithm compares values numerically, so -22
  • Time complexity: O(n²) in worst case, O(n) in best case (already sorted)
  • Space complexity: O(1) - sorts in place

Conclusion

Bubble sort handles negative and positive numbers seamlessly by comparing their numerical values. While not the most efficient for large datasets, it's simple to understand and implement for educational purposes.

Updated on: 2026-03-15T23:19:00+05:30

973 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements