codescracker


c

C Program Bubble Sort



« Previous Program Next Program »


Bubble Sort in C

To perform bubble sort in C programming, you have to ask to the user to enter the array size then ask to enter array elements, now start sorting the array elements using bubble sort technique.

C Programming Code for Bubble Sort

Following C program sort the array using bubble sort technique :

/* C Program - Bubble Sort */
		
#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	int n, i, arr[50], j, temp;
	printf("Enter total number of elements :");
	scanf("%d",&n);
	printf("Enter %d numbers :",n);
	for(i=0; i<n; i++)
	{
		scanf("%d",&arr[i]);
	}
	printf("Sorting array using bubble sort technique...\n");
	for(i=0; i<(n-1); i++)
	{
		for(j=0; j<(n-i-1); j++)
		{
			if(arr[j]>arr[j+1])
			{
				temp=arr[j];
				arr[j]=arr[j+1];
				arr[j+1]=temp;
			}
		}
	}
	printf("Elements sorted successfully..!!\n");
	printf("Sorted list in ascending order :\n");
	for(i=0; i<n; i++)
	{
		printf("%d  ",arr[i]);
	}
	getch();
}

When the above c program is compile and executed, it will produce the following result:

c program bubble sort

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test