How to write this pseudocode in matlab?

a=[5,9,13,2,7,10,4,8,11,1];
int phase, x, t;
for (phase=0; phase<n; phase++)
if(phase%2 ==0)
{
for(x=1;x<n;x+=2)
if(a[x-1]>a[x])
{
t=a[x];
a[x]=a[x-1];
a[x-1]=t;
}
end
end
}
else
{
for(x=1; x<n-1; x +=2)
if(a[x]>a[x+1])
{
t=a[x];
a[x]=a[x+1];
a[x+1]=t;
}
}
end
end
end
end

1 Comment

please don’t close question that have an answer

Sign in to comment.

Answers (1)

Disclaimer : Just an approximate code
x=1;
phase=0;
while phase<n
if phase==0
while x<n
if a(x-1)>a(x)
t=a(x);
a(x)=a(x-1);
a(x-1)=t;
end
x=x+2;
end
end
phase=phase+1;
end

Asked:

on 8 Dec 2018

Commented:

on 9 Dec 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!