Professional Documents
Culture Documents
2) PROGRAM TO SORT THE ARRAY ELEMENTS BY BUBBLE SORT OF ARRAY WITH FIXED
LENGTH
#include<stdio.h>
#include<conio.h>
static int p=0,c=0,in=0;
void main()
{
int arr[5],i,j;
clrscr();
3) PROGRAM TO SORT THE ARRAY ELEMENT BY BUBBLE SORT FOR VARIABLE LENGTH
ARRAY
#include<stdio.h>
#include<conio.h>
static int p=0,c=0,in=0;
void main()
{
int *ptr,n,i,j;
clrscr();
printf("\n\n\n Enter the size of an array to be sorted:");
scanf("%d",&n);
ptr=new int[n];
printf("\n\n Array of required size is created");
printf("\n\n ================================================");
printf("\n\n Enter the elements of an array:");
for(i=0;i<n;i++)
{
printf("\n ");
scanf("%d",&ptr[i]);
}
for(i=(n-1);i>0;i--)
{
p++;
for(j=0;j<i;j++)
{
c++;
if(ptr[j]>ptr[j+1])
{
int temp;
temp=ptr[j+1];
ptr[j+1]=ptr[j];
ptr[j]=temp;
in++;
}
}
}
printf("\n\n ================================================");
printf("\n\n Sorted elements of an array:");
for(i=0;i<n;i++)
{
printf("\n %d",ptr[i]);
}
printf("\n\n\n
printf("\n\n\n BUBBLE SORT INFORMATION ");
printf("\n\n Total number of passes : %d",p);
printf("\n\n Total number of comparisons : %d",c);
printf("\n\n Total number of interchanges : %d",in);
getch();
}