C Program for Bubble Sort

Program for bubble sort in c; Through this tutorial, we will learn how to write a program for bubble sort using for loop, while loop and function in c.

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. Example: First Pass: ( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1. ( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4.

C Program for Bubble Sort

  • C Program for Bubble Sort using For Loop
  • C Program for Bubble Sort using While Loop
  • C Program for Bubble Sort using Function

C Program for Bubble Sort using For Loop

/* C Program for Bubble Sort */
#include <stdio.h>
int main()
{
    int a[100], number, i, j, temp;

    printf("\n Please Enter the total Number of Elements  :  ");
    scanf("%d", &number);

    printf("\n Please Enter the Array Elements  :  ");
    for(i = 0; i < number; i++)
        scanf("%d", &a[i]);

    for(i = 0; i < number - 1; i++)
    {
        for(j = 0; j < number - i - 1; j++)
        {
            if(a[j] > a[j + 1])
            {
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }
    }
    printf("\n List Sorted in Ascending Order : ");
    for(i = 0; i < number; i++)
    {
        printf(" %d \t", a[i]);
    }
    printf("\n");
    return 0;
}

The output of the above c program; is as follows:

Please Enter the total Number of Elements  :  5
Please Enter the Array Elements  :  9 4 1 8 6
List Sorted in Ascending Order :  1 	 4 	 6 	 8 	 9 

C Program for Bubble Sort using While Loop

#include <stdio.h>
int main()
{
    int a[100], number, i, j, temp;

    printf("\n Please Enter the total Number of Elements  :  ");
    scanf("%d", &number);

    printf("\n Please Enter the Array Elements  :  ");
    for(i = 0; i < number; i++)
        scanf("%d", &a[i]);

    i = 0;
    while(i < number - 1) {
        j = 0;
        while(j < number - i - 1) {
            if(a[j] > a[j + 1]) {
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
            j++;
        }
        i++;
    }
    printf("\n List in Ascending Order : ");
    for(i = 0; i < number; i++)
        printf(" %d \t", a[i]);
    printf("\n");
    return 0;
}

The output of the above c program; is as follows:

Please Enter the total Number of Elements  :  5
Please Enter the Array Elements  :  44 22 1 66 8
List in Ascending Order :  1 	 8 	 22 	 44 	 66 

C Program for Bubble Sort using Function

#include <stdio.h>
void bubFunc(int a[], int number) {
    int i, j, temp;
    for(i = 0; i < number - 1; i++) {
        for(j = 0; j < number - i - 1; j++) {
            if(a[j] > a[j + 1]) {
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }
    }
}

int main()
{
    int arr[100], size, i;

    printf("\n Please Enter the total Elements  :  ");
    scanf("%d", &size);

    printf("\n Please Enter the Array Elements  :  ");
    for(i = 0; i < size; i++)
        scanf("%d", &arr[i]);

    bubFunc(arr, size);
    printf("\n List in Ascending Order : ");
    for(i = 0; i < size; i++)
    {
        printf(" %d \t", arr[i]);
    }
    printf("\n");
    return 0;
}

The output of the above c program; is as follows:

Please Enter the total Elements  :  5
Please Enter the Array Elements  :  5 6 7 1 2
List in Ascending Order :  1 	 2 	 5 	 6 	 7 	

Recommended C Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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