Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

C program for bubble sort

C program for bubble sort

#include <stdio.h>
#include <stdlib.h>
int bubble_sort(int,int[]);    // function prototype
int main()
{ 
  int n;
  int arr[100];
  printf("\n This is a simple program for bubble sort\n");

  printf("\n Enter the size of array you want\n");
  scanf("%d",&n);     // taking input as length of array
  
  printf("\n Now Enter the elements of array\n");
  for(int i=0;i<n;i++)
  scanf("%d",&arr[i]);   // taking input as elements of array

  printf("Array elements entered by you is:\n ");
  for(int i=0;i<n;i++)
  printf("%d\t",arr[i]);  // printing entered array

  printf("\n\n Array elements after sorting :\n");
  bubble_sort(n,arr);  // function call
  printf("\n Sorted array elements after bubble sort");
  return 0;
}

int bubble_sort(int n,int arr[])   // function definition
{
  int temp;
  for (int j=0;j<n-1;j++)         // outer loop
    {
      for (int i=0;i<n-j-1;i++)   // inner loop
         {
           if(arr[i]>arr[i+1])
              {
               temp=arr[i+1];
               arr[i+1]=arr[i];
               arr[i]=temp;
              }
         }
    }

  for (int j=0;j<n;j++)   // displaying final array
  printf("%d \t",arr[j]);
  return 0;
}

Output:

C program for bubble sort
Output:1
C program for bubble sort
Output :2
C program for bubble sort
Output:3

The above code can’t be directly copy-pasted. You must have to type with your own hand. This is made for your better future. 

Are you made for copy paste?

Absolutely “NO”

Then, try hard to understand this code and type with your own.

Leave a Comment

error: