Bird
Raised Fist0
DSA C#programming~20 mins

Bubble Sort Algorithm in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bubble Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Bubble Sort on a small array
What is the output of the following C# code after performing bubble sort on the array?
DSA C#
int[] arr = {5, 3, 8, 4, 2};

for (int i = 0; i < arr.Length - 1; i++) {
    for (int j = 0; j < arr.Length - i - 1; j++) {
        if (arr[j] > arr[j + 1]) {
            int temp = arr[j];
            arr[j] = arr[j + 1];
            arr[j + 1] = temp;
        }
    }
}

Console.WriteLine(string.Join(" -> ", arr) + " -> null");
A8 -> 5 -> 4 -> 3 -> 2 -> null
B2 -> 3 -> 4 -> 5 -> 8 -> null
C5 -> 3 -> 8 -> 4 -> 2 -> null
D2 -> 3 -> 5 -> 4 -> 8 -> null
Attempts:
2 left
💡 Hint
Remember bubble sort swaps adjacent elements if they are in the wrong order, pushing the largest to the end each pass.
🧠 Conceptual
intermediate
1:00remaining
Number of passes in Bubble Sort
For an array of length n, how many passes (outer loop iterations) does the bubble sort algorithm perform in the worst case?
An - 1 passes
Bn / 2 passes
Cn + 1 passes
Dn passes
Attempts:
2 left
💡 Hint
Each pass places the largest unsorted element at the end, so one less pass than the array length is needed.
🔧 Debug
advanced
2:00remaining
Identify the error in this Bubble Sort implementation
What error will this C# bubble sort code produce when run?
DSA C#
int[] arr = {4, 2, 7, 1};

for (int i = 0; i <= arr.Length - 1; i++) {
    for (int j = 0; j < arr.Length - i - 1; j++) {
        if (arr[j] > arr[j + 1]) {
            int temp = arr[j];
            arr[j] = arr[j + 1];
            arr[j + 1] = temp;
        }
    }
}

Console.WriteLine(string.Join(" -> ", arr) + " -> null");
ANo error, outputs sorted array
BNullReferenceException
CCompilation error
DIndexOutOfRangeException
Attempts:
2 left
💡 Hint
Check the loop boundaries carefully to see if any index goes out of range.
❓ Predict Output
advanced
2:00remaining
Output after partial bubble sort passes
What is the state of the array after the first pass of bubble sort on this array?
DSA C#
int[] arr = {9, 7, 5, 3, 1};

for (int j = 0; j < arr.Length - 1; j++) {
    if (arr[j] > arr[j + 1]) {
        int temp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = temp;
    }
}

Console.WriteLine(string.Join(" -> ", arr) + " -> null");
A1 -> 3 -> 5 -> 7 -> 9 -> null
B9 -> 7 -> 5 -> 3 -> 1 -> null
C7 -> 5 -> 3 -> 1 -> 9 -> null
D7 -> 9 -> 5 -> 3 -> 1 -> null
Attempts:
2 left
💡 Hint
One pass moves the largest element to the end by swapping adjacent pairs.
🧠 Conceptual
expert
1:30remaining
Optimization to stop Bubble Sort early
Which modification to the bubble sort algorithm can stop it early if the array is already sorted?
AUse recursion instead of loops
BReduce the inner loop length by half each pass
CSort only the first half of the array
DAdd a boolean flag to check if any swap happened in a pass; stop if none
Attempts:
2 left
💡 Hint
If no swaps occur during a pass, the array is sorted and we can stop.

Practice

(1/5)
1. What is the main idea behind the Bubble Sort algorithm?
easy
A. Divide the list into halves and sort each half separately
B. Insert each element into its correct position in a sorted sublist
C. Use a pivot to partition the list into smaller parts
D. Repeatedly swap adjacent elements if they are in the wrong order

Solution

  1. Step 1: Understand Bubble Sort process

    Bubble Sort compares adjacent elements and swaps them if they are out of order.
  2. Step 2: Identify the correct description

    This swapping of neighbors continues until the list is sorted, which matches Repeatedly swap adjacent elements if they are in the wrong order.
  3. Final Answer:

    Repeatedly swap adjacent elements if they are in the wrong order -> Option D
  4. Quick Check:

    Bubble Sort = Swap neighbors [OK]
Hint: Bubble Sort swaps neighbors repeatedly until sorted [OK]
Common Mistakes:
  • Confusing Bubble Sort with Quick Sort or Merge Sort
  • Thinking it divides the list into parts
  • Assuming it inserts elements like Insertion Sort
2. Which of the following is the correct syntax to swap two elements arr[i] and arr[i+1] in C# during Bubble Sort?
easy
A. temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp;
B. arr[i] = arr[i+1]; arr[i+1] = arr[i];
C. arr[i], arr[i+1] = arr[i+1], arr[i];
D. swap(arr[i], arr[i+1]);

Solution

  1. Step 1: Understand swapping in C#

    C# does not support tuple unpacking or direct swap functions by default, so a temporary variable is needed.
  2. Step 2: Identify correct swap syntax

    Using a temporary variable to hold one value, then swapping is correct as in temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp;.
  3. Final Answer:

    temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; -> Option A
  4. Quick Check:

    Use temp variable to swap [OK]
Hint: Use a temp variable to swap two elements in C# [OK]
Common Mistakes:
  • Trying to swap without a temp variable
  • Using Python-style tuple swap syntax
  • Assuming a built-in swap function exists
3. What will be the output of the following C# code snippet after applying one full pass of Bubble Sort?
int[] arr = {5, 3, 8, 4};
for (int i = 0; i < arr.Length - 1; i++) {
  if (arr[i] > arr[i + 1]) {
    int temp = arr[i];
    arr[i] = arr[i + 1];
    arr[i + 1] = temp;
  }
}
Console.WriteLine(string.Join(", ", arr));
medium
A. 3, 5, 4, 8
B. 3, 4, 5, 8
C. 5, 3, 4, 8
D. 8, 5, 4, 3

Solution

  1. Step 1: Perform one pass of Bubble Sort on array

    Start with arr = [5, 3, 8, 4]. Compare pairs:
    5 > 3? Yes, swap -> [3, 5, 8, 4]
    5 > 8? No swap
    8 > 4? Yes, swap -> [3, 5, 4, 8]
  2. Step 2: Final array after one pass

    After one pass, array is [3, 5, 4, 8].
  3. Final Answer:

    3, 5, 4, 8 -> Option A
  4. Quick Check:

    One pass swaps neighbors to bubble largest to end [OK]
Hint: One pass moves largest element to the end [OK]
Common Mistakes:
  • Assuming full sort after one pass
  • Not swapping last pair correctly
  • Confusing indices in loop
4. Identify the error in this Bubble Sort code snippet and choose the correct fix:
int[] arr = {4, 2, 7, 1};
for (int i = 0; i < arr.Length; i++) {
  for (int j = 0; j < arr.Length; j++) {
    if (arr[j] > arr[j + 1]) {
      int temp = arr[j];
      arr[j] = arr[j + 1];
      arr[j + 1] = temp;
    }
  }
}
medium
A. Change outer loop to i < arr.Length - 1
B. Change inner loop condition to j < arr.Length - 1
C. Remove the inner loop completely
D. Swap arr[i] and arr[j] instead of arr[j] and arr[j+1]

Solution

  1. Step 1: Analyze inner loop range

    The inner loop uses j from 0 to arr.Length, but accesses arr[j+1]. This causes out-of-range error when j = arr.Length - 1.
  2. Step 2: Fix inner loop condition

    Changing inner loop to j < arr.Length - 1 prevents accessing arr[arr.Length], fixing the error.
  3. Final Answer:

    Change inner loop condition to j < arr.Length - 1 -> Option B
  4. Quick Check:

    Prevent out-of-range by limiting inner loop [OK]
Hint: Inner loop must stop before last index to avoid errors [OK]
Common Mistakes:
  • Not adjusting inner loop range
  • Changing outer loop incorrectly
  • Misunderstanding which indices to swap
5. Given the array int[] arr = {7, 2, 9, 4, 3}, how many total swaps will Bubble Sort perform to fully sort the array in ascending order?
hard
A. 4
B. 5
C. 6
D. 7

Solution

  1. Step 1: Trace swaps during Bubble Sort passes

    Initial array: [7, 2, 9, 4, 3]
    Pass 1:
    7 > 2? Swap -> [2, 7, 9, 4, 3] (1 swap)
    7 > 9? No swap
    9 > 4? Swap -> [2, 7, 4, 9, 3] (2 swaps)
    9 > 3? Swap -> [2, 7, 4, 3, 9] (3 swaps)
    Pass 2:
    2 > 7? No swap
    7 > 4? Swap -> [2, 4, 7, 3, 9] (4 swaps)
    7 > 3? Swap -> [2, 4, 3, 7, 9] (5 swaps)
    Pass 3:
    2 > 4? No swap
    4 > 3? Swap -> [2, 3, 4, 7, 9] (6 swaps)
    Pass 4:
    No swaps needed, array sorted.
  2. Step 2: Count total swaps

    Total swaps = 6.
  3. Final Answer:

    6 -> Option C
  4. Quick Check:

    Count swaps each pass carefully [OK]
Hint: Count swaps each pass until no swaps remain [OK]
Common Mistakes:
  • Counting only first pass swaps
  • Forgetting swaps in later passes
  • Assuming swaps equal number of elements
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard