3 | C # program for 3 - way quick sort ; A function which is used to swap values ; A utility function to print an array ; It uses Dutch National Flag Algorithm ; To handle 2 elements ; update i and j ; j = mid ; or high + 1 ; 3 - way partition based quick sort ; if ( low >= high ) 1 or 0 elements ; Note that i and j are passed as reference ; Recur two halves ; Driver code ; int [ ] a = { 4 , 39 , 54 , 14 , 31 , 89 , 44 , 34 , 59 , 64 , 64 , 11 , 41 } ; int [ ] a = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int [ ] a = { 91 , 82 , 73 , 64 , 55 , 46 , 37 , 28 , 19 , 10 } ; int [ ] a = { 4 , 9 , 4 , 4 , 9 , 1 , 1 , 1 } ; ; Function Call
using System ; class GFG { static void swap < T > ( ref T lhs , ref T rhs ) { T temp ; temp = lhs ; lhs = rhs ; rhs = temp ; } public static void printarr ( int [ ] a , int n ) { for ( int i = 0 ; i < n ; ++ i ) Console . Write ( a [ i ] + " ▁ " ) ; Console . Write ( " STRNEWLINE " ) ; } public static void partition ( int [ ] a , int low , int high , ref int i , ref int j ) { if ( high - low <= 1 ) { if ( a [ high ] < a [ low ] ) swap ( ref a [ high ] , ref a [ low ] ) ; i = low ; j = high ; return ; } int mid = low ; int pivot = a [ high ] ; while ( mid <= high ) { if ( a [ mid ] < pivot ) swap ( ref a [ low ++ ] , ref a [ mid ++ ] ) ; else if ( a [ mid ] == pivot ) mid ++ ; else if ( a [ mid ] > pivot ) swap ( ref a [ mid ] , ref a [ high -- ] ) ; } i = low - 1 ; } public static void quicksort ( int [ ] a , int low , int high ) { return ; int i = 0 , j = 0 ; partition ( a , low , high , ref i , ref j ) ; quicksort ( a , low , i ) ; quicksort ( a , j , high ) ; } static void Main ( ) { int [ ] a = { 4 , 9 , 4 , 4 , 1 , 9 , 4 , 4 , 9 , 4 , 4 , 1 , 4 } ; int size = a . Length ; printarr ( a , size ) ; quicksort ( a , 0 , size - 1 ) ; printarr ( a , size ) ; } }