C program of Bubble Sort Algorithm | C Code Champs

C program of Bubble Sort Algorithm | C Code Champs

312
5
SHARE

Bubble sorting is a simple sorting technique in sorting algorithm. In bubble sorting algorithm, we arrange the elements of the list by forming pairs of adjacent elements. It means we repeatedly step through the list which we want to sort, compare two items at a time and swap them if they are not in the right order. Another way to visualize the bubble sort algorithm is as its name, the smaller element bubble to the top. Here is the source code implements bubble sorting algorithm in C which sorts an unordered list of integer. An implementation of bubble sort algorithm in C.

void BubbleSort(int a[], int array_size)
{
     int i, j, temp;
     for (i = 0; i < (array_size - 1); ++i)
     {
          for (j = 0; j < array_size - 1 - i; ++j )
          {
               if (a[j] > a[j+1])
               {
                    temp = a[j+1];
                    a[j+1] = a[j];
                    a[j] = temp;
               }
          }
     }
}

A single, complete “bubble step” is the step in which a maximum element is bubbled to its correct position. This is handled by the inner for loop.

for (j = 0; j < array_size - 1 - i; ++j )
{
     if (a[j] > a[j+1])
     {
          temp = a[j+1];
          a[j+1] = a[j];
          a[j] = temp;
     }
}

 

Examine the following table. (Note that each pass represents the status of the array after the completion of the inner for loop, except for pass 0, which represents the array as it was passed to the function for sorting).

8 6 10 3 1 2 5 4 } pass 0
6 8 3 1 2 5 4 10 } pass 1
6 3 1 2 5 4 8 10 } pass 2
3 1 2 5 4 6 8 10 } pass 3
1 2 3 4 5 6 8 10 } pass 4
1 2 3 4 5 6 8 10 } pass 5
1 2 3 4 5 6 8 10 } pass 6
1 2 3 4 5 6 8 10 } pass 7

The above tabulated clearly depicts how each bubble sort works. Note that each pass results in one number being bubbled to the end of the list.

C program of Bubble Sort Algorithm by C Code Champs:

#include<stdio.h>
#include<conio.h>
int arr[20];
int n;
void get();       // This is
void Bubble();    // the prototype for the functions
void Show();

int main(){
printf("------------------------------------------------------------------\n");
printf("---------------------- Made by C code champ ----------------------\n");
printf("------------------------------------------------------------------\n\n");
printf("- This Program Explains Sorting Using Bubble Sort Algorithm -\n\n");
get();
Bubble();
Show();
getch();

}

void get(){
          int i;
          while(1){
                   printf("Enter the size of the elements : ");
                   scanf("%d",&n);
                   if(n<=20)
                             break;
                   else
                             printf("\nSorry the maximum no of elements is 20\n\n");
                   }
          printf("\n");
          printf("----------------------\n");
          printf("Enter the values \n");
          printf("----------------------\n\n");
          for(i=0;i<n;i++)
                   scanf("%d",&arr[i]);
}

void Bubble(){
          int i,j;
          for(i=1;i<n;i++){

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

                             if(arr[j]>arr[j+1]){      //for descending use "<"

                                      int t;
                                      t=arr[j];
                                      arr[j]=arr[j+1];
                                       arr[j+1]=t;

                                      }

                             }

                   }

          }

void Show(){
          int i;
          printf("\n");
          printf("-----------------------\n");
          printf("Sorted Array Elements\n");
          printf("-----------------------\n");
          for(i=0;i<n;i++){
                   printf("%d\n",arr[i]);

          }

}

 

Let us know in case of any queries or doubts..

5 COMMENTS

LEAVE A REPLY