This change may be done in a way which does not compromise a lot on the performance and takes some extra space, possibly. Can we make any sorting algorithm stable? Any given sorting algo which is not stable can be modified to be stable. There can be sorting algo specific ways to make it stable, but in general, any comparison based sorting algorithm which is not stable by nature can be modified to be stable by changing the key comparison operation so that the comparison of two keys considers position as a factor for objects with equal keys.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Skip to content. Change Language. Related Articles. Table of Contents. Save Article. For example, we have a list of some students who belong to different labs, 1. If the use a stable algorithm, it will ensure that the order of previous sorting will be preserved if two keys are the same.
So the names corresponding to the same labs are in sorted order. The result will be. 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. How are we doing? Please help us improve Stack Overflow. Take our short survey. 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. 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. Data Mining. Data Warehouse. Javatpoint Services JavaTpoint offers too many high quality services. Another Definition of Stable Sorting: A Stable Sort is one which preserves the original order of input set, where the comparison algorithm does not distinguish between two or more items.
In Place Sorting Algorithm: An In-Place Sorting Algorithm directly modifies the list that is received as input instead of creating a new list that is then modified. In this Sorting, a small amount of extra space it uses to manipulate the input set.
In other Words, the output is placed in the correct position while the algorithm is still executing, which means that the input will be overwritten by the desired output on run-time.
In-Place, Sorting Algorithm updates input only through replacement or swapping of elements. An algorithm which is not in-place is sometimes called not-in-Place or out of Place.
An Algorithm can only have a constant amount of extra space, counting everything including function call and Pointers, Usually; this space is O log n. Note: Bubble sort, insertion sort, and selection sort are in-place sorting algorithms.
Because only swapping of the element in the input array is required. Bubble sort and insertion sort can be applying as stable algorithms but selection sort cannot without significant modifications.
0コメント