Over 10 years we helping companies reach their financial and branding goals. Onum is a values-driven SEO agency dedicated.

CONTACTS
HTML & CSS

Day 17 – Evening Session: Bubble Sort Code + Dry Run – ECET 2026 CSE (Data Structures)

In ECET 2026, Data Structures is one of the most important and high-scoring subjects for CSE diploma students. Sorting algorithms are a guaranteed topic in exams, and Bubble Sort is the simplest to learn and implement. Even though it’s not the fastest in real-world scenarios, it’s frequently asked in viva, lab exams, and MCQs.

In this session, we’ll go through Bubble Sort’s logic, C code, and a complete dry run so that you understand exactly how it works.


📘 Concept Notes – Bubble Sort

Definition:
Bubble Sort is a simple comparison-based sorting algorithm where adjacent elements are compared and swapped if they are in the wrong order. This process repeats until the list is sorted.


🔍 Algorithm Steps:

  1. Compare the first two elements.
  2. Swap them if they are in the wrong order.
  3. Move to the next pair and repeat.
  4. Continue until the end of the array.
  5. Repeat the above steps for n-1 passes until no swaps are needed.

🧠 Time Complexity:

  • Best Case: O(n) (already sorted list)
  • Worst Case: O(n²)
  • Space Complexity: O(1) (in-place sorting)

💻 C Program – Bubble Sort

#include <stdio.h>

void bubbleSort(int arr[], int n) {
    int i, j, temp;
    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                // Swap elements
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

int main() {
    int arr[] = {5, 3, 8, 4, 2};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Original array: ");
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);

    bubbleSort(arr, n);

    printf("\nSorted array: ");
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);

    return 0;
}

Dry Run Example

Input Array: {5, 3, 8, 4, 2}

Pass 1:

  • Compare 5 & 3 → swap → {3, 5, 8, 4, 2}
  • Compare 5 & 8 → no swap → {3, 5, 8, 4, 2}
  • Compare 8 & 4 → swap → {3, 5, 4, 8, 2}
  • Compare 8 & 2 → swap → {3, 5, 4, 2, 8}

Pass 2:

  • Compare 3 & 5 → no swap → {3, 5, 4, 2, 8}
  • Compare 5 & 4 → swap → {3, 4, 5, 2, 8}
  • Compare 5 & 2 → swap → {3, 4, 2, 5, 8}

Pass 3:

  • Compare 3 & 4 → no swap → {3, 4, 2, 5, 8}
  • Compare 4 & 2 → swap → {3, 2, 4, 5, 8}

Pass 4:

  • Compare 3 & 2 → swap → {2, 3, 4, 5, 8}

Final Sorted Array: {2, 3, 4, 5, 8}


🔟 10 Expected ECET 2026 MCQs – Bubble Sort

Q1. Bubble sort is based on:
A) Selection
B) Swapping
C) Merging
D) Partitioning

Q2. Best case time complexity of bubble sort is:
A) O(n²)
B) O(log n)
C) O(n)
D) O(1)

Q3. Bubble sort compares:
A) Random elements
B) Non-adjacent elements
C) Adjacent elements
D) None

Q4. In-place sorting means:
A) Uses extra array
B) Uses no extra space
C) Uses recursion
D) Uses linked list

Q5. If array is already sorted, bubble sort requires:
A) n passes
B) 1 pass
C) 0 passes
D) n² passes

Q6. Bubble sort is most efficient for:
A) Large datasets
B) Already sorted small datasets
C) Reverse sorted datasets
D) Linked lists

Q7. Bubble sort belongs to which category?
A) Divide & Conquer
B) Brute force
C) Comparison-based
D) Hashing

Q8. Which loop runs outer in bubble sort?
A) Inner loop
B) for (i = 0; i < n – 1; i++)
C) while loop
D) None

Q9. Worst-case complexity of bubble sort is:
A) O(n²)
B) O(n)
C) O(log n)
D) O(2ⁿ)

Q10. Bubble sort is also called:
A) Ripple sort
B) Quick sort
C) Shell sort
D) Heap sort


Answer Key Table

Q.NoAnswer
Q1B
Q2C
Q3C
Q4B
Q5B
Q6B
Q7C
Q8B
Q9A
Q10A

🧠 Explanations of All Answers

  • Q1 → B: Bubble sort swaps elements to sort.
  • Q2 → C: Best case O(n) if array already sorted.
  • Q3 → C: Always compares adjacent elements.
  • Q4 → B: In-place means no extra space used.
  • Q5 → B: Only 1 pass needed if sorted.
  • Q6 → B: Best for small already sorted lists.
  • Q7 → C: Compares and swaps to arrange.
  • Q8 → B: Outer loop is i loop in C code.
  • Q9 → A: Worst case O(n²) for reverse sorted.
  • Q10 → A: Bubble sort is also called Ripple sort.

🎯 Why This Practice Matters for ECET 2026

Sorting algorithms are common in ECET practicals and theory. Bubble Sort is the first sorting method taught in Data Structures, making it a favorite for direct MCQs and coding questions. By learning both code and dry run, you’ll score in programming-based questions without confusion.


📲 Join Our ECET Prep Community on Telegram

For daily coding practice, MCQs, and ECET notes,
👉 Join here: @LearnNewThingsHub

Leave a comment

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