There is only some code of various data structure procedures. Try to
understand, please don't copy-paste code, It will harm for only you not
me. I try to add comment in code so that you can easily catch the
procedures.
Bubble Sort:
#include<stdio.h>
int main()
{
int num[7],i,j;
int guess;
for(j=0;j<7;j++)
scanf("%d",&num[j]); //U don't give \n after scanf.
//it give large value at last. (ASCENDING ORDER)
for(i=1;i<7;i++) //loop will move 6 times.
for(j=0;j<7-1;j++) //Also move 6. But need to start at 0.
if(num[j]>num[j+1])
{
guess=num[j];
num[j]=num[j+1];
num[j+1]=guess;
}
}
for(j=0;j<7;j++)
printf(" %2d",num[j]);
printf("\n");
//It give large value at first (DECENDING ORDER)
for(i=1;i<7;i++)
{
for(j=0;j<7-1;j++) //Also move 6. But need to start at 0.
if(num[j]<num[j+1])
{
guess=num[j];
num[j]=num[j+1];
num[j+1]=guess;
}
}
for(j=0;j<7;j++)
printf(" %2d",num[j]);
printf("\n");
return 0;
}
Bubble Sort:
#include<stdio.h>
int main()
{
int num[7],i,j;
int guess;
for(j=0;j<7;j++)
scanf("%d",&num[j]); //U don't give \n after scanf.
//it give large value at last. (ASCENDING ORDER)
for(i=1;i<7;i++) //loop will move 6 times.
//
last digit automatically stay at right position.
{for(j=0;j<7-1;j++) //Also move 6. But need to start at 0.
if(num[j]>num[j+1])
{
guess=num[j];
num[j]=num[j+1];
num[j+1]=guess;
}
}
for(j=0;j<7;j++)
printf(" %2d",num[j]);
printf("\n");
//It give large value at first (DECENDING ORDER)
for(i=1;i<7;i++)
{
for(j=0;j<7-1;j++) //Also move 6. But need to start at 0.
if(num[j]<num[j+1])
{
guess=num[j];
num[j]=num[j+1];
num[j+1]=guess;
}
}
for(j=0;j<7;j++)
printf(" %2d",num[j]);
printf("\n");
return 0;
}
মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন