codescracker


c

C Program to Sort an Array using 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.

Logic Used in Bubble Sort

The logic used in bubble sort is that each consecutive element is compared. If first element is greater than the second, then swapped will be performed otherwise not. Continue swapping until and unless all the possible elements are swapped and arranged in that way, that the first element is smallest and last element is largest in that list.

Note - Number of pass = total number of element in the array.

Therefore, we have to run the total number or amount of swapping function as told in the above note or line. For example, if we have total of 10 elements in the array, then we have to perform swapping 10 times as shown in the program given here. And as we all knows that indexing in array starts from 0, then we have to start from 0 to 9 if total number of elements is 10.

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

Main Steps involved in above Program

Here are some of the main steps involved in the above program:

Now let's learn how to make the same program using function in C.

Bubble Sort using Function in C

The question is, write a program in C to ask array size and array element at run-time and then sort the array using bubble sort technique with the help of function. Finally, display the sorted array in ascending order.

Here is the program that implements bubble sort to sort an array in ascending order using function in C:

// Sorting an array in ascending order using bubble 
// sort method or technique in C

#include<stdio.h>
#include<conio.h>
void bubbleSort(int arr[], int n);
int main()
{
	int arr[100], nos, i;
	printf("How many element that has to be sorted ? ");
	scanf("%d", &nos);
	printf("\n");
	for(i=0; i<nos; i++)
	{
		printf("Enter integer value for element no.%d: ",i+1);
		scanf("%d",&arr[i]);
	}
	bubbleSort(arr, nos);
	printf("\n\nFinally sorted array is:\n");
	for(i=0; i<nos; i++)
	{
		printf("%d\n",arr[i]);
	}
	getch();
	return 0;
}
void bubbleSort(int arr[], int no)
{
	int i, j, temp;
	for(i=no-2; i>=0; i--)
	{
		for(j=0; j<=i; j++)
		{
			if(arr[j]>arr[j+1])
			{
				temp = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = temp;
			}
		}
	}
}

As the above program is written under CodeBlocks IDE, therefore after successfully build and run. Here is the first screenshot of first sample run:

bubble sort using function c

And here is the second screenshot of first sample run:

bubble sort c program using function

Main Steps involved in above Program

All the steps are same as discussed in above one except function. As here we have used function to implement bubble sort. Here are some of the main steps that tells how to use function in this case:

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