Quotient | Java program to implement Quotient - Remainder Sort ; max_element finds maximum element in an array ; min_element finds minimum element in an array ; Creating a ROW * COL matrix of all zeros ; Driver Code
import java . util . * ; class GFG { static void QRsort ( int arr [ ] , int size ) { int MAX = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int MIN = Arrays . stream ( arr ) . min ( ) . getAsInt ( ) ; System . out . println ( " Maximum ▁ Element ▁ found ▁ is ▁ : ▁ " + MAX ) ; System . out . println ( " Minimum ▁ Element ▁ found ▁ is ▁ : ▁ " + MIN ) ; int COL = MIN ; int ROW = MAX / MIN + 1 ; int [ ] [ ] matrix = new int [ ROW ] [ COL ] ; for ( int i = 0 ; i < size ; i ++ ) { int quotient = arr [ i ] / MIN ; int remainder = arr [ i ] % MIN ; matrix [ quotient ] [ remainder ] = arr [ i ] ; } int k = 0 ; for ( int i = 0 ; i < ROW ; i ++ ) { for ( int j = 0 ; j < COL ; j ++ ) { if ( matrix [ i ] [ j ] != 0 ) { arr [ k ++ ] = matrix [ i ] [ j ] ; } } } } static void printArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 3 , 7 , 4 , 8 , 2 , 6 } ; int size = arr . length ; System . out . println ( " Initial ▁ Array ▁ : ▁ " ) ; printArray ( arr , size ) ; QRsort ( arr , size ) ; System . out . println ( " Array ▁ after ▁ sorting ▁ : ▁ " ) ; printArray ( arr , size ) ; } }