📖 Scenario: You have a list of numbers that you want to arrange from smallest to largest. This is like sorting your books by height on a shelf so they look neat and easy to find.
🎯 Goal: You will build a simple program that sorts a list of numbers using the bubble sort method. This method compares numbers next to each other and swaps them if they are in the wrong order. After repeating this many times, the list becomes sorted.
📋 What You'll Learn
Create an array of integers with exact values
Use a variable to hold the size of the array
Implement the bubble sort algorithm using nested loops
Print the sorted array in the correct order
💡 Why This Matters
🌍 Real World
Sorting is used in many places like arranging names in a phone book, organizing files, or preparing data for searching.
💼 Career
Understanding sorting algorithms like bubble sort helps in writing efficient code and solving problems in software development.
Progress0 / 4 steps
1
Create the array of numbers
Create an integer array called numbers with these exact values: 5, 3, 8, 4, 2.
DSA C
Hint
Use curly braces to list the numbers inside the array.
2
Set the size of the array
Create an integer variable called size and set it to the number of elements in the numbers array, which is 5.
DSA C
Hint
Count how many numbers are in the array and assign that number to size.
3
Implement the bubble sort algorithm
Write nested for loops to sort the numbers array using bubble sort. Use i and j as loop variables. Inside the inner loop, compare numbers[j] and numbers[j + 1]. If numbers[j] is greater, swap them using a temporary variable called temp.
DSA C
Hint
Remember to reduce the inner loop range by i each time because the largest numbers settle at the end.
4
Print the sorted array
Use a for loop with variable i to print each element of the numbers array separated by spaces. Use printf inside the loop and print a newline after the loop.
DSA C
Hint
Use printf("%d ", numbers[i]) inside the loop and printf("\n") after the loop.
Practice
(1/5)
1. What is the main idea behind the Bubble Sort Algorithm?
easy
A. Use a pivot to partition the array into smaller parts
B. Divide the array into halves and sort each half separately
C. Repeatedly swap adjacent elements if they are in the wrong order
D. Insert each element into its correct position in a sorted sublist
Solution
Step 1: Understand the sorting method
Bubble sort works by comparing adjacent elements and swapping them if they are out of order.
Step 2: Identify the correct description
Repeatedly swap adjacent elements if they are in the wrong order describes this swapping of neighbors repeatedly until sorted.
Final Answer:
Repeatedly swap adjacent elements if they are in the wrong order -> Option C
Quick Check:
Bubble sort = swapping neighbors [OK]
Hint: Bubble sort swaps neighbors repeatedly until sorted [OK]
Common Mistakes:
Confusing bubble sort with divide and conquer methods
Thinking bubble sort uses a pivot element
Mixing bubble sort with insertion sort logic
2. Which of the following is the correct syntax for the inner loop of bubble sort in C to compare adjacent elements in an array arr of size n?
easy
A. for (int j = 0; j <= n - i; j++)
B. for (int j = 0; j < n - i - 1; j++)
C. for (int j = 1; j < n; j++)
D. for (int j = 0; j < n; j++)
Solution
Step 1: Understand the inner loop range
The inner loop runs from 0 to n - i - 1 because after each pass, the last i elements are sorted.
Step 2: Match the correct loop syntax
for (int j = 0; j < n - i - 1; j++) correctly uses for (int j = 0; j < n - i - 1; j++) to avoid out-of-bound access.
Final Answer:
for (int j = 0; j < n - i - 1; j++) -> Option B
Quick Check:
Inner loop limit = n - i - 1 [OK]
Hint: Inner loop stops before last sorted elements [OK]
Common Mistakes:
Using j < n causes out-of-bound access
Starting j from 1 misses first element comparison
Using <= instead of < causes extra iteration
3. What will be the output of the following C code snippet?
int arr[] = {4, 2, 3};
int n = 3;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
medium
A. 2 4 3
B. 4 3 2
C. 3 2 4
D. 2 3 4
Solution
Step 1: Trace first pass of bubble sort
Compare 4 and 2, swap -> arr = {2, 4, 3}; then compare 4 and 3, swap -> arr = {2, 3, 4}
Step 2: Trace second pass and final array
Second pass compares 2 and 3 (no swap), 3 and 4 (no swap). Array remains {2, 3, 4}.
Final Answer:
2 3 4 -> Option D
Quick Check:
Sorted array = 2 3 4 [OK]
Hint: Bubble sort sorts array ascending by swapping neighbors [OK]
Common Mistakes:
Not swapping correctly on first pass
Printing unsorted array
Confusing loop limits causing partial sort
4. Identify the error in the following bubble sort code snippet and select the correct fix:
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; 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 for (int i = 0; i < n - 1; i++)
B. Change inner loop to for (int j = 1; j < n; j++)
C. Remove the swap inside the if condition
D. Change outer loop to for (int i = 1; i < n; i++)
Solution
Step 1: Analyze loop ranges for bubble sort
The outer loop should be for (int i = 0; i < n - 1; i++) because after n-1 passes, array is sorted, avoiding an unnecessary pass.
Step 2: Identify the fix
Change outer loop to for (int i = 0; i < n - 1; i++) correctly optimizes by limiting to n-1 passes.
Final Answer:
Change outer loop to for (int i = 0; i < n - 1; i++) -> Option A
Quick Check:
Outer loop limit = n - 1 [OK]
Hint: Outer loop runs n-1 times in bubble sort [OK]
Common Mistakes:
Running outer loop n times wastes time
Changing inner loop start incorrectly
Removing swap breaks sorting logic
5. Given an array arr = {5, 1, 4, 2, 8}, how many total swaps will bubble sort perform to sort it in ascending order?