Login || Register

Login || Register

Bubble Sort in C

www.dheerajpatidar.com

In this post I’m gonna share you one of the Array Sorting Program called “Bubble Sort C program”.

Do you know that is Bubble Sort?. If no, then let’s understand the Bubble Sort Algorithm first.

Contents

What is Bubble Sort?

Bubble Sort is the simplest sorting algorithm that repeatedly swap the adjacent elements if they are in the wrong order.

  • Here we are Swapping adjacent elements by using a temporary variable called temp. 
  • This algorithm is not suitable for large data sets as its time Complexity is  average and worst case complexity are of Ο(n²) where n is the number of loops.
  • Space Complexity of this Algorithm is O(1) because we don’t need extra space.

Lets have look below image to understand in better way:

www.dheerajpatidar.com

Bubble Sort in C

 

 #include <stdio.h>

 int main(){

    int arr[100], r, i, n, temp, swap=0;

 

    printf(“How Many Elements: “);

    scanf(%d,&n);

 

    for(i=0; i<n; i++){

        printf(“Enter Element %d: “,i+1);

        scanf(%d,&arr[i]);

    }

    printf(\n);

 

    for(r=0; r<n1; r++){

        for(i=0; i<n1r; i++){

            if(arr[i]>arr[i+1]){

                temp=arr[i];

                arr[i]=arr[i+1];

                arr[i+1]=temp;

                swap=1;

            }

        }

    }

 

    if(!swap){

        printf(“List is already Sorted!\n);

    }

    else{

        printf(“After Sorting: “);

        for(i=0; i<n; i++){

            printf(%d,arr[i]);

        }

        printf(\n);

    } 

    return 0;

 }

Output:

  How Many Elements: 8

 Enter Element 1: 34

 Enter Element 2: 23

 Enter Element 3: 4

 Enter Element 4: 7

 Enter Element 5: 55

 Enter Element 6: 12

 Enter Element 7: 21

 Enter Element 8: 43

 After Sorting: 4 7 12 21 23 34 43 55

How the above program will work?

1) The above program is about Array shorting technique called Bubble Sort.

2) In the Program first of all I’ve declared Array of size 100. that means the declared array can hold 100 items. We have declare some variables as well like:

  • r for number of rounds, 
  • i for number of iterations in each round, 
  • n for the total number of elements in array, 
  • temp for storing number temporarily for swapping, 
  • swap for feedback about the is done or not. Initial value is 0.

 int arr[100], r, i, n, temp, swap=0;

3) After, the declaration statement, I’ve ask the user that How may Elements he/she want to enter in array, and store the answer in variable n that we have declare earlier.

  printf(“How Many Elements: “);

scanf(%d,&n);

4) After that, we will have to take all the number  on by one using for loop.

 for(i=0; i<n; i++){

       printf(“Enter Element %d: “,i+1);

       scanf(%d,&arr[i]);

 }

 printf(\n);

5) After, taking each of the element and storing them into declared array, now the next steps is sorting the array using bubble sort approach.

In this step we have to use 2 for loop, one inside of another. The logic of for loops will be in the following manner:

  • Outer for loop will be responsible for iteration of number of rounds, until r<n-1. here r refers to the number of round, and n-1 is refers to the total number of elements in the array.
  • Inner for loop will be responsible for the number of iterations in each round. As number rounds will increase, the number of iterations will  decrease.
  • In the If statement of the inner loop, we will swap the values of each pair of number, if arr[i] is greater than the arr[i+1], here arr[i] refers to any particular number, and arr[i+1] refers to the next element of the arr[i].
  • If the condition is satisfied, we will store the First number (arr[i]) into temp variable, and replace it with the Second variable (arr[i+1]). At last we Replace the second number with the value store in temp variable.
  • At last we will assign 1 to swap variable.

 for(r=0; r<n1; r++){

       for(i=0; i<n1r; i++){

            if(arr[i]>arr[i+1]){

                temp=arr[i];

                arr[i]=arr[i+1];

                arr[i+1]=temp;

                swap=1;

            }

       }

 }

6) After writing the main logic of the bubble sort, the next step is printing the sorted array if the sorting have been done. here we will use the following steps:

  • First of all we use the if statement with the condition !swap, which means if swap haven’t been done then print List is already Sorted!, else print array which have been sorted.
  • In the else, statement we have first print the message “After sorting: ” and then using for loop we have print the whole sorted array.

 if(!swap){

    printf(“List is already Sorted!\n);

 }

 else{

    printf(“After Sorting: “);

        for(i=0; i<n; i++){

            printf(%d,arr[i]);

        }

     printf(\n);

 }

Conclusion

In this post we have discussed about the Bubble Sort Program in C in easy to understand way.

In order to clear all the Basics of C Programming, you can visit my C Programming Course for Free.

I hope you have got useful content from here. I’m trying to share my best with you.

For any query about this post, you can comment us.

Thanks for Visit!

Dheeraj Patidar
Dheeraj Patidarhttps://www.dheerajpatidar.com
My Self Dheeraj Patidar, founder of this site, provide the content about Digital Marketing, Programming, Tech News, and more technical aspects. I'm currently persuing B Tech iin Information Technology (IT).
RELATED ARTICLES

Variables in C

Operators in C

LEAVE A REPLY

Please enter your comment!
Please enter your name here