codescracker
c

C Program for Bubble Sort



« Previous Program Next Program »

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.

But before going through the program, if you are not aware of how bubble sorts works, then refer to the step by step working of Bubble Sort. Now let's move on and implement it in C program.

Bubble Sort in C

Now let's implement bubble sort in a C program as given below. I'll explain each and every line of code later on. The question is, write a program in C that sorts given array in ascending order using Bubble Sort technique. The answer to this question is:

#include<stdio.h>
#include<conio.h>
int main()
{
    int arr[50], 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:

c program bubble sort

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:

bubble sort in c

Program Explained

Display Array after Each Sort

If you want to display the array after each sort, then here is the program you may go for:

#include<stdio.h>
#include<conio.h>
int main()
{
    int arr[10], 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 this program:

c sort array using bubble sort

Bubble Sort Program in C using Function

Here is another program that does the same job using a user-defined function bubbleSort():

#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;
            }
        }
    }
}

After successful build and run. Here is the first screenshot of the sample run:

bubble sort using function c

And here is the second screenshot of the sample run:

bubble sort c program using function

Program Explained

All the steps are same as discussed in very first program (of this article) 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 Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker