C Programming code for Bubble Sort of 1D Array – KLE Tech

Write a Modular C Programming Code for Bubble Sort of a 1D Array?

CODE:

#include<stdio.h>
void read(int A[],int n);
void display(int A[],int n);
void bubble(int A[],int n);
main()
{
 int A[30],i,n,temp,j,k;

 printf("Enter the size of array\n");
 scanf("%d",&n);
 printf("Enter elements of the aray:\n");
 read(A,n);
 printf("Elements of the array before sorting:\n");
 display(A,n);
 bubble(A,n);
 printf("\nElements of the array after sorting:\n");
 display(A,n);
}
void read(int A[],int n)
{
 int i;
 for(i=0;i<n;i++)
 scanf("%d",&A[i]);
}

void display(int A[],int n)
{
 int i;
 for(i=0;i<n;i++)
 printf("%d\t",A[i]);
}
void bubble(int A[],int n)
{
 int i,j,temp;

 for(i=0;i<=n-2;i++)
 {
 for(j=0;j<=(n-2-i);j++) { if(A[j]>A[j+1])
 {
 temp=A[j];
 A[j]=A[j+1];
 A[j+1]=temp;
 }
 }
 }
}


OUTPUT

Enter the size of array
5
Enter elements of the aray:
3
6
4
2
8
Elements of the array before sorting:
3 6 4 2 8
Elements of the array after sorting:
2 3 4 6 8
Process returned 0 (0x0) execution time : 6.104 s
Press any key to continue.

 

Please find some more codes of 1D Arrays, 2D Arrays, Pointers on the below page:

Top 100+ C Programming codes – KLE Technological University

 

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!