Bubble sort program in C

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
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++)
	{
		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.

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

76 thoughts on “Bubble sort program in C

  1. 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 😉

  2. 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 …..

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

  4. 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 :D.

  5. this is a good program within a small amount of code………………

    thank you for your best companion

  6. 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++)

  7. 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”

  8. 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.

  9. Gold is one of the traditional precious metals used for wedding jewelry.

    So how can the happy couple reduce the unrivalled stress levels that come with such an predominant event in their lives.
    As a wedding is a one-time event, the photographer must be prepared for the unexpected.

  10. The equipment itself is not really particularly expensive these times,
    so leasing can cost considerably more in the medium-to-long term.

    You can know all the ins and outs of business and can run a restaurant without fail; but would you know the first thing about installing the kitchen grills.
    Two: Increase in income – When you are able to increase your
    traffic to the business, you will easily be able to also increase
    your income.

  11. Starting a small business with no money may sound impossible but there are ways to kick off a very profitable
    business without ever spending a single dime of your own money.
    You can know all the ins and outs of business and can run a restaurant without fail;
    but would you know the first thing about installing the
    kitchen grills. Clients do not look no matter whether you’ve a small or big budgeted communication service.

  12. The equipment itself is not really particularly expensive these
    times, so leasing can cost considerably more in the medium-to-long term.
    When job offers do come and I’ll admit, some companies may fear hiring you because they
    fear a former owner being an employee, negotiate the best salary you can along with the best benefits you can for
    you and your family. What you give away doesn’t have to be costly, but it must be valuable.

  13. The equipment itself is not really particularly expensive these times,
    so leasing can cost considerably more in the medium-to-long term.
    Without enough traffic, you will never have enough
    customers and this means you won’t be making enough money.
    For clients, you can target neighborhoods, communities
    or even companies.

  14. The following are the benefits that you have to be aware of
    the most. You can know all the ins and outs of business and can run a
    restaurant without fail; but would you know the first thing about installing the kitchen grills.
    Two: Increase in income – When you are able to increase your traffic to the business, you will easily be able to also
    increase your income.

  15. If you’re educated (or seeking an education) you will probably find a ton of opportunity in a
    small town. As with everything else, it is only the mindset of the entrepreneur that dictates the success
    and failure of a company. For clients, you can target neighborhoods,
    communities or even companies.

  16. Or, perhaps you’re one of those students who desires a challenge, or even a
    good addition to your résumé for Grad school.
    You can know all the ins and outs of business and can run a restaurant without fail; but
    would you know the first thing about installing the kitchen grills.
    Typically, this works well if we are involved in a solid networking and we reciprocate, by referring clients to those
    other businesses.

  17. Yet, there are grants that are made for equipment and
    training. You can know all the ins and outs of business and
    can run a restaurant without fail; but would you know the first thing about installing the
    kitchen grills. Clients do not look no matter whether you’ve a small or big
    budgeted communication service.

  18. Then send the postcard to all of the addresses in your area, and try to draw customers in.

    Platforms: Android, Blackberry, i – Phone, i
    – Pad, Windows Phone. For clients, you can target neighborhoods, communities or even companies.

  19. Yet, there are grants that are made for equipment and training.
    A lot of people have small business ideas that they often fail to put into
    practice either because they lack the money to make an investment or because the fear to
    lose everything. When travelling, for instance, it would be
    possible to login to your accounting database and enter transactions on the road.

  20. Or, perhaps you’re one of those students who desires a challenge, or even a good
    addition to your résumé for Grad school. Further, amount of
    loan must be need-based, subject to ceiling of Rs 25,000 per borrower for purchase of machinery or equipment etc,
    and meeting working capital requirement of one operating cycle.
    Two: Increase in income – When you are able to increase your traffic
    to the business, you will easily be able to also increase your income.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in