In: Computer Science
Convert this strategy into an algorithm and write it below. remember to provide an algorithm header, number the algorithm steps and to use pseudocode conventions.
Scan the input array A, counting the number of negative, zero and positive numbers. Let these counts be x, y and z respectively.
Create a new array B of the same size and set a local variable neg to 1, zero to x+1, and pos to x+y+1.
Scan the input array from left to right, and if the current number being looked at is negative, copy it to B[neg] and increment neg, if it is a zero, copy it to B[zero] and increment zero, and if it is positive, copy it to B[pos] and increment pos.
Output array B.
Algorithm/Pseudo-Code for given Problem.
1)Start of the Program.
2)Scan the Input array and store it in A.
3)Let the Length of the array is Len.
4)Declare integers x,y,z
5)For loop to traverse the elements of array A one by one.
Let i be the iteration index.
for i=1 to Len:
if(A[i]==0)
y++;
else if(A[i]<0)
x++;
else
z++;
i++;
end of for loop
6) Create a new Array B of length Len same as Array A.
7)Declare and Initialize neg=1, zero=x+1 and pos=x+y+1
8) Scan Array A elements and keep them into Array B.
for i=i to Len:
if(A[i]==0)
B[zero]=A[i];
zero++;
else if(A[i]<0)
B[neg]=A[i];
neg++;
else
B[pos]=A[i];
pos++;
i++;
end of for loop
9) Output Array B
10) End of Program
Feel free to ask any doubts, if you face any difficulty in understanding.
Please upvote the answer if you find it helpful