Quotient | CPP 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
#include <algorithm> NEW_LINE #include <iostream> NEW_LINE using namespace std ; void QRsort ( int arr [ ] , int size ) { int MAX = * max_element ( arr , arr + size ) ; int MIN = * min_element ( arr , arr + size ) ; cout << " Maximum ▁ Element ▁ found ▁ is ▁ : ▁ " << MAX << endl ; cout << " Minimum ▁ Element ▁ found ▁ is ▁ : ▁ " << MIN << endl ; int COL = MIN ; int ROW = MAX / MIN + 1 ; int matrix [ ROW ] [ COL ] = { 0 } ; for ( int i = 0 ; i < size ; i ++ ) { int quotient = arr [ i ] / MIN ; int remainder = arr [ i ] % MIN ; matrix [ quotient ] [ remainder + 1 ] = 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 ] ; } } } } void printArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) cout << arr [ i ] << " ▁ " ; cout << endl ; } int main ( ) { int arr [ ] = { 5 , 3 , 7 , 4 , 8 , 2 , 6 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Initial ▁ Array ▁ : ▁ " << endl ; printArray ( arr , size ) ; QRsort ( arr , size ) ; cout << " Array ▁ after ▁ sorting ▁ : ▁ " << endl ; printArray ( arr , size ) ; }