Recover Rotated Sorted Array

Problem: Given a rotated sorted array, recover the sorted array in-place.

For example:
{ 4, 5, 1, 2, 3 } –> {1, 2, 3, 4, 5}

Challenge requirement:
in-palce, O(1) extra space and O(n) time

Solution:
3-step reverse operation
Step 1: find two different sub array sections {4, 5}, {1, 2, 3}
Step 2. reverse sub array elements to: {5, 4}, { 3, 2, 1}
Step 3: reverse all array elements to {1, 2, 3, 4, 5}

Code:

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on RedditShare on StumbleUponEmail this to someoneShare on TumblrDigg this

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">