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

void main()
	int A[20], N, Temp, i, j;
	printf(“\n\n\t ENTER THE NUMBER OF TERMS…:);
	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++)
				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++)

Download exe and source code here.

Author Name :

Total : 63 Comments

63 Responses to “Bubble sort program in C”

  1. bhawini sahu says:

    yes it was very taf means c langueg…………..

  2. harikine says:

    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.

  3. Atul Kumar Mishra says:

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

    thank you for your best companion

  4. karthik says:

    The second for loop should be N-1-i like below
    for(j=0; j<(N-1)-i;j++)

  5. Naseem Ahmad Saifi says:

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

  6. wanglee says:

    Please can you help me write this in BASIC programing language? And also give the algorithm

  7. Rob G says:

    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. ritu says:

    why u r using the temp???

  9. ritu says:

    int pass,i;
    float A[],n;

  10. CHETNA says:

    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.

  11. rahul says:

    thanks for uploading program….it helps me in the exam….just copy paste dude….

  12. sravani.bathini says:

    thank u very much.

  13. sravani.bathini says:

    y u used 25,11=I

Leave a Reply

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

Free email signup