Bubble shorting in c programming

20 Sep 2022 Balmiki Mandal 0 C Programming

Bubble Sorting in C Programming - Algorithm and Example

Bubble sorting is a simple sorting algorithm that works by repeatedly comparing adjacent elements in a list and swapping them if they are in the wrong order. The algorithm is named after the way that the larger elements "bubble up" to the top of the list.

Bubble sorting is a stable sorting algorithm, which means that it preserves the order of equal elements in the list. It is also an in-place sorting algorithm, which means that it does not require any additional memory.

C program to Bubble shorting 

#include<stdio.h>
void main()
{
    int a[5],i,j,t,ele;
    ele=sizeof(a)/sizeof(a[0]);
    printf("Enter the array elements:");
    for(i=0;i<ele;i++)
    scanf("%d",&a[i]);
    printf("Unshorted Elements:");
    for(i=0;i<ele;i++)
    printf(" %d",a[i]);
    printf("\n");
    for(i=0;i<ele;i++)
    {
        for(j=0;j<ele-1-i;j++)
        {
            if(a[j]>a[j+1])
            {
                t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
            }
        }
    }
    printf("shorted Elements:");
    for(i=0;i<ele;i++)
    printf(" %d",a[i]);
    printf("\n");
}

Output:

Enter the array elements: 5 15 1 25 12
Unsorted Elements:  5 15 1 25 12
shorted Elements:  1 5 12 15 25

Further Reading:

Bubble shorting in c programming

Bubble short in string in c

Write a c program to short 2D array using bubble short

Selection shorting in c Programming

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.