Solution Review: Sorting From 1 to n

This review provides a detailed analysis of the different ways to sort numbers from 1 to n.

First solution

This solution consists of two nested loops. The outer loop will replace the value at index i with -1. The inner loop will do the following:

  • Store the current value temporarily.
  • Set the required value at the proper index.
  • Set value for the next iteration.
  • Set the cursor for the next iteration.

Let’s look more through illustrations.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.