Sort Colors

To solve this coding challenge, we need to sort an array of integers representing colors (0 for red, 1 for white, and 2 for blue) in a specific order: red, white, and blue. The challenge specifically requires an in-place solution and optimally, a one-pass algorithm with constant extra space. This can be achieved using the Dutch National Flag algorithm proposed by Edsger W. Dijkstra.

Explanation

The Dutch National Flag algorithm partitions the array into three parts:
  1. All 0's (red) on the left.
  2. All 1's (white) in the middle.
  3. All 2's (blue) on the right.
  4. We will use three pointers to achieve this:
  5. left
    pointer to track the boundary between 0's and the rest.
  6. right
    pointer to track the boundary between 2's and the rest.
  7. i
    pointer to go through each element in the array.
  8. Initially,
    left
    is set to the beginning of the array,
    right
    is set to the end of the array, and
    i
    is used as an iterator that will go through each element. The general idea is that:
  9. If
    nums[i]
    is 0, swap it with the element at the
    left
    pointer, then move both
    left
    and
    i
    pointers to the right.
  10. If
    nums[i]
    is 2, swap it with the element at the
    right
    pointer, then move the
    right
    pointer to the left.
  11. If
    nums[i]
    is 1, just move the
    i
    pointer to the right.
  12. The loop continues until the
    i
    pointer surpasses the
    right
    pointer. This ensures that all elements are sorted in one pass with constant extra space (using only a few additional variables).
    Here is the detailed pseudocode with explanations:
  13. Initialize pointers :
    •                                             
      left = 0         # Initialize `left` pointer to track the boundary for 0's
      right = length(nums) - 1   # Initialize `right` pointer to track the boundary for 2's
      i = 0             # `i` pointer to iterate through the array
      
                                              
  14. While loop to process the array :
    •                                             
      while i <= right:
      if nums[i] == 0:
      # Swap elements at `left` and `i`
      temp = nums[left]
      nums[left] = nums[i]
      nums[i] = temp
      
      # Move the `left` and `i` pointers to the right
      left += 1
      i += 1
      elif nums[i] == 2:
      # Swap elements at `right` and `i`
      temp = nums[right]
      nums[right] = nums[i]
      nums[i] = temp
      
      # Move the `right` pointer to the left
      right -= 1
      else:
      # If the element is 1, just move the `i` pointer to the right
      i += 1
      
                                              
  15. End of the loop :
    •                                             
      # At this point, the array is sorted in-place with all 0's at the beginning,
      # followed by 1's, and all 2's at the end.
      
                                              
    Step-by-Step Explanation of Pseudocode:
  16. Initialization :
    • Set
      left
      to 0,
      right
      to the last index of the array, and
      i
      to 0.
  17. Iteration and swapping :
    • The loop runs as long as
      i
      is less than or equal to
      right
      .
    • If
      nums[i]
      equals 0, the current element is a red color which should be moved to the front section. Swap this element with the element at the
      left
      pointer and move both
      left
      and
      i
      to the right.
    • If
      nums[i]
      equals 2, the element is blue which should be moved to the end section. Swap it with the element at the
      right
      pointer and move the
      right
      pointer to the left. Do not move the
      i
      pointer because the new element at index
      i
      needs to be checked.
    • If
      nums[i]
      equals 1, the element is white and already in the correct middle section, so simply move the
      i
      pointer to the right.
  18. Completion :
    • When the while loop exits, the array will be sorted in the desired order.
By following this thought-out approach, one can effectively and efficiently sort the colors in the given array in a single pass with minimal extra space. This ensures both optimality and simplicity in addressing the problem requirements.