The price of being stable is the overhead of "insert" is much larger than "swap". Alma-Do has a better explanation about this topic. Would have been better if the second example 2,3,1,1 was unstable in the normal algorithm and was stable after the modified algo.
Its stable in either way. The first example is a better one. Alma Do Alma Do First, you have to understand that there is nothing unstable kind of thing with the Selection sort as it vividly depends upon the data structure you are using for the specific. For example, consider the fact that we have an array as 4 2 4 1 and upon sorting this array using the traditional method the result will be 1 2 4 4 , simple. But during the first round of the iteration, the 4 which is present at the 0th index will be placed at the last index which is ethically wrong considering the fact of stability as the 4 which is present on the 2nd index should come later than the 4 present on the 0th index.
You can make a slight modification in the implementation and instead of swapping the numbers, choose the minimum element during the first round and then place that element at its correct position and shift the whole array. I know the shifting operation might cost more than the swapping but this is the best I can think of. If you are concerned about the shifting operation, you may use the Linked list rather than the array and insert the appropriate element at its appropriate position in O 1 time.
Vipul Maheshwari Vipul Maheshwari 1 1 1 bronze badge. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.
Post as a guest Name. Email Required, but never shown. Active 3 years, 8 months ago. Viewed 3k times. Improve this question. Why isn't heapsort stable? JohnnyMopp I have asked specifically for selection sort. Stability is unrelated to complexity. A sort is stable if all items which compare as equal appear in the same order in the output list as in the input list.
It's nothing to do with "forming a strong algorithmic foundation". May be it will help you. Show 3 more comments. Active Oldest Votes. For instance, when sorting the array 2 2' 1 since the element with the minimum key is 1, you'll have to push it to the lowest position of the array by swapping 1 with 2: 1 2' 2 Swapping 1 with 2 changed the relative order of the two equal elements 2' and 2.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write. 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. Save Article. Improve Article. Like Article. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.
Post as a guest Name. Email Required, but never shown. Featured on Meta. Now live: A fully responsive profile. Version labels for answers.
0コメント