Question

# Design and implement an algorithm that is given as input an integer value $k \geq 0$ and a list of k numbers $\mathrm{N}_{1}, \mathrm{N}_{2}, \ldots, \mathrm{N}_{\mathrm{k}}$ Your algorithm should reverse the order of the numbers in the list. That is, if the original list contained: $\mathrm{N}_{1}=5, \mathrm{N}_{2}=13, \mathrm{N}_{3}=8, \mathrm{N}_{4}=27, \mathrm{N}_{5}=10(\mathrm{k}=5)$ then when your algorithm has completed, the values stored in the list will be: $\mathrm{N}_{1}=10, \mathrm{N}_{2}=27, \mathrm{N}_{3}=8, \mathrm{N}_{4}=13, \mathrm{N}_{5}=5$

Solution

Verified

Let's store the elements in reversed order in the sequence: $R_1, R_2, ... R_k$

After that, let's copy the already reversed list back into $N_1, N_2, ... N_k$

• Get the value of $k$
• Get the elements of the list $N_1, N_2, ... N_k$
• Set the value of $i$ to 1
• While $\left(i\leq k \right)$ do
•      Set value of $R_i$ to $N_{k+1-i}$
•      Increment $i$ by 1
• End of loop
• Set value of $i$ to 1
• While $\left(i\leq k \right)$ do
•      Set value of $N_i$ to $R_i$
•      Increment $i$ by 1
• End of loop
• Print the result: $N_1, N_2, ... N_k$

## Recommended textbook solutions #### Computer Organization and Design MIPS Edition: The Hardware/Software Interface

5th EditionISBN: 9780124077263 (5 more)David A. Patterson, John L. Hennessy
220 solutions #### Fundamentals of Database Systems

7th EditionISBN: 9780133970777 (1 more)Ramez Elmasri, Shamkant B. Navathe
687 solutions #### Introduction to Algorithms

3rd EditionISBN: 9780262033848 (2 more)Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen
726 solutions #### Invitation to Computer Science

7th EditionISBN: 9781305075771 (1 more)G Michael Schneider, Judith L. Gersting
112 solutions