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

后端 未结 10 2406
被撕碎了的回忆
被撕碎了的回忆 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:14

    A priority queue is an example of this. Say you have this:

    1. (1, "bob")
    2. (3, "bill")
    3. (1, "jane")

    If you sort this from smallest to largest number, an unstable sort might do this.

    1. (1, "jane")
    2. (1, "bob")
    3. (3, "bill")

    ...but then "jane" got ahead of "bob" even though it was supposed to be the other way around.

    Generally, they are useful for sorting multiple entries in multiple steps.

    0 讨论(0)
  • 2020-12-12 23:18

    Not all sorting is based upon the entire value. Consider a list of people. I may only want to sort them by their names, rather than all of their information. With a stable sorting algorithm, I know that if I have two people named "John Smith", then their relative order is going to be preserved.

    Last     First       Phone
    -----------------------------
    Wilson   Peter       555-1212
    Smith    John        123-4567
    Smith    John        012-3456
    Adams    Gabriel     533-5574
    

    Since the two "John Smith"s are already "sorted" (they're in the order I want them), I won't want them to change positions. If I sort these items by last, then first with an unstable sorting algorithm, I could end up either with this:

    Last     First       Phone
    -----------------------------
    Adams    Gabriel     533-5574
    Smith    John        123-4567
    Smith    John        012-3456
    Wilson   Peter       555-1212
    

    Which is what I want, or I could end up with this:

    Last     First       Phone
    -----------------------------
    Adams    Gabriel     533-5574
    Smith    John        012-3456
    Smith    John        123-4567
    Wilson   Peter       555-1212
    

    (You see the two "John Smith"s have switched places). This is NOT what I want.

    If I used a stable sorting algorithm, I would be guaranteed to get the first option, which is what I'm after.

    0 讨论(0)
  • 2020-12-12 23:19

    Let's say you are sorting on an input set which has two fields, and, you only sort on the first. The '|' character divides the fields.

    In the input set you have many entries, but, you have 3 entries that look like

    . . . AAA|towing . . . AAA|car rental . . . AAA|plumbing . . .

    Now, when you get done sorting you expect all the fields with AAA in them to be together.

    A stable sort will give you: . . . AAA|towing AAA|car rental AAA|plumbing . . .

    ie, the three records which had the same sort key, AAA, are in the same order in the output that they were in the input. Note that they are not sorted on the second field, because you didn't sort on the second field in the record.

    An unstable sort will give you: . . . AAA|plumbing AAA|car rental AAA|towing . . .

    Note that the records are still sorted only on the first field, and, the order of the second field differs from the input order.

    An unstable sort has the potential to be faster. A stable sort tends to mimic what non-computer scientist/non-math folks have in their mind when they sort something. Ie, if you did an insertion sort with index cards you would most likely have a stable sort.

    0 讨论(0)
  • 2020-12-12 23:20

    It enables your sort to 'chain' through multiple conditions.

    Say you have a table with first and last names in random order. If you sort by first name, and then by last name, the stable sorting algorithm will ensure people with the same last name are sorted by first name.

    For example:

    • Smith, Alfred
    • Smith, Zed

    Will be guaranteed to be in the correct order.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-12 23:27

    You can't always compare all the fields at once. A couple of examples: (1) memory limits, where you are sorting a large disk file, and there isn't room for all the fields of all records in main memory; (2) Sorting a list of base class pointers, where some of the objects may be derived subclasses (you only have access to the base class fields).

    Also, stable sorts have deterministic output given the same input, which can be important for debugging and testing.

    0 讨论(0)
提交回复
热议问题