When is a sorting algorithm stable




















I know there are many answers for this, but to me, this answer , by Robert Harvey , summarized it much more clearly:. A stable sort is one which preserves the original order of the input set, where the [unstable] algorithm does not distinguish between two or more items.

However, objects with same priority in sorting may be distinct, and sometime their relative order is meaningful information. In this case, unstable sort generates problems. For example, you have a list of data which contains the time cost [T] of all players to clean a maze with Level [L] in a game. Suppose we need to rank the players by how fast they clean the maze. However, an additional rule applies: players who clean the maze with higher-level always have a higher rank, no matter how long the time cost is.

Of course you might try to map the paired value [T,L] to a real number [R] with some algorithm which follows the rules and then rank all players with [R] value. However, if stable sorting is feasible, then you may simply sort the entire list by [T] Faster players first and then by [L]. In this case, the relative order of players by time cost will not be changed after you grouped them by level of maze they cleaned. PS: of course the approach to sort twice is not the best solution to the particular problem but to explain the question of poster it should be enough.

For instance [2,1,2] will be sorted using stable sort as permutation [2,1,3] first is index 2, then index 1 then index 3 in sorted output That mean that output is always shuffled same way. Other non stable, but still correct permutation is [2,3,1]. Quick sort is not stable sort and permutation differences among same elements depends on algorithm for picking pivot.

Some implementations pick up at random and that can make quick sort yielding different permutations on same input using same algorithm. Some more examples of the reason for wanting stable sorts. Databases are a common example. Take the case of a transaction data base than includes last first name, date time of purchase, item number, price.

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 21 days ago. There are a lot of qualities of each sorting algorithm that allow you to choose what algorithm fits your requirements, but one of the sorting algorithm qualities I want to talk about in this post is stability.

Stability of a sorting algorithmn is the label that indicates whether or not it will preserve the order of two or more elements in the array if they have equal sorting keys.

A sorting algorithm is stable if it preserves the order of multiple elements with the same keys. Conversely, a sorting algorithm is unstable if it cannot guarantee order preservation of multiple elements with equal keys. An example in real life: There is a line of people that need to be ordered by age. If there are two people with the same age standing in line, the sorter would be using a stable approach if there is a guarantee that their order in line relative to each remains the same.

Sorting stability is interesting by itself, but when do we care about it? We care in a situation when we require the output to have the guarantee of preserving order for duplicate keys. Here is a visual respresentation of the data:. 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. Share Tweet Share. Interview coming up? But in doing so, if the sorting algorithm is not stable, we might get a result like this-. The dataset is now sorted according to sections, but not according to names. In the name-sorted dataset, the tuple was before , but since the sorting algorithm is not stable, the relative order is lost.

If on the other hand we used a stable sorting algorithm, the result would be- Here the relative order between different tuples is maintained.

It may be the case that the relative order is maintained in an Unstable Sort but that is highly unlikely. Which sorting algorithms are stable? Comparison based stable sorts such as Merge Sort and Insertion Sort, maintain stability by ensuring that- Element comes before if and only if , here i, j are indices and. Since , the relative order is preserved i. Other non-comparison based sorts such as Counting Sort maintain stability by ensuring that the Sorted Array is filled in a reverse order so that elements with equivalent keys have the same relative position.

Some sorts such as Radix Sort depend on another sort, with the only requirement that the other sort should be stable. Which sorting algorithms are unstable? Quick Sort , Heap Sort etc.



0コメント

  • 1000 / 1000