Bubble sort in C

In this article, you’ll learn about the bubble sort algorithm and how to implement it in the C programming language with and without a function in easiest way.

Bubble sort algorithm in C

Bubble sort in C is one of the most simple algorithm. In bubble sort, if the elements are to be arranged in ascending order then the largest element will be pushed at the end of the list and this process will continue till all the elements are placed in the ascending order.

In order to achieve this, every element will be checked with the succeeding element and they are swapped if the element is larger than the next element. One complete traversal of the array/list is known as a pass.

Program: Bubble sort program in C

This is a C program to sort N numbers in ascending order using bubble sort, where N is the number of elements in the array which is entered by the user.

#include<stdio.h>
int main()
{
    int array[10];
    int i, j, num, temp;
    printf("Enter the number of elements: ");
    scanf("%d", &num);
    printf("Enter the elements of the array: ");
    for (i = 0; i < num; i++)
    {
        scanf("%d", &array[i]);
    }
    printf("\nInput array is:\n");
    for (i = 0; i < num; i++)
        printf("%d\t", array[i]);
    
    //bubble sort algorithm starts here
    
    for (i = 0; i < num; i++)
    {
        for (j = 0; j < (num - i - 1); j++)
        {
            if (array[j] > array[j + 1])
            {
                temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;
            }
        }
    }
    printf("\nArray after bubble sort:\n");
    for (i = 0; i < num; i++)
        printf("%d\t", array[i]);
    printf("\n");
    return 0;
}

Output: Sort the numbers in ascending order using bubble sort

Enter the number of elements: 7
Enter the elements of the array: 91 32 45 26 102 53 85

Input array is:
91      32      45      26      102     53      85
Array after bubble sort:
26      32      45      53      85      91      102

We can separate the bubble sort algorithm to understand it properly using function. Below is a bubble sort example to implement the same.

Program: Bubble sort in C using function

This C program uses bubble sort algorithm inside a function to sort the numbers in descending order. The array elements(numbers) are entered by the user.

 /* Bubble sort program in C using function*/

#include<stdio.h>
//function prototype
int bubbleSort(int[], int);
int main()
{
    int array[10], num;
    printf("Enter the size of the array: ");
    scanf("%d", &num);
    printf("Enter the elements of the array: ");
    for (int i = 0; i < num; i++)
    {
        scanf("%d", &array[i]);
    }
    printf("\nInput array is:\n");
    for (int i = 0; i < num; i++)
        printf("%d\t", array[i]);
    
    printf("\n\nArray in decreasing order after bubble sort:\n");
    bubbleSort(array, num);   //function calling
    printf("\n");
    return 0;
}

//Function definition is always almost similar to the core algorithm
int bubbleSort(int arr[], int size)
{
    int temp, i, j;
    for (i = 0; i < size; i++)
    {
        for (j = 0; j < (size - i - 1); j++)
        {
            if (arr[j] < arr[j + 1])
            {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
    for (i = 0; i < size; i++)
        printf("%d\t", arr[i]);
}

Output of the above bubble sort example is:

Enter the size of the array: 6    
Enter the elements of the array: 312 42 9128 6 73 4801       

Input array is:
312     42      9128    6       73      4801

Array in decreasing order after bubble sort:
9128    4801    312     73      42      6

Challenge: Write C program to sort numbers in descending order using bubble sort in the comments.

Also read:

In this post, we learned about bubble sort algorithm in C and its implementation program with and without function.

For more programming articles and code snippets, visit programming articles.

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at programming-articles.com, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →

Leave a Reply

Your email address will not be published. Required fields are marked *