Striver-SDE-Sheet

Repository containing solution for #SdeSheetChallenge by striver

View the Project on GitHub

Sort 0 1 2

O(N log N) Time Complexity with sort function.

O(N)+O(N) Time using counting sort.

O(N) Time, 3 Pointers, dutch national flag algorithm.

Code

#include <bits/stdc++.h>
void sort012(int *arr, int n)
{
    int low = 0, mid = 0, high = n-1;
    while(mid<=high){
        switch(arr[mid])
        {
            case 0:
                swap(arr[low++],arr[mid++]);
                break;
            case 1:
                mid++;
                break;
            case 2:
                swap(arr[mid],arr[high--]);
                break;
        }
    }
}