Three way partitioning of an array around a given range | Partitions arr [ 0. . n - 1 ] around [ lowVal . . highVal ] ; Initialize ext available positions for smaller ( than range ) and greater lements ; Traverse elements from left ; If current element is smaller than range , put it on next available smaller position . ; If current element is greater than range , put it on next available greater position . ; Driver code
function threeWayPartition ( arr , n , lowVal , highVal ) { let start = 0 , end = n - 1 ; for ( let i = 0 ; i <= end ; ) { if ( arr [ i ] < lowVal ) { let temp = arr [ start ] ; arr [ start ] = arr [ i ] ; arr [ i ] = temp ; start ++ ; i ++ ; } else if ( arr [ i ] > highVal ) { let temp = arr [ end ] ; arr [ end ] = arr [ i ] ; arr [ i ] = temp ; end -- ; } else i ++ ; } } let arr = [ 1 , 14 , 5 , 20 , 4 , 2 , 54 , 20 , 87 , 98 , 3 , 1 , 32 ] ; let n = arr . length ; threeWayPartition ( arr , n , 10 , 20 ) ; document . write ( " " ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;