Home » Sometech » WAP to sort an array in ascending order using bubble sort.

WAP to sort an array in ascending order using bubble sort.

#include<stdio.h>

#include<conio.h>

#define sf scanf

#define pf printf

#define size 20

void main()

{

 

int n,i,a[size],j,temp;

clrscr();

pf(“Enter the number of elements (<=20) =>”);

sf(“%d”,&n);

 

pf(“\nEnter the elements in random order”);

 

for(i=0;i<n;i++)

{

pf(“\nEnter the element a[%d]=>”,i);

sf(“%d”,&a[i]);

}

 

pf(“\nThe original array elements are:\n”);

 

for(i=0;i<n;i++)

 pf(“%d “,a[i]);

 

for(j=n-1;j>=1;j–)

{

 

 for(i=0;i<=(j-1);i++)

 {

   if(a[i+1]<a[i])

   {

     temp=a[i+1];

     a[i+1]=a[i];

     a[i]=temp;

 

  }

 }

}

pf(“\nThe sorted array elements are:\n”);

for(i=0;i<n;i++)

pf(“%d “,a[i]);

getch();

}

Advertisements

SoME Tech

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

%d bloggers like this: