Here is the program to sort the given integer in ascending order using bubble sort method. Please find the pictorial tutor of the bubble sorting.
Logic : The entered integers are stored in the array A. Here, to sort the data in ascending order, any number is compared with the next numbers for orderliness. i.e. first element A[0] is compared with the second element A[1]. If forth is greater than the prior element then swapping them, else no change. Then second element is compared with third element, and procedure is continued. Hence, after the first iteration of the outer for loop, largest element is placed at the end of the array. In the second iteration, the comparisons are made till the last but one position and now second largest element is placed at the last but one position. The procedure is traced till the array length.
If we complement the if condition in this program, it will give out the sorted array in descending order. Sorting can also be done in other methods, like selection sorting and insertion sorting, which follows in the next pages.

Bubble sort Image Demo
Here is the C program to sort the numbers using Bubble sort
#include<stdio.h> void main() { int A[20], N, Temp, i, j; clrscr(); printf(“\n\n\t ENTER THE NUMBER OF TERMS…: “); scanf(“%d”,&N); printf(“\n\t ENTER THE ELEMENTS OF THE ARRAY…:”); for(i=0; i<N; i++) { gotoxy(25, 11+i); scanf(“\n\t\t%d”, &A[i]); } for(i=0; i<N-1; i++) for(j=0; j<N-i;j++) if(A[j]>A[j+1]) { Temp = A[j]; A[j] = A[j+1]; A[j+1] = Temp; } printf(“\n\tTHE ASCENDING ORDER LIST IS…:\n”); for(i=0; i<N; i++) printf(“\n\t\t\t%d”,A[i]); getch(); } |
Description :
This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.
#Home #Sitemap #Resources #Terms of Use
Copyright©2012 electrofriends.com All Rights Reserved
Contact:info@electrofriends.com
yes it was very taf means c langueg…………..
Hey Ranjith,
Thanks for the good work. Bubble sort algorithm should check whether swap occured atleast once if not the array is sorted and the algo should stop
.
this is a good program within a small amount of code………………
thank you for your best companion
The second for loop should be N-1-i like below
for(j=0; j<(N-1)-i;j++)
thanks a lot,i like this.this is good program with good example but
The second for loop should be N-1-i like below
for(j=0; j<(N-1)-i;j++)
Please can you help me write this in BASIC programing language? And also give the algorithm
Bubble sort is really an appalling algorithm and should never, ever, be used in production code.
From Wikipedia:
“However, some researchers such as Owen Astrachan have gone to great lengths to disparage bubble sort and its continued popularity in computer science education, recommending that it no longer even be taught.”
Donald Knuth, in his famous book The Art of Computer Programming, concluded that “the bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems”
why u r using the temp???
int pass,i;
float A[],n;
for(pass=1;pass<n;pass++)
{
for(i=0;iA[i+1])
swap(&A[i],&A[i+1]);
}
why in loop we are taking N-1 and N-i in suome side it is taken N-2.IAM not able to distinguish between in all these thing specially confused between selection sort,insertion sort and bubble sort.when to use N-1 when N-2 ETC.