Given a set of n objects in no specific order (n = 5
in this example):
{
apple,
orange,
banana,
cherry,
cabbage
}
Keep all of the items in an array. Use a separate array / hashmap for relations between pairs. Then use your favorite comparison-based search algorithm (say quicksort).
When two elements are to be compared, find out relative precedence using the map. If there is no known precedence, ask the user.
When a new precedence is added, adjust the precedence order of the whole matrix. For example when the user says apple < banana and then separately banana < cherry, add apple < cherry to the hashmap.
The comparison-based search algorithms are designed to optimize number of comparisons and this translates to optimization of the number of questions asked to the user in this case.