Bubble Sort algorithm using C : Explained

Aditya SharmaAditya Sharma
2 min read

Bubble sort array perform sorting using data structure techniques. Application of such array is also called Bubble Sort.

The following is the procedure for sorting a given data element set by Bubble Sort.

  1. In Bubble Sort each element is compare with its adjacent element.

  2. If the first element is larger than the second element changes. Otherwise No change.

  3. In this way the next element is compare with its adjacent element and step 2 is repeated.

  4. This process is called pass and each passing element hold its position.

This way the given element gets sorted.

Ex :- 10, 50, 20, 15, 5

We take an array -

Pass - 1

Compare A1 & A2 and arrange them.

A1<A2

Compare A2 & A3 and arrange them.

A2 < A3

Compare A3 & A4 and arrange them.

A3 < A4

  • Compare A4 & A5 and arrange them.

    A4 < A5

The above process called Pass - 1

Pass - 2

  • Compare A1 & A2 and arrange them.

    A1 < A2

  • Compare A2 & A3 and arrange them.

    A2 < A3

  • Compare A3 & A4 and arrange them.

    A3 < A4

The above process called Pass - 2

Pass - 3

  • Compare A1 & A2 arrange them.

    A1 < A2

  • Compare A2 & A3 arrange them.

    A2 < A3

This above process called pass - 3

Pass - 4

  • Compare A1 & A2 and arrange them.

    A1 < A2

    Thus the element after sorting - 5, 10, 15, 20, 50

Program in C :-

Output :-

5, 10, 15, 20, 50

Time complexity of Bubble Sort :-

  1. Worst case - 0(N^2)

  2. Average case - 0(N^2)

  3. Best case - 0(N)

0
Subscribe to my newsletter

Read articles from Aditya Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aditya Sharma
Aditya Sharma

I am a web developer from Bengaluru, India. My mission is to turn your vision into a compelling digital experience. I'm here to collaborate and bring your ideas to life. Let's build something amazing together!