hello i hav an assignment to sort about 10000 numbers and to find no.ofexcahange and no.of comparisons in the sort.the data is to be sorted by selection,insertion and bubble sort...the data is sorted correctly but no.of exchange and comparisons are same which is not possible..plzz help me..
Code:
//insertion
int insertion1(int d1[10000])
{
int temp,i,excount=0,comcount=0;;
int b[10000];
d1[0]=-32767; //assigning sentinel value
for (int i=2;i<=10000;i++)
{
temp=d1[i];
int j=i-1;
while ((d1[j]>temp)&& comcount++)
{
d1[j+1]=d1[j];
j--;
excount++;
}
d1[j+1]=temp;
}
cout<<"Number of exchange are:"<<excount<<"\n";
cout<<"Number of comparisons are:"<<comcount<<"\n";
return 0;
}
//bubble
int bubble1(int f1[10000])
{
int temp;
int swapped;
int excount=0,comcount=0;;
for (int i=0;i<10000;i++)
{
swapped=0;
for (int j=0;j<10000;j++)
{
if ((f1[j]>f1[j+1]) && comcount++)
{
{temp=f1[j];
f1[j]=f1[j+1];
f1[j+1]=temp;
excount++;
swapped=1;}
}
}
if (!swapped)
break;
}
cout<<"Number of exchange are:"<<excount<<"\n";
cout<<"Number of comparisons are:"<<comcount<<"\n";
return 0;
}
//selection
int selection1(int e1[10000])
{
int i,excount=0,comcount=0;
int min;
for( int i=0;i<10000;i++)
{
min=i;
for (int j=i+1;j<10000;j++)
{
if ((e1[min]>e1[j])&& comcount++)
{
min=j;
excount++;
}
}
int temp=a1[i];
a1[i]=a1[min];
a1[min]=temp;
}
cout<<"Number of exchange are:"<<excount<<"\n";
cout<<"Number of comparisons are:"<<comcount<<"\n";
return 0;
}