Tuesday, February 21, 2012

1. BUBBLE SORT PROGRAM IN C

Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, it is not efficient for sorting large lists.


Example

Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort algorithm. In each step, elements written in bold are being compared. No of passes=4.
First Pass:
5 1 4 2 8 )  ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps them.
( 1 5 4 2 8 )  ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 )  ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 )  ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
Second Pass:
1 4 2 5 8 )  ( 1 4 2 5 8 )
( 1 4 2 5 8 )  ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 )  ( 1 2 4 5 8 )
( 1 2 4 5 8 )  ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
1 2 4 5 8 )  ( 1 2 4 5 8 )
( 1 2 4 5 8 )  ( 1 2 4 5 8 )
( 1 2 4 5 8 )  ( 1 2 4 5 8 )
( 1 2 4 5 8 )  ( 1 2 4 5 8 )
Finally,the 4th pass takes place similarly and after that the array is sorted, and the algorithm can terminate.


ALGORITHM

procedure bubbleSort( A : list of sortable items )
  repeat
    swapped = false
    for i = 1 to length(A) - 1 inclusive do:
      if A[i-1] > A[i] then
        swap( A[i-1], A[i] )
        swapped = true
      end if
    end for
  until not swapped
end procedure

ANIMATION:




Sample Output of Code:


CODE
/*cod3-4holic.blogspot.com - Bubble Sort Program */
#include<stdio.h>
#include<conio.h>
int arr[20];
int n;
void get();       // This is
void Bubble();    // the prototype for the functions
void Show();


void main(){
clrscr();
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 :\n");
                   scanf("%d",&n);
                   if(n<=20)
                             break;
                   else
                             printf("Sorry 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]);
          }
}

DOWNLOAD CODE