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 #Submit #Terms of Use
Copyright©2011 electrofriends.com All Rights Reserved
Contact:info@electrofriends.com | Powered by Dhyeya
November 10th, 2009 at 8:56 am
Program is nice…
but very typical to read….
try to use CODE TAG …
December 5th, 2009 at 11:01 am
it s very nice .you have cleared all my doubts.its to the point yet very helpful.
December 16th, 2009 at 8:59 am
ya…nice 2 understand….very helpful
December 16th, 2009 at 9:08 am
clear….eay to understand
January 21st, 2010 at 6:41 pm
Thank you so much ..for the info really helpful
Btw how can i put the descending order along with the ascending???
output
Ascending order is: Descending order is
1 3
2 2
3 1
i tried duplicating it below before getch and changed < to greater than to i but still shows the same sort in ascending…Pls2x reply
January 21st, 2010 at 7:14 pm
ohh i figured it out myself
thanks anyway
for the initial info
#include
#include
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=1;i<=N;i++)
{
gotoxy(25,5+i);
scanf("\n\t\t%d",&A[i]);
}
for(i=1;i<=N-1;i++)
for(j=1;jA[j+1])
{
Temp = A[j];
A[j] =A[j+1];
A[j+1] = Temp;
}
printf(“\n\t the Ascending order list is:\n”);
for(i=1; i to < */
/*———————————————————-*/
for(i=1;i<=N-1;i++)
for(j=1;j<=N-i;j++)
if (A[j]<A[j+1])
{
Temp = A[j];
A[j] =A[j+1];
A[j+1] = Temp;
}
printf("\n\t the Descending order list is:\n");
for(i=1;i<=N;i++)
printf("\n\t\t\t%d",A[i]);
getch();
}
Starting from the line:D i added the descending sort
if anyone needs it there you have it
Sorry if my English is bad …..
January 21st, 2010 at 7:16 pm
and by the way since its not shown what the includes are cause it may cause some errors
its include
include
Have a nice day evry1:D
January 21st, 2010 at 7:17 pm
ohh my its still not shown
maybe i just type the
insides of the prototype
stdio.h and conio.h………
April 5th, 2010 at 12:24 am
ya it is gud
very helpful fo me to do it easily thankss………:):P
June 24th, 2010 at 3:20 am
This one is also a great work .. thanks
November 23rd, 2010 at 12:48 pm
this information is very helpful for not only students even also teachers. simply its too helpful.
November 23rd, 2010 at 12:56 pm
FOR ME its very helpful.
December 9th, 2010 at 10:42 am
very nice ..
December 9th, 2010 at 3:07 pm
the information is very useful. THANKS FOR THIS
March 29th, 2011 at 1:47 pm
Its good one. But i dono y we r putting j<N-i i dono
plz tell me
April 8th, 2011 at 5:47 pm
very nice u done great job
April 16th, 2011 at 7:27 pm
thanks…lucid way..
April 29th, 2011 at 9:18 pm
VERY EASY TO UNDERSTAND ….THNKS A LOT!!!
June 27th, 2011 at 4:35 pm
nice illusion
July 28th, 2011 at 5:21 pm
according to me,this program is ineffecient.
try to reduce its complexity if array is already sorted or
PARTIALLY SORTED.?????????????
August 30th, 2011 at 4:07 pm
hupp
September 5th, 2011 at 5:10 pm
thank u…..
September 11th, 2011 at 4:33 am
really nice to understand…!
September 13th, 2011 at 8:21 am
cool ans dude…………. thank you so much
September 21st, 2011 at 7:45 am
fuck!!!!
September 26th, 2011 at 8:59 pm
in dev c++ it shows gotoxy undeclaredd
October 12th, 2011 at 11:52 pm
There is an index boundary problem in the code.
for(i=0; i<N-1; i++)
for(j=0; jA[j+1])
When i = 0, in the second for loop, when j = N – 1, the A[j+1] is over boundary.
October 15th, 2011 at 1:54 pm
This tutorial is very nice. Thankyou.
October 18th, 2011 at 3:43 am
U r doing an excellent job…..programs r very clear to understand
October 24th, 2011 at 12:40 am
programs has logical error. It works for MOST set of input but not ALL.
to make it work properly it should be j<n-i-1 in the second loop instead of j<n-i
October 29th, 2011 at 1:51 pm
great dude
November 8th, 2011 at 10:22 am
ur program was quite useful
November 8th, 2011 at 10:23 am
very easy to understand
November 8th, 2011 at 10:26 am
ur program was really very interesting.It gave clearcut idea about bublesort.Now we are very much clear with it.U can send more programs in c it will be very much useful for students.
November 8th, 2011 at 10:28 am
ur program was not that much lengthy so it is easy to understand.
November 8th, 2011 at 10:30 am
ur program was not that much lengthy,it is easy to get the concept behind bubble sort.
November 11th, 2011 at 11:06 pm
Its nt for diploma students
November 28th, 2011 at 2:44 am
very nice…help helpful and thanks
December 11th, 2011 at 1:42 pm
Your blog is pretty cool to me and your subject matter is very relevant. I was browsing around and came across something you might find interesting. I was guilty of 3 of them with my sites. “99% of site managers are guilty of these 5 errors”. http://is.gd/jhKkC5 You will be suprised how fast they are to fix.
December 13th, 2011 at 7:46 pm
what is gotoxy(25,11+i); statement
I did not understand
January 4th, 2012 at 7:14 pm
thank u so much.its really helpful.
January 8th, 2012 at 1:53 pm
nice
January 8th, 2012 at 2:40 pm
thank u
January 18th, 2012 at 10:43 am
thanx its really easy………
January 23rd, 2012 at 11:55 pm
Dude… it’s superb………
easy understanding……. it’s really helps alot to me now….
please post for merge sort also … like this.
Thanks alot……….