Bubble sort program in C

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

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.

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

63 Responses to “Bubble sort program in C”

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

    Reply

Leave a Reply