Introduction
Sorting is arranging the given list in ascending/descending order, with respect to a given key field. This list can be number of records, with each record having many fields.
Types of sorting
- Exchange/Bubble Sort
- Selection Sort
- Insertion Sort
- Quick/Partition Exchange Sort
- Merge/2-Way Merge Sort
Exchange/Bubble Sort : It uses simple algorithm. It sors by comparing each pair of adjusent items and swaping them in the order .This will be repeated until no swaps are needed. The algorithm got its name from the way smaller elements "bubble" to the top of the list.
It is not that much efficient, when a list is having more than a few elements. Among simple sorting algorithms, algorithms like insertion sort are usually considered as more efficient.
Bubble sort is little slower compared to other sorting techniques but it is easy because it deals with only two elements at a time.
An example to bubble sort is given below.
#include <stdio.h> void main() { int a[10]={34,2,56,78,34,5,76,10,47,29}; //initializing array of elements int n=10; int i,j,temp; for(i=0;i<10;i++) //for loop to select each element in the array { for(j=0;j<n-i;j++) //for loop to swap elements after comparing { if(a[j]>a[j+1]) { temp=a[j]; //swaping a[j]=a[j+1]; a[j+1]=temp; } } } for(i=0;i<n;i++) //for loop to print sorted elements { printf("%d \n",a[i]); } }
Comments Post a Comment
sanjeev sharma 7/13/2012 (IST) / Reply
enquary
aratrik 8/25/2012 (IST) / Reply
thanks a lot
Aabi 9/27/2012 (IST) / Reply
thank you for help in c language....
nagesh hosale 10/29/2012 (IST) / Reply
thanks for cleraring my doubts
dddddd 12/21/2012 (IST) / Reply
Exactly what i needed. I'll try it. I hope it will work for a beginner like me. Thanks.
birendra 4/16/2013 (IST) / Reply
why it is not running in my "c-free5" application ???
dutta 4/17/2013 (IST) / Reply
Pls explain me bubble sort with an example not by programming language.