C Program to Sort an Array using Bubble Sort

C Program to Sort an Array using Bubble Sort

A class stores five marks in their original order: 40, 35, 45, 35 and 50. The bubble sort task needs a clear rule about order, duplicates and how many elements exist.

Bubble Sort orders a sequence through a defined comparison and preserves its elements, including duplicates.

Before this exercise, review array elements and bounds and function parameters. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why bubble sort uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Order the same marks in the opposite direction using the worked insertion approach.

Bubble Sort: follow the data

  1. Given data

    A class stores five marks in their original order: 40, 35, 45, 35 and 50. The bubble sort task needs a clear rule about order, duplicates and how many elements exist.

  2. Rule

    Bubble Sort orders a sequence through a defined comparison and preserves its elements, including duplicates.

  3. Observed result

    35 35 40 45 50

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

Swap neighboring values when they are out of order. Each completed pass puts the largest remaining value at the end of the unsorted prefix.

The complete example knows its five-element bound. In a reusable function, pass an explicit element count with the array pointer; a pointer alone does not report that bound. This sample has two equal marks, which makes duplicate handling observable.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
int main(void) {
  int marks[] = {40, 35, 45, 35, 50};
  int count = (int)(sizeof marks / sizeof marks[0]);
  for (int end = count - 1; end > 0; end--) {
    int changed = 0;
    for (int i = 0; i < end; i++) {
      if (marks[i] > marks[i + 1]) {
        int saved = marks[i]; marks[i] = marks[i + 1]; marks[i + 1] = saved; changed = 1;
      }
    }
    if (!changed) break;
  }
  for (int i = 0; i < count; i++) printf("%d%s", marks[i], i == count - 1 ? "\n" : " ");
  return 0;
}

Expected output

35 35 40 45 50
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int marks[] = {40, 35, 45, 35, 50};
  int count = (int)(sizeof marks / sizeof marks[0]);
  for (int end = count - 1; end > 0; end--) {
    int changed = 0;
    for (int i = 0; i < end; i++) {
      if (marks[i] > marks[i + 1]) {
        int saved = marks[i]; marks[i] = marks[i + 1]; marks[i + 1] = saved; changed = 1;
      }
    }
    if (!changed) break;
  }
  for (int i = 0; i < count; i++) printf("%d%s", marks[i], i == count - 1 ? "\n" : " ");
  return 0;
}

The first pass changes 40,35,45,35,50 into 35,40,35,45,50. Another pass moves the remaining 35 left.

Compare the resulting output with the original sequence. A changed order and a changed set of values are different effects; decide which your operation promises.

Follow the worked calculation

  1. Start with the sample

    The first pass changes 40,35,45,35,50 into 35,40,35,45,50. Another pass moves the remaining 35 left.

  2. Apply the rule

    Compare the resulting output with the original sequence. A changed order and a changed set of values are different effects; decide which your operation promises.

  3. Check the result

    35 35 40 45 50

    35 35 40 45 50
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int marks[] = {40, 35, 45, 35, 50};
  int count = (int)(sizeof marks / sizeof marks[0]);
  for (int end = count - 1; end > 0; end--) {
    int changed = 0;
    for (int i = 0; i < end; i++) {
      if (marks[i] > marks[i + 1]) {
        int saved = marks[i]; marks[i] = marks[i + 1]; marks[i + 1] = saved; changed = 1;
      }
    }
    if (!changed) break;
  }
  for (int i = 0; i < count; i++) printf("%d%s", marks[i], i == count - 1 ? "\n" : " ");
  return 0;
}

Order the same marks in the opposite direction using the worked insertion approach.

Worked answer

The worked answer changes the comparison direction while keeping the valid indexes and element count. Equal marks remain present; sorting is not duplicate removal.

C17 · complete program

#include <stdio.h>
int main(void) {
  int marks[] = {40, 35, 45, 35, 50};
  int count = (int)(sizeof marks / sizeof marks[0]);
  for (int i = 1; i < count; i++) {
    int value = marks[i], j = i;
    while (j > 0 && marks[j - 1] < value) { marks[j] = marks[j - 1]; j--; }
    marks[j] = value;
  }
  for (int i = 0; i < count; i++) printf("%d%s", marks[i], i == count - 1 ? "\n" : " ");
  return 0;
}

Expected output

50 45 40 35 35
Run in C compiler →

Recognize the important boundaries

Worst-case comparisons grow quadratically. The changed flag can stop an already ordered pass; using a strict > comparison preserves equal-value order.

The sample marks and their sums fit int. General input requires both valid array bounds and an arithmetic-range contract.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Does sorting remove the repeated mark 35?
Choose one answer. You can try again.

Answer and explanation

No; ordering retains all five elements.

Correct. Duplicate removal is a separate operation with a different output count.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Why name whether an implementation is stable?
Choose one answer. You can try again.

Answer and explanation

It describes whether equal-key records keep their earlier relative order.

Correct. This matters when sorting student records rather than only indistinguishable integer values.

Use the idea independently

Bubble Sort orders a sequence through a defined comparison and preserves its elements, including duplicates.

Continue with array indexes and bounds.

Continue with pointer and count contracts.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program to Sort an Array Using Selection Sort →