In this post, we will see how to sort N2 numbers in increasing order using an N × N Young tableau in O(N3) time.

An N × N Young tableau is an N × N matrix such that entries of each row are sorted from left to right and the entries of each column are sorted from top to bottom. Some entries of a Young tableau may be infinity, which indicates an empty entry. Thus, a Young tableau can be used to hold n <= N2 finite numbers.

 
Refer the following article on Young tableau as a prerequisite of this post:

Young Tableau | Insert, Search, Extract-Min, Delete, Replace

Practice this problem

 
To sort an array using Young tableau, insert each of its values into an empty Young tableau, one at a time. Afterward, repeatedly call the Extract-Min routine on the Young tableau until the tableau is empty and put the returned values back to the original array.

Following is the C++, Java, and Python program that demonstrates it:

C++


Download  Run Code

Output:

1 2 3 4 5 6 7 8

Java


Download  Run Code

Output:

[1, 2, 3, 4, 5, 6, 7, 8]

Python


Download  Run Code

Output:

[1, 2, 3, 4, 5, 6, 7, 8]

Constructing an O(N × N) Young tableau from O(N2) keys takes O(N3) time. Also, the extractMin() routine is called O(N × N) times, which runs in linear time. So, the overall time complexity of the proposed solution is O(N3) for O(N2) keys. The additional space used by the program is O(N2) for constructing an N × N Young tableau.

In order words, the sorting procedure runs in O(n1.5) time and O(n) space, where n is the size of the input.