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

Antworten (1)

madhan ravi
madhan ravi am 8 Dez. 2018

0 Stimmen

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

Gefragt:

am 8 Dez. 2018

Kommentiert:

am 9 Dez. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by