Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

Bubble Sort in C Programming

August 25, 2013 Posted by: GPA Views: 950 0 comments

Problem

Write Bubble Sorting program using C.

Solution

Bubble sort is also known as exchange sort. In bubble sort algorithm array is traversed from 0 to the length-1 index of the array and compared one element to the next element and swap values in between if the next element is less than the previous element. In other words, bubble sorting algorithm compare two values and put the largest value at largest index. The algorithm follow the same steps repeatedly until the values of array is sorted.

Bubble sort algorithm: 

Example we have an array unsorted A[0],A[1],A[2]................ A[n-1] and A[n] as input. Then the following steps are used for bubble sort algorithm:
1.Compare A[0] and A[1] .
 2.If A[0]>A[1] then swap A[0] and A[1].
3.Take next A[1] and A[2].
4.Comapre these values.
5.If A[1]>A[2] then swap A[1] and A[2]
................................................................
at last compare A[n-1] and A[n]. If A[n-1]>A[n] then swap A[n-1] and A[n]. As we see the highest value is reached at n-th position. At next iteration leave n-th value. Then apply the same steps repeatedly on A[0],A[1],A[2]................ A[n-1] elements repeatedly until the values of array is sorted. 

Say we have the following array:

A[10] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; 

In this example you will see step by step how this algorithm swap all value in array until the values of array is sorted. 

Example:

   

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
   int A[10] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
   int i,j,k;//counter
   printf( "Array before Bubble sorting:\n" );
   for ( i = 0; i < 10; i++ ) {
      
printf( "%d ",A[i]);
   }
   int temp;
   int counterforswap=1;//counter for swap
   int size=10;//array size
   for (i= 1; i<size; i++ ) {
        //loop to control number of comparisons per pass
      for (j = 0; j < size-1; j++ ) {
       // compare adjacent elements and swap them if first element is greater than second element
           if (A[j] > A[j + 1] ) {
           temp = A[j];//swap elements for this use any variable temp
           A[j] = A[j + 1];
           [j + 1] = temp;
           printf("\n\nElements after swapping #%d:\n",counterforswap);
           for (k = 0; k < size; k++ ) {
              printf("%d ",A[k]); 
          }
           counterforswap++;
       }
      }
   }
   //show result after sorting
   printf("\n\nArray after Bubble sorting:\n" );
   for ( i = 0; i < 10; i++ ) {
   printf( "%d ",A[i]);
   }
   printf("\n");
   system("PAUSE");
   return 0;
}

Output

Bubble Sort in C example

Download this example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe