What is stable sort




















Say the data base is normally sorted by date time. Then a query is made to make a sorted copy of the data base by last first name, since a stable sort preserves the original order, even though the inquiry compare only involves last first name, the transactions for each last first name will be in data time order.

A similar example is classic Excel, which limited sorts to 3 columns at a time. To sort 6 columns, a sort is done with the least significant 3 columns, followed by a sort with the most significant 3 columns. A classic example of a stable radix sort is a card sorter, used to sort by a field of base 10 numeric columns. The cards are sorted from least significant digit to most significant digit. On each pass, a deck of cards is read and separated into 10 different bins according to the digit in that column.

Then the 10 bins of cards are put back into the input hopper in order "0" cards first, "9" cards last. Then another pass is done by the next column, until all columns are sorted. Actual card sorters have more than 10 bins since there are 12 zones on a card, a column can be blank, and there is a mis-read bin.

To sort letters, 2 passes per column are needed, 1st pass for digit, 2nd pass for the 12 11 zone. Later there were card collating merging machines that could merge two decks of cards by comparing fields. The input was two already sorted decks of cards, a master deck and an update deck. The collator merged the two decks into a a new mater bin and an archive bin, which was optionally used for master duplicates so that the new master bin would only have update cards in case of duplicates.

This was probably the basis for the idea behind the original bottom up merge sort. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. What is stability in sorting algorithms and why is it important? Ask Question. Asked 12 years, 1 month ago. Active 22 days ago. Viewed k times. I'm very curious, why stability is or is not important in sorting algorithms? Improve this question.

Levent Divilioglu 9, 5 5 gold badges 51 51 silver badges bronze badges. DarthVader DarthVader For parallelization purposes? Classic QuickSort is unstable — Konstantin Spirin.

A note for those who might misunderstood the concept like me: The order of equal elements is guaranteed to be preserved. It's not what I used to think: if the elements in previous order are considered equal, then in the coming stable sort, they would follow the previous order. Though you may find the latter understanding also makes sense in many cases. Add a comment. Active Oldest Votes. Suppose we have a list of 5-letter words: peach straw apple spork If we sort the list by just the first letter of each word then a stable-sort would produce: apple peach straw spork In an unstable sort algorithm, straw or spork may be interchanged, but in a stable one, they stay in the same relative positions that is, since straw appears before spork in the input, it also appears before spork in the output.

You can't stack unstable sorts in the same fashion. Improve this answer. Sergio 1, 3 3 gold badges 22 22 silver badges 39 39 bronze badges. Joey Adams Joey Adams With that assumption, straw and spork compare equal.

Stable sort will preserve the order of input, whereas unstable sort does not make that guarantee. The sort function in most programming languages lets the user supply a custom ordering function. If the user's function treats different items as equal e. See OCaml's array sorting functions for a real-world example. I do not understand the line.. What do you mean by key here? Please explain the statement.. So when sorting by first letter, then for each item, its "sorting key" is its first letter.

JoeyAdams Can you please add the information in your comment into your answer. I was about to down vote this as spork does come before straw unless you are only sorting by the first letter.

To me that is not a natural way to sort strings and should be made clear. Example - Say you have a list with each item having information about destination of the flight and departure time. You first sort the list based on time. We then sort it based on destination. Try some questions now. Check out interviewcake. We'll never post on your wall or message your friends. Where do I enter my password? Actually, we don't support password-based login.

Never have. Just the OAuth methods above. Interview Cake. Toggle navigation Interview Cake. Interview Tips First coding interview? Start here. Stable Sort. So it's output might look like this: [ 'Charlie', 2. Suppose, for example, that we have a file containing a list of computing science students who are enrolled in several different lab sections:. If we sort these students alphabetically using their names as the key, then it is very unlikely that they will be grouped nicely into lab sections as well:.

So, we might like to sort again, using the lab section as a key, so that we have all the students in lab section A together, all the students in lab section B together, and so on. If we now do that, we might wind up with this:.



0コメント

  • 1000 / 1000