Use sorting algorithm and Print number of exchange.
;
;
;
;
;
;
;
;
;
;
;
;;
;
;
;
;
CODE:
#include<stdio.h>
int main()
{
int s,i,j,a[1030],t,ans;
while(scanf("%d",&s)==1)
{
ans=0;
for(i=0; i<s; i++)
{
scanf("%d",&a[i]);
}
for(i=1; i<s; i++)
for(j=s-1; j>=i; j--)
if(a[j-1]>a[j])
{
t=a[j-1];
a[j-1]=a[j];
a[j]=t;
ans=ans+1;
}
printf("Minimum exchange operations : %d\n",ans);
}
return 0;
}
Great info :for online practice test visit kidsfront.com/competitive-exams/quantitative-aptitude-practice-test.html
ReplyDelete