6 sorting/10 partition/main.cpp
/*
Given an array and index of pivot element place all the elements which are smaller than that element 
to left of that element and the rest to right
*/
Given an array and index of pivot element place all the elements which are smaller than that element
to left of that element and the rest to right
Given an array and index of pivot element place all the elements which are smaller than that element to left of that element and the rest to right
int naive(int a[], int l, int h, int pivot){    
    int n = h-l+1, index=0;
    int temp[n];
    for(int i=l; i<=h; i++)
        if(a[i] < a[pivot])
            temp[index++] = a[i];
    for(int i=l; i<=h; i++)
        if(a[i] == a[pivot])
            temp[index++] = a[i];
    int res = l+index-1;
    for(int i=l; i<=h; i++)
        if(a[i] > a[pivot])
            temp[index++] = a[i];
    index = 0;
    for(int i=l; i<=h; i++)
        a[i] = temp[index++];
    return res;
}
int naive ( int a [ ] , int l , int h , int pivot ) { int n = h - l + 1 , index = 0 ; int temp [ n ] ; for ( int i = l ; i <= h ; i ++ ) if ( a [ i ] < a [ pivot ] ) temp [ index ++ ] = a [ i ] ; for ( int i = l ; i <= h ; i ++ ) if ( a [ i ] == a [ pivot ] ) temp [ index ++ ] = a [ i ] ; int res = l + index - 1 ; for ( int i = l ; i <= h ; i ++ ) if ( a [ i ] > a [ pivot ] ) temp [ index ++ ] = a [ i ] ; index = 0 ; for ( int i = l ; i <= h ; i ++ ) a [ i ] = temp [ index ++ ] ; return res ; }
Given an array and index of pivot element place all the elements which are smaller than that element
to left of that element and the rest to right
Given an array and index of pivot element place all the elements which are smaller than that element to left of that element and the rest to right
//O(n) space and O(n) time