What is the benefit for a sort algorithm to be stable?

后端 未结 10 2411
被撕碎了的回忆
被撕碎了的回忆 2020-12-12 22:35

A sort is said to be stable if it maintains the relative order of elements with equal keys. I guess my question is really, what is the benefit of maintaining this relative o

10条回答
  •  执笔经年
    2020-12-12 23:23

    One case is when you want to sort by multiple keys. For example, to sort a list of first name / surname pairs, you might sort first by the first name, and then by the surname.

    If your sort was not stable, then you would lose the benefit of the first sort.

提交回复
热议问题