Design a function for bubble sort in c

26 Sep 2022 Balmiki Mandal 0 C Programming

Step-by-Step: Bubble Sort Function in C

Bubble sort is a simple sorting algorithm that repeatedly steps through the list of elements to be sorted, compares each adjacent pair of elements, and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.

In C, a bubble sort algorithm can be implemented using nested loops. The outer loop iterates through the entire array and the inner loop compares adjacent elements and swaps them if they are not in the correct order. After each iteration of the inner loop, the largest element "bubbles" to the end of the array, so on each subsequent iteration the inner loop can be shortened by one element.

write a c program for  bubble sorting using the function

C programming

#include<stdio.h>
void print_int_array(const int *,int);
void bubble_short(int *,int);
void main()
{
    int a[5]={10,200,3,400,50},ele;
    ele=sizeof(a)/sizeof(a[0]);
    printf("Before sort..\n");
    print_int_array(a,ele);
    bubble_short(a,ele);
    printf("After...\n");
    print_int_array(a,ele);
}
void bubble_short(int *a,int ele)
{
  int i,j,t;
  for(i=0;i<ele-1;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;
    }
  } 
}
void print_int_array(const int *p,int ele)
{
    int i;
    for(i=0;i<ele;i++)
    printf(" %d",*p++);//printf(" %d",p[i]);
    printf("\n");
}

Output:

Before sort..

 10 200 3 400 50

After...

 3 10 50 200 400

Further Reading:

Concatenating Two Strings in C: A Step-by-Step Guide

Design a function to count given character in given string

Design a function to reverse the given string

Design a function to search given character in given string in c

Design a function to reverse the word in given string

Design a function replace the words in reverse order in a given string line

design a function to reverse data in between two location

Design a function to find string length in c

Design a Function to print the string character by character

Design a function to print the elements of a given integers array.

Design a function for bubble sort in c

Design a function to swapping of two number

Design a function to check the given number is Armstrong or not if Armstrong return 1 else 0

 

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.