#include<stdio.h>
#include<conio.h>
int a[1000];
int main()
{
int i,j,s,temp,p=0,h=0,op,ans,upper,c,n,y,x,count=0;
printf("Enter the upper limits");
scanf("%d",&upper);
for(c=0;c<upper;c++)
{
a[c]=rand()%1000+1;
}
do
{
printf("enter sorting operations to perfom:/t 1-Insertion sort 2-selection sort 3-exit");
scanf("%d",&op);
{
switch(op)
{
case 1:
{
for(i=1;i<upper;i++)
{
temp=a[i];
j=i-1;
h++;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
p++;
}
a[j+1]=temp;
}
printf("After sorting: ");
for(i=0;i<upper;i++)
printf(" %d",a[i]);
printf("\nNO OF COUNT FOR OUTER LOOP IS =%d\n",h);
printf("\nNO OF COUNT IS =%d",p);
break;
}
case 2:
{
for(x=0; x<n; x++)
{
int index_of_min = x;
for(y=x;y<n;y++)
{
if(a[index_of_min]>a[y])
{
index_of_min = y;
count++;
}
}
int temp = a[x];
a[x] = a[index_of_min];
a[index_of_min] = temp;
}
printf("After sorting is: ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
printf("\nThe count is :%d",count);
break;
}
case 3:
{
exit(0);
break;
}
default:
{
printf("wrong choice");
}
}
}
printf("do you want to cont.if yes press 1 or else 0\n");
scanf("%d",&ans);
}while(ans==1);
getch();
return 0;
}
#include<conio.h>
int a[1000];
int main()
{
int i,j,s,temp,p=0,h=0,op,ans,upper,c,n,y,x,count=0;
printf("Enter the upper limits");
scanf("%d",&upper);
for(c=0;c<upper;c++)
{
a[c]=rand()%1000+1;
}
do
{
printf("enter sorting operations to perfom:/t 1-Insertion sort 2-selection sort 3-exit");
scanf("%d",&op);
{
switch(op)
{
case 1:
{
for(i=1;i<upper;i++)
{
temp=a[i];
j=i-1;
h++;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
p++;
}
a[j+1]=temp;
}
printf("After sorting: ");
for(i=0;i<upper;i++)
printf(" %d",a[i]);
printf("\nNO OF COUNT FOR OUTER LOOP IS =%d\n",h);
printf("\nNO OF COUNT IS =%d",p);
break;
}
case 2:
{
for(x=0; x<n; x++)
{
int index_of_min = x;
for(y=x;y<n;y++)
{
if(a[index_of_min]>a[y])
{
index_of_min = y;
count++;
}
}
int temp = a[x];
a[x] = a[index_of_min];
a[index_of_min] = temp;
}
printf("After sorting is: ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
printf("\nThe count is :%d",count);
break;
}
case 3:
{
exit(0);
break;
}
default:
{
printf("wrong choice");
}
}
}
printf("do you want to cont.if yes press 1 or else 0\n");
scanf("%d",&ans);
}while(ans==1);
getch();
return 0;
}
No comments:
Post a Comment