C Program to Sort an Array using Bubble Sort

In this tutorial, we will learn about how to create a program in C that sorts an array in ascending order using bubble sort technique. At last we have also created a function that can be used to sort any given array in ascending order.

Bubble Sort in C

To perform bubble sort in C programming, you have to ask from user to enter the array size and then 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 adjacent element is compared. If first element is greater than the second, then swapping operation will be performed, otherwise continue to check for next. Continue swapping until and unless all the possible elements gets swapped and arranged in a way that the first element is smallest and the 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 method as told above. For example, if we have total of 10 elements in an 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, therefore we have to start from 0 to 9 if total number of elements is 10.

Let's suppose user has supplied 10 as array size and 10 1 9 2 8 3 7 4 6 5 as its elements, then here are step by step swapping using bubble sort technique:

• 1 9 2 8 3 7 4 6 5 10
• 1 2 8 3 7 4 6 5 9 10
• 1 2 3 7 4 6 5 8 9 10
• 1 2 3 4 6 5 7 8 9 10
• 1 2 3 4 5 6 7 8 9 10
• 1 2 3 4 5 6 7 8 9 10
• 1 2 3 4 5 6 7 8 9 10
• 1 2 3 4 5 6 7 8 9 10

C Programming Code for Bubble Sort

Following C program sorts an array in ascending order using bubble sort technique:

// Write a program in C that sorts given array in
// ascending order using Bubble Sort Technique
//-------codescracker.com----------

#include<stdio.h>
#include<conio.h>
int main()
{
int arr, i, j, n, temp;
printf("Enter total number of elements to store: ");
scanf("%d", &n);
printf("Enter %d elements:", n);
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
printf("\nSorting 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("All Array elements sorted successfully!\n");
printf("Array elements in ascending order:\n\n");
for(i=0; i<n; i++)
printf("%d ", arr[i]);
getch();
return 0;
}

As the above program was written under Code::Blocks IDE, therefore after successful build and run, here is the first snapshot of the sample run: Now supply size for the array say 10 and then enter 10 array elements. After providing array size and its element, press ENTER key to sort and print all array elements in Ascending order using bubble sort method. Here is the second snapshot of the sample run: Here are some of the main steps involved in the above program:

• We have asked from user to enter array size
• Then to enter array elements of that size
• Now for sorting, we have used first (outer) for loop from 0 to (n-1) as array indexing starts from 0 not 1.
• And then second (inner) for loop from 0 to (n-i-1). As we have to compare two consecutive element, and if first element is greater than second, then we have to swap.
• For example, if value of i is 0, then value of j will also be 0, and then using if statement, 0th element is compared with (0+1)th or 1st element. If the element present at 0 will be greater than the element present at 1, then the swapping will be performed.
• The loop will run upto the end that is, if total of 10 elements in the array, then at last the 8th element will gets compared with (8+1)th element, that is 9th element which is the last one.
• After running the loop to check, compare and swap each and every consecutive element (or every jth to (j+1)th element), we have sorted the array in ascending order.
• Finally print out the same array that will show all of its element in ascending order

If you want to allow user to enter any 10 array elements and sort it using bubble sort. And also want to display array after each sort performance, then here is the program you may go for:

// Write a program in C to sort an array in ascending order
// using bubble sort and also display array after
// each step of sorting
// -----codescracker.com------

#include<stdio.h>
#include<conio.h>
int main()
{
int arr, i, j, temp;
printf("Enter 10 array elements:");
for(i=0; i<10; i++)
scanf("%d", &arr[i]);
for(i=0; i<(10-1); i++)
{
for(j=0; j<(10-i-1); j++)
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
printf("\n");
printf("Step %d: ", i+1);
for(j=0; j<10; j++)
printf("%d ", arr[j]);
printf("\n");
}
printf("\nSorted Array is:\n");
for(i=0; i<10; i++)
printf("%d ", arr[i]);
getch();
return 0;
}

Here is the final snapshot of 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 that asks to enter array size and array element from user at run-time and then sort that 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:

// Write a program in C to sort an array with the help of bubble sort
// technique using function
// -------codescracker.com--------

#include<stdio.h>
#include<conio.h>
void bubbleSort(int arr[], int n);
int main()
{
int arr, 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;
}
}
}
}

After successful build and run. Here is the first screenshot of the sample run: And here is the second screenshot of the sample run: 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:

• Here we have asked from uesr to enter array size and then all elements of that size one by one using for loop
• We have passed the array and array size to function. Passing array means passing array elements, here array size that comes in square bracket, example, arr is optional.
• After above two steps, we are at function step. In function, we have used same technique as discussed above, that is comparing each consecutive elements from start to end.
• If first is greater than second, then swap, if second is greater than third then swap and so on.
• But here we have initialized i with (no-1). Here no indicates to total number of element. For example, if total number of element is 10, then i is initialized to 8, and in second loop, j is initialized with 0.
• Then at first run, the jth element is compared with (j+1)th element. That is 0th element is compared with (0+1)th element (1st) element. At second run, 1st element is compared with 2nd element and so on. This loop will run total of 8 times to compare 8th element with 9th element which is the last element.
• After successfully doing all the above steps, the function execution will be over and our array gets sorted in ascending order.
• Now we only have to display the array element with the help of loop

Same Program in Other Programming Language

You may also like same program in other programming languages:

Tools
Calculator