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

July 03, 2022 · 1 min read

https://practice.geeksforgeeks.org/problems/sort-an-array-of-0s-1s-and-2s/0

class Solution {
public:
  void sort012(int arr[], int n) {
    int low = 0, mid = 0, high = n - 1;
    while (mid <= high) {
      if (arr[mid] == 0) {
        swap(arr[mid], arr[low]);
        low++;
        mid++;
      } else if (arr[mid] == 1) {
        mid++;
      } else {
        swap(arr[mid], arr[high]);
        high--;
      }
    }
  }
};
Find the Kth max and min element in an array
Move all the negative elements to one side of the array