Bubble sort program in C

Thursday, December 4th, 2008

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

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();
}

Download exe and source code here.

Avatar Image

Author Name :
Ranjith

Total : 50 Comments


50 Responses to “Bubble sort program in C”

  1. Nirav dangi says:

    Program is nice…
    but very typical to read….
    try to use CODE TAG …

  2. himanshi says:

    it s very nice .you have cleared all my doubts.its to the point yet very helpful.

  3. srikanth says:

    ya…nice 2 understand….very helpful

  4. srikanth says:

    clear….eay to understand

  5. Neill Van says:

    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 ;)

  6. Neill Van says:

    ohh i figured it out myself :D thanks anyway :D

    for the initial info :D

    #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 :D if anyone needs it there you have it :D

    Sorry if my English is bad …..

  7. Neill Van says:

    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

  8. Neill Van says:

    ohh my its still not shown

    maybe i just type the
    insides of the prototype

    stdio.h and conio.h………

  9. swati says:

    ya it is gud
    very helpful fo me to do it easily thankss………:):P :D

  10. Nadeem says:

    This one is also a great work .. thanks

  11. SHRIDEVI S says:

    this information is very helpful for not only students even also teachers. simply its too helpful.

  12. SHRIDEVI NASHIMATH says:

    FOR ME its very helpful.

  13. Eswar says:

    very nice ..

  14. yogesh dangi says:

    the information is very useful. THANKS FOR THIS

  15. Muthukumar says:

    Its good one. But i dono y we r putting j<N-i i dono
    plz tell me

  16. ramesh says:

    very nice u done great job

  17. anurag says:

    thanks…lucid way..

  18. HARPREET says:

    VERY EASY TO UNDERSTAND ….THNKS A LOT!!!

  19. Upendra says:

    nice illusion

  20. johny says:

    according to me,this program is ineffecient.
    try to reduce its complexity if array is already sorted or
    PARTIALLY SORTED.?????????????
    :(

  21. annonymous says:

    hupp

  22. Soumya says:

    thank u…..

  23. sumit says:

    really nice to understand…!

  24. vissu says:

    cool ans dude…………. thank you so much

  25. jan says:

    fuck!!!!

  26. saranya says:

    in dev c++ it shows gotoxy undeclaredd
    :( :(

  27. cooper says:

    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.

  28. Mukesh Rajput says:

    This tutorial is very nice. Thankyou.

  29. shreeman says:

    U r doing an excellent job…..programs r very clear to understand

  30. Shady says:

    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

  31. pavan says:

    great dude :)

  32. rose says:

    ur program was quite useful

  33. lotus says:

    very easy to understand

  34. aamina firza says:

    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.

  35. nahida says:

    ur program was not that much lengthy so it is easy to understand.

  36. nahida says:

    ur program was not that much lengthy,it is easy to get the concept behind bubble sort.

  37. Basava says:

    Its nt for diploma students

  38. ammu says:

    very nice…help helpful and thanks

  39. top mistakes says:

    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.

  40. karunakar says:

    what is gotoxy(25,11+i); statement
    I did not understand

  41. sayali. says:

    thank u so much.its really helpful.

  42. aruna says:

    nice

  43. aruna says:

    thank u

  44. mohini says:

    thanx its really easy………

  45. snrao says:

    Dude… it’s superb………
    easy understanding……. it’s really helps alot to me now….
    please post for merge sort also … like this.

    Thanks alot……….

  46. Sujit Kumar says:

    It is compiling with 26 Errors & 5 Warnings…….
    Plz. modify it…….

  47. myra says:

    i didn’t understand the the line for:

    “for(i=0; i<N-1; i++)
    for(j=0; j<N-i;j++)"

    why it had a N-1?
    how it works?
    thanks a lot!

  48. myra says:

    someone will help me??

  49. bhaskar says:

    it runs!! thanxx dude

  50. Muslim says:

    bubble sorting program not execute

    #include
    #incluse
    #include

    void main()
    {
    clrscr();
    int k,i,n=10,TEMP=0,PTR=0;
    int DATA[10]={26,24,3,17,25,24,13,60,47,1};
    for (k=1;k<n;k++)
    {
    int PTR=0;
    while (PTRDATA[PTR+1])
    {
    TEMP=DATA[PTR];
    DATA[PTR+1]=TEMP;
    }
    DATA[PTR]=DATA[PTR+1];
    PTR++;
    }
    }
    for( i=0;i<10;i++)
    {
    printf("%d\n ",DATA[i]);

    }
    getch();

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup