A Algorithm and Flowchat and Program for Bubble sort
ALGORITHM
Step 1: Start
Step 2: [input] read n numbers
Step 3: [for loop statement]
Step 4: for(i=1;i<n;i++) [loop for passing number]
Step 5: for(j=0;j=n-i;j++) [loop for comparison]
Step 6: [using if condition]
if(a[j]>a[j+1])
Step 7: [body of if condition]
{
Step 8: temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
[end of if condition]
[end of step 5 j loop]
[end of step 4 i loop]
Step 9: print the sorted numbers
Step 10: Stop [end of the algorithm]
FLOWCHAT
PROGRAM
#include<stdio.h>
void main()
{
int a[100];
int n,i,j,temp;
printf("\n enter size of elements \n");
scanf("%d",&n);
for(i=0;;i<n;i++)
{
scanf("%d",&a[i];
}
for(i=1;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n list of numbers in sorted order \n");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
getch();
clrscr();
}
Step 1: Start
Step 2: [input] read n numbers
Step 3: [for loop statement]
Step 4: for(i=1;i<n;i++) [loop for passing number]
Step 5: for(j=0;j=n-i;j++) [loop for comparison]
Step 6: [using if condition]
if(a[j]>a[j+1])
Step 7: [body of if condition]
{
Step 8: temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
[end of if condition]
[end of step 5 j loop]
[end of step 4 i loop]
Step 9: print the sorted numbers
Step 10: Stop [end of the algorithm]
FLOWCHAT
PROGRAM
#include<stdio.h>
void main()
{
int a[100];
int n,i,j,temp;
printf("\n enter size of elements \n");
scanf("%d",&n);
for(i=0;;i<n;i++)
{
scanf("%d",&a[i];
}
for(i=1;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n list of numbers in sorted order \n");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
getch();
clrscr();
}
Comments
Post a Comment