05 Sort 0s, 1s and 2s

GeeksForGeeks Question
Solution
void sort012(int a[], int n)
{
    int low = 0, mid = 0, high = n - 1;
    
    while(mid <= high) {
        switch(a[mid]) {
            case 0:
                swap(a[low++], a[mid++]);
            break;
            case 1:
                mid++;
            break;
            case 2:
                swap(a[mid], a[high--]);
            break;
        }
    }
}

Time Complexity: O(n)

Space Complexity: O(1)

Last updated