Bubble Sort – Report
1. Introduction
Bubble Sort is one of the simplest sorting algorithms in computer science. It is a comparison-based
algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if
they are in the wrong order. The process is repeated until the list is sorted.
2. How It Works
Bubble Sort works by iterating through a list multiple times. During each pass:
• Adjacent elements are compared.
• If the current element is greater than the next (for ascending order), they are swapped.
• After each pass, the largest unsorted element "bubbles up" to its correct position.
3. Algorithm (Pseudocode)
plaintext
CopyEdit
for i from 0 to n-1:
for j from 0 to n-i-2:
if array[j] > array[j+1]:
swap array[j] and array[j+1]
4. Time and Space Complexity
Case Time Complexity
Best Case O(n) (if optimized with a swapped flag)
Average Case O(n²)
Worst Case O(n²)
• Space Complexity: O(1) – in-place sorting.
5. Example
For the list: [5, 3, 8, 4, 2]
• Pass 1: [3, 5, 4, 2, 8]
• Pass 2: [3, 4, 2, 5, 8]
• Pass 3: [3, 2, 4, 5, 8]
• Pass 4: [2, 3, 4, 5, 8]
6. Advantages
• Simple to implement and understand.
• No extra space required (in-place).
• Useful for small datasets or as an educational tool.
7. Disadvantages
• Very inefficient on large lists.
• Not suitable for real-world use cases with large data.
8. Applications
• Teaching and learning sorting concepts.
• Small datasets where performance is not critical.