Find Minimum in Rotated Sorted Array

In this tutorial, I am going to discuss a very interesting problem find minimum in rotated sorted array.

Given an array which is sorted in ascending order is rotated at some unknown point. Write a code to find the minimum element in a sorted and rotated array.

You may assume no duplicate exists in the array.

For example:

Example 1:

Input: [6, 7, 8, 1, 2]
Output: 1

The minimum element in this sorted and rotated array is 1.

Example 2:

Input: [8, 9, 10, 1, 0, 1, 2]
Output: 0

In this example, the minimum element is 0.

Sort an Array of 0s, 1s and 2s

Given an array consisting of 0s, 1s, and 2s. Write code to sort an array of 0s, 1s, and 2s without using an extra space or a sorting algorithm.

OR

Given an array of size n containing only 0s, 1s, and 2s. Write a code to sort the array in ascending order.

For example:

Input  : {0, 1, 1, 0, 1, 2, 0, 1, 2}

Output : {0, 0, 0, 1, 1, 1, 1, 2, 2}

In this example, the input array only consists of 0s, 1s, and 2s in an unsorted order.

In the output array, all the 0s, 1s and 2s are in sorted order.