# Quotient – Remainder Sort

Quotient – Remainder Sort is a non-comparison based sorting algorithm. Steps to perform Quotient – Remainder sort as described below :

1. Find MIN and MAX of the array.
2. Create an ROW*COL matrix consisting of 0s, where ROW = MIN and COL = MAX/MIN + 1
3. For each element in input array, calculate quotient and remainder and store in matrix[quotient][remainder]
4. For each element in matrix, if element != 0, append element to sorted array

Examples:

```Input : 2 5 3 7 4 8
MAX = 8, MIN = 2, ROW = 4, COL 2
matrix : 2 3
4 5
0 7
8 0
Output : 2 3 4 5 7 8
```

## C++

 `// CPP program to implement Quotient - Remainder ` `// Sort ` `#include ` `#include ` `using` `namespace` `std; ` ` `  `void` `QRsort(``int` `arr[], ``int` `size) ` `{ ` `    ``// max_element finds maximum element in an array ` `    ``int` `MAX = *max_element(arr, arr + size); ` ` `  `    ``// min_element finds minimum element in an array ` `    ``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; ` ` `  `    ``// Creating a ROW * COL matrix of all zeros ` `    ``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); ` `    ``cout << ``"Initial Array : "` `<< endl; ` `    ``printArray(arr, size); ` ` `  `    ``QRsort(arr, size); ` ` `  `    ``cout << ``"Array after sorting : "` `<< endl; ` `    ``printArray(arr, size); ` `} `

## Java

 `// Java program to implement  ` `// Quotient - Remainder Sort  ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` `static` `void` `QRsort(``int` `arr[], ``int` `size)  ` `{ ` `    ``// max_element finds maximum element in an array  ` `    ``int` `MAX = Arrays.stream(arr).max().getAsInt(); ` ` `  `    ``// min_element finds minimum element in an array  ` `    ``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``; ` ` `  `    ``// Creating a ROW * COL matrix of all zeros  ` `    ``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(); ` `} ` ` `  `// Driver Code ` `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); ` `} ` `} ` ` `  `// This code is contributed by Princi Singh `

## C#

 `// C# program to implement  ` `// Quotient - Remainder Sort  ` `using` `System; ` `using` `System.Linq; ` ` `  `class` `GFG  ` `{ ` `static` `void` `QRsort(``int` `[]arr, ``int` `size)  ` `{ ` `    ``// max_element finds maximum element in an array  ` `    ``int` `MAX = arr.Max(); ` ` `  `    ``// min_element finds minimum element in an array  ` `    ``int` `MIN = arr.Min(); ` ` `  `    ``Console.WriteLine(``"Maximum Element found is : "` `+ MAX); ` `    ``Console.WriteLine(``"Minimum Element found is : "` `+ MIN); ` ` `  `    ``int` `COL = MIN; ` `    ``int` `ROW = MAX / MIN + 1; ` ` `  `    ``// Creating a ROW * COL matrix of all zeros  ` `    ``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++)  ` `    ``{ ` `        ``Console.Write(arr[i] + ``" "``); ` `    ``} ` `    ``Console.WriteLine(); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `[]arr = {5, 3, 7, 4, 8, 2, 6}; ` `    ``int` `size = arr.Length; ` `    ``Console.WriteLine(``"Initial Array : "``); ` `    ``printArray(arr, size); ` ` `  `    ``QRsort(arr, size); ` ` `  `    ``Console.WriteLine(``"Array after sorting : "``); ` `    ``printArray(arr, size); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```Initial Array :
5 3 7 4 8 2 6
Maximum Element found is : 8
Minimum Element found is : 2
Array after sorting :
2 3 4 5 6 7 8
```

References
Abul Hasnat, Tanima Bhattacharyya, Atanu Dey, Santanu Halder and Debotosh Bhattachajee, 2017. A Novel Soring Algorithm using Quotient and Remainder,
J. Eng. Applied Sci, 12 (Special Issue 7): 8016-8020, 2017, ISSN : 1816-949X

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : princi singh, Rajput-Ji