Sorting a set of objects by a user's preference

前端 未结 10 1897
终归单人心
终归单人心 2020-12-13 16:09

Given a set of n objects in no specific order (n = 5 in this example):

{
    apple,
    orange,
    banana,
    cherry,
    cabbage
}

10条回答
  •  -上瘾入骨i
    2020-12-13 16:36

    1. 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).

    2. When two elements are to be compared, find out relative precedence using the map. If there is no known precedence, ask the user.

    3. 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.

提交回复
热议问题