Home | Projects | Notes > Data Structures & Algorithms > Bubble Sort: T = O(n2) , S = O(1)

Bubble Sort: T = O(n2) , S = O(1)

 

Bubble Sort

Algorithm

  1. Input: An array of elements to be sorted.

  2. Loop: Start a loop that will run for the total number of elements in the array.

  3. Inner Loop: Within the loop, iterate through the array from the beginning to the second-to-last element.

  4. Compare and Swap: For each pair of adjacent elements in the inner loop, compare them. If the elements are in the wrong order (i.e., the current element is greater than the next element), swap them.

  5. Repeat: Continue this process for each pair of adjacent elements in the array for each iteration of the outer loop.

  6. One Pass Complete: After each pass of the inner loop, the largest (or smallest, depending on the sorting order) element "bubbles up" to the end (or beginning) of the array.

  7. Reduce Inner Loop: In the next iteration of the outer loop, you can reduce the range of the inner loop by one, as the largest (or smallest) element is already in its correct position.

  8. Repeat Outer Loop: Repeat steps 3 to 7 for the remaining unsorted portion of the array until the entire array is sorted.

Pseudo-code

 

Code