BUBBLE SORT
BUBBLE SORT:-
ALGORITHM:-
STEP 1. REPEAT STEP 2 & 3 FOR K=1 TO N-1
STEP 2. SET PTR:= 1
STEP 3. REPEAT WHILE PTR <= N-K
A) IF DATA [PTR]>DATA[PTR+1] THEN INTERCHANGE DATA[PTR] & DATA[PTR+1]
B) SET PTR:= PTR+1
STEP 4. EXIT
CODE:-
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n,arr[20],temp;
int flag;
clrscr();
printf("Enter the size of array : ");
scanf("%d",&n);
printf("\n");
for(i=0; i<n; i++)
{
printf("enter array element : ");
scanf("%d",&arr[i]);
}
printf("\nUnsorted Array : ");
for(i=0; i<n; i++)
{
printf ("%d ",arr[i]);
}
for(i=0;i<n-1;i++)
{ flag=0;
for(j=0;j<n-1;j++)
{
if(arr[j]>=arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
flag=1;
}
}
if(flag==0)
{
break;
}
}
printf("\n\nHere's the sorted Array : ");
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
printf("\n");
getch();
return 0;
}
Comments
Post a Comment
Kaushikmadhav77@gmail.com