Related Articles

# Quotient – Remainder Sort

• Difficulty Level : Easy
• Last Updated : 16 Jul, 2021

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 = MAX/MIN+1 and COL = MIN.
3. For each element in the input array, calculate quotient and remainder and store in the 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 Sorting Algorithm using Quotient and Remainder,
J. Eng. Applied Sci, 12 (Special Issue 7): 8016-8020, 2017, ISSN : 1816-949X

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up