A sorting algorithm is said to be stable if two objects with equal or same keys appear in the same order in sorted output as they appear in the input array to be sorted.
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 key or by tweaking it in a way such that its meaning doesn’t change and it becomes stable as well.
Note: Subscripts are only used for understanding the concept. Input : 4A 5 3 2 4B 1 Output : 1 2 3 4B 4A 5 Stable Selection Sort would have produced Output : 1 2 3 4A 4B 5
Selection sort works by finding the minimum element and then inserting it in its correct position by swapping with the element which is in the position of this minimum element. This is what makes it unstable.
Swapping might impact in pushing a key(let’s say A) to a position greater than the key(let’s say B) which are equal keys. which makes them out of desired order.
In the above example 4A was pushed after 4B and after complete sorting this 4A remains after this 4B. Hence resulting in unstability.
Selection sort can be made Stable if instead of swapping, the minimum element is placed in its position without swapping i.e. by placing the number in its position by pushing every element one step forward.
In simple terms use a technique like insertion sort which means inserting element in its correct place.
EXPLANATION WITH EXAMPLE:
Example: 4A 5 3 2 4B 1 First minimum element is 1, now instead of swapping. Insert 1 in its correct place and pushing every element one step forward i.e forward pushing. 1 4A 5 3 2 4B Next minimum is 2 : 1 2 4A 5 3 4B Next minimum is 3 : 1 2 3 4A 5 3 2 4B Repeat the steps until array is sorted. 1 2 3 4A 4B 5
1 2 3 4 4 5
This article is contributed by Shubham Rana. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Program to sort an array of strings using Selection Sort
- Position of an element after stable sort
- Stable sort for descending order
- Selection Sort
- Recursive Selection Sort
- 8086 program for selection sort
- Sorting Algorithms Visualization | Selection Sort
- Iterative selection sort for linked list
- C++ program for Sorting Dates using Selection Sort
- A sorting algorithm that slightly improves on selection sort
- Recursive selection sort for singly linked list | Swapping node links
- Stable QuickSort
- Job Selection Problem - Loss Minimization Strategy | Set 2
- Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?