C-PROGRAM SORTING ELEMENTS IN AN ARRAY
#include<stdio.h>
void main()
{
int a[20],i,j,n,p,ch,min,sw,temp;
do
{
printf("Enter the no of elements ");
scanf("%d",&n); printf("Enter the elements ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("MENU\n1.BUBBLE SORT\n2.SELECTION SORT\n");
printf("enter the choice ");
scanf("%d",&ch);
C tutorial video for beginners
void main()
{
int a[20],i,j,n,p,ch,min,sw,temp;
do
{
printf("Enter the no of elements ");
scanf("%d",&n); printf("Enter the elements ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("MENU\n1.BUBBLE SORT\n2.SELECTION SORT\n");
printf("enter the choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:
for(i=0;i<(n-1);i++)
{
for(j=0;j<(n-i-1);j++)
{
if (a[j]>a[j+1])
{
sw=a[j];
a[j]=a[j+1];
a[j+1]=sw;
}
}
}
printf("The sorted array is\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
break;
case 2:
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
if (a[j]<a[i])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("The sorted list\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
break;
default:
printf("invalid entry");
break;
}
printf("\ndo you want to continue(1/0)\n");
scanf("%d",&p);
} while(p==1);
}
C Tutorial For beginners{
case 1:
for(i=0;i<(n-1);i++)
{
for(j=0;j<(n-i-1);j++)
{
if (a[j]>a[j+1])
{
sw=a[j];
a[j]=a[j+1];
a[j+1]=sw;
}
}
}
printf("The sorted array is\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
break;
case 2:
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
if (a[j]<a[i])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("The sorted list\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
break;
default:
printf("invalid entry");
break;
}
printf("\ndo you want to continue(1/0)\n");
scanf("%d",&p);
} while(p==1);
}
Comments
Post a Comment