please be neat. thank you. Develop an algorithm for the following problem: a.Ask
ID: 3723294 • Letter: P
Question
please be neat. thank you.
Develop an algorithm for the following problem:
a.Ask the user to input a vector of any desired size. Also, the user needs to input a value for Ascend. The value could be either 1, meaning Ascending or 0 meaning Descending.
b.Develop an accurate flow chat that generates another vector, w, in the requested order.
c.No MATLAB programming is required.
d.Test your program against the following test vector:
V= [-2, 3, 0, 5]
e.The result should be w-2 0 3 5] if Ascend was 1 or w=[5 3 0 -2] if Ascend was 0.
f.Show the loops by simply initializing with the initial value and checking for end of loop.
Explanation / Answer
Develop an algorithm for the following problem:
algorithm vector_sort(A[0....n-1],k)
// input: a non-empty vector A and k. k can be 1 or 0. 1 means ascending order and 0 means descending order
// Output: Vector W in Sorted order either ascending or descending.
for i=0:A.length-1
// copy vector A into Vector W
W[i]=A[i];
if k==1 //for Ascending order
for i=0:W.length-1
{
for j=0:W.length-1
{
if(W[j]>W[j+1])
swap(W[j],W[j+1]);
} //end of inner loop
} //end of outer loop
else if k==0 // for Descending order
for i=0:W.length-1
{
for j=0:W.length-1
{
if(W[j]<W[j+1])
swap(W[j],W[j+1]);
} //end of inner loop
} //end of outer loop
endif
for i:0:W.length-1
print W[i];
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.