1067. Sort with Swap(0,*) (25)
#include <stdio.h>
#define N 100000+1
void swap(int*a,int*b)
{
int temp=*a;
*a=*b;*b=temp;
}
int FindPosition(int a[],int n,int s)
{
int i;
for(i=s;i<n;++i)
if(a[i]!=i)break;
if(i==n)return 0;
else return i;
}
int main()
{
int n,a[N],count=0;
scanf("%d",&n);
for(int i=0;i<n;++i)
{
int temp;
scanf("%d",&temp);
a[temp]=i;
}
int s=1;
s=FindPosition(a,n,s);
while(s)
{
if(a[0]==0)
{
swap(&a[0],&a[s]);
++count;
}
while(a[0])
{
swap(&a[0],&a[a[0]]);
++count;
}
s=FindPosition(a,n,s);
}
printf("%d",count);
return 0;
}