Bubble Sort Algorithm B.Tech/BCA Notes 2025 with Time Complexity PDF

The Bubble Sort Algorithm is a fundamental sorting technique taught in most computer science and engineering programs. Despite its simplicity, Bubble Sort is a cornerstone in understanding sorting logic and algorithm design. In the context of B.Tech Data Structures Notes 2025, this algorithm is often covered in Unit 2 Notes Data Structures as a basic comparison-based technique that introduces the iterative sorting paradigm. Students studying under various Indian universities or pursuing Engineering Study Material for internal exams will find this chapter essential.

Bubble Sort works by repeatedly swapping adjacent elements if they are in the wrong order. This process is repeated until the entire list is sorted. The largest unsorted element “bubbles up” to its correct position in each iteration, which is how the algorithm gets its name. Its implementation is straightforward but not efficient for large datasets. However, due to its conceptual simplicity, it remains widely taught.

Let us consider an array A of n elements. The Bubble Sort compares each element A[i] with its adjacent A[i+1]. If A[i] > A[i+1], the two elements are swapped. This process continues from the start of the array to the end and is repeated for all elements until no swaps are needed, which indicates the array is sorted.

Basic Working Principle

In each pass through the array, Bubble Sort pushes the largest unsorted value to its correct position. If there are n elements in the array, Bubble Sort requires at most n-1 passes. This leads to a time complexity that is quadratic in the worst and average cases.

Example

Consider an unsorted array: A = [5, 3, 8, 4, 2]

Pass 1:

  • Compare 5 and 3 β†’ Swap β†’ [3, 5, 8, 4, 2]
  • Compare 5 and 8 β†’ No Swap
  • Compare 8 and 4 β†’ Swap β†’ [3, 5, 4, 8, 2]
  • Compare 8 and 2 β†’ Swap β†’ [3, 5, 4, 2, 8]

Pass 2:

  • Compare 3 and 5 β†’ No Swap
  • Compare 5 and 4 β†’ Swap β†’ [3, 4, 5, 2, 8]
  • Compare 5 and 2 β†’ Swap β†’ [3, 4, 2, 5, 8]
  • Compare 5 and 8 β†’ No Swap

Pass 3:

  • Compare 3 and 4 β†’ No Swap
  • Compare 4 and 2 β†’ Swap β†’ [3, 2, 4, 5, 8]
  • Compare 4 and 5 β†’ No Swap
  • Compare 5 and 8 β†’ No Swap

Pass 4:

  • Compare 3 and 2 β†’ Swap β†’ [2, 3, 4, 5, 8]
  • Remaining comparisons do not require swapping.

After 4 passes, the array is sorted.

Bubble Sort Algorithm (Pseudocode)

Procedure BubbleSort(A[0..n-1])
   for i = 0 to n-1 do
      for j = 0 to n-2-i do
         if A[j] > A[j+1] then
            swap A[j] and A[j+1]
         end if
      end for
   end for
End Procedure

Optimized Bubble Sort

An optimization to the basic algorithm involves stopping early if the array becomes sorted before all passes are complete. This can be achieved by adding a Boolean variable swapped. If no two elements were swapped in a pass, the array is already sorted, and we can break early.

Optimized Bubble Sort (Pseudocode)

Procedure BubbleSortOptimized(A[0..n-1])
   for i = 0 to n-1 do
      swapped = false
      for j = 0 to n-2-i do
         if A[j] > A[j+1] then
            swap A[j] and A[j+1]
            swapped = true
         end if
      end for
      if swapped = false then
         break
      end if
   end for
End Procedure

Time and Space Complexity

CaseTime Complexity
Best CaseO(n)
Average CaseO(n^2)
Worst CaseO(n^2)
  • Best Case: When the array is already sorted. Only one pass is made with no swaps. Hence, O(n).
  • Average Case: Array is randomly ordered, leading to approximately n(n-1)/4 comparisons β†’ O(n^2).
  • Worst Case: Array is sorted in reverse order β†’ Maximum number of swaps and comparisons β†’ O(n^2).

Space Complexity: Bubble Sort is an in-place sorting algorithm, meaning it requires O(1) auxiliary space.

Stability and Use Cases

Bubble Sort is a stable sorting algorithm because it does not change the relative order of equal elements. Although it is inefficient for large data sets, its stability makes it suitable for small datasets where stability matters. It’s commonly used in academic exercises to teach basic sorting principles, particularly for BCA and B.Tech students studying Engineering Study Material or revising from Unit 2 Notes Data Structures.

πŸ“Œ Insert Diagram: Flowchart of Bubble Sort Algorithm

Dry Run Example

Let’s dry run the optimized Bubble Sort on: A = [6, 2, 7, 1]

Initial: [6, 2, 7, 1]
Pass 1:
- 6 > 2 β†’ Swap β†’ [2, 6, 7, 1]
- 6 < 7 β†’ No Swap
- 7 > 1 β†’ Swap β†’ [2, 6, 1, 7]
Pass 2:
- 2 < 6 β†’ No Swap
- 6 > 1 β†’ Swap β†’ [2, 1, 6, 7]
- 6 < 7 β†’ No Swap
Pass 3:
- 2 > 1 β†’ Swap β†’ [1, 2, 6, 7]
- Remaining: No Swap

Array sorted after 3 passes.

Real-life Analogy

Imagine students in a queue trying to arrange themselves by height. Each student compares with the next one and swaps positions if the next student is shorter. They continue until the tallest reaches the end, and the process is repeated for remaining students. This analogy aligns well with the Bubble Sort process.

Comparison with Other Sorting Algorithms

AlgorithmTime Complexity (Avg)SpaceStable
Bubble SortO(n^2)O(1)Yes
Selection SortO(n^2)O(1)No
Insertion SortO(n^2)O(1)Yes
Merge SortO(n log n)O(n)Yes
Quick SortO(n log n)O(log n)No

πŸ“Œ Insert Diagram: Comparison of Sorting Algorithms (Bar Chart Style)

πŸ”Ή Short Answer Questions (2-3 Marks)

  1. What is the basic principle of the Bubble Sort algorithm? β€” It compares adjacent elements and swaps them if they are in the wrong order.
  2. When does the Bubble Sort algorithm stop early? β€” When no swaps occur in a complete pass, indicating the array is already sorted.
  3. Is Bubble Sort a stable algorithm? β€” Yes, it preserves the relative order of equal elements.
  4. What is the best-case time complexity of Bubble Sort? β€” O(n)
  5. Why is Bubble Sort inefficient for large datasets? β€” Because its average and worst-case time complexity is O(n^2).
  6. What is the auxiliary space requirement for Bubble Sort? β€” O(1), as it is an in-place sorting algorithm.
  7. How many passes does Bubble Sort take to sort an array of size n? β€” Up to (n-1) passes in the worst case.

πŸ”Έ Long Answer Questions (5–10 Marks)

  1. Explain the working of the Bubble Sort algorithm with a detailed example. β€” Answer: Refer to the earlier dry run example with step-by-step passes and how the largest elements bubble to the top.
  2. Write the pseudocode for both standard and optimized Bubble Sort. Discuss the improvement. β€” Answer: Standard Bubble Sort runs for all passes regardless of sorting state. Optimized Bubble Sort breaks early if no swaps are made, improving best-case time complexity from O(n^2) to O(n).
  3. Compare Bubble Sort with Merge Sort and Insertion Sort. Highlight the key differences. β€” Answer: Merge Sort is faster (O(n log n)) but requires extra space; Insertion Sort is stable and works better for small or nearly sorted arrays; Bubble Sort is easiest to understand but slowest for large data.
  4. What are the practical applications of Bubble Sort in real-life or industry? β€” Answer: Mainly in academic and educational settings to teach algorithmic thinking and basic concepts of sorting, due to its simplicity.

πŸ“ Chapter Summary

Bubble Sort is a comparison-based algorithm that works by swapping adjacent elements if they are in the wrong orderIt requires at most (n-1) passes for sorting an array of n elementsIn the best case, its time complexity is O(n), while average and worst cases are O(n^2)It is a stable, in-place algorithm requiring O(1) extra spaceOptimized Bubble Sort improves performance by breaking early if the array becomes sorted before all passesIt is mainly used for teaching purposes in B.Tech and BCA curriculum as part of Unit 2 Notes Data Structures

πŸ“˜ Model Paper / Sample Internal Exam Questions

  1. Explain the Bubble Sort algorithm with a real-time example and derive its time complexity.
  2. Write an optimized pseudocode for Bubble Sort and explain the conditions for early termination.
  3. Compare and contrast Bubble Sort and Selection Sort with a suitable example.
  4. Illustrate the working of Bubble Sort on the following array: [10, 3, 15, 7, 8]
  5. What is meant by a stable sorting algorithm? Is Bubble Sort stable? Justify your answer.

β€œClick below to download this chapter as PDF”

Leave a Reply

Your email address will not be published. Required fields are marked *