sorting a array with 0,1,2 elements only.

 

void sort_0_1_2(int a[], int n)
{
    // sort(a,a+n); //direct function
    int low,mid,high;
    low=mid=0;
    high=n-1;
    while(mid<=high)
    {
        if(a[mid]==0)
          {
              swap(a[mid],a[low]);
          low++;
          mid++;
          }
          else if(a[mid]==2)
          {
              swap(a[mid],a[high]);
             
                
                  high--;
             
          }
          else
          mid++;
    }
}

Comments

Popular posts from this blog

Amazing Subarrays(cpp,interviewbit)

Symmetric Tree(leetcode,cpp):

sum of left leaves in a tree(leetcode).