Write a c program to sort a list of elements using bubble and also implement modified bubble sort
#include<stdio.h>
#include<stdlib.h>
void display(int arr[], int n)
{
int i;
for(i=0;i<n;i++)
{
printf(" %d",arr[i]);
}
}
void bubbleShort(int arr[],int n)
{
int i,j,t;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
printf("\n AFTER PASS %d LIST IS ", i+1);
display(arr,n);
}
}
void modifiedBubbleShort(int arr[],int n)
{
int i,j,t,flag=1;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
flag=0;
t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
if(flag==1)
break;
printf("\n AFTER PASS %d LIST IS ", i+1);
display(arr,n);
}
}
void input(int arr[],int n)
{
int i;
printf("ENTER THE LIST ELEMENT ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
}
int main()
{
int n,*arr,ch;
printf("ENTER THE SIZE OF LIST ");
scanf("%d",&n);
arr=(int*)malloc(n*sizeof(int));
input(arr,n);
printf("YOUR ENTERED ELEMENTS ARE : ");
display(arr,n);
printf("\n 1-> normal bubbleshort \n 2-> for modifiedbubbleShort\n");
scanf("%d",&ch);
if(ch==1)
{
bubbleShort(arr,n);
printf("\nLIST after bubbleShort ");
display(arr,n);
}
else if(ch==2)
{
modifiedBubbleShort(arr,n);
printf("\nLIST AFTER modifiedbubbleShort ");
display(arr,n);
}
else
printf("enter a valid choise");
return 0;
}

Post a Comment
0 Comments