Thursday, July 17, 2008

/* Write a C program to sort N numbers in ascending order *
* using Bubble sort and print both the given and the sorted *
* array with suitable headings */

#include stdio.h
#include conio.h
#define MAXSIZE 10

void main()
{
int array[MAXSIZE];
int i, j, N, temp;

clrscr();

printf("Enter the value of N\n");
scanf("%d",&N);

printf("Enter the elements one by one\n");
for(i=0; i< j="0;"> array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
printf("Sorted array is...\n");
for(i=0; i

Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be sorted, comparing two items at a time 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.


The logic contains inportant concepts to note. Lets see at the nested for loop where all the execution is done. The outer for(i Based) keeps track of the number of times the loop is looked after arranging successive elements. From the above, by the algorithm of Bubble sort, the program has go through the loop N number of times to sort N numbers. This is not an advisible algorithm for large data set as the program exexution time becomes extremly high.


The inner For (j based) keeps track of the element that is swapped now. It keeps increasing untill the Limit of N-i-1 is reached. The inside of this For loop is a simple swap program. So our necessary job is done


2 comments:

Anonymous said...

well,this a better method to make the student understand the subject more clearly..THANKS...I also need on java basically on packages and examples on graphics using C or C++

sri said...

this site is really useful.thanks a lot for helping us out