3,426 views

2 Answers

0 votes
0 votes

Stable sorting algorithms maintain the relative order of records with equal keys (i.e. values). That is, a sorting algorithm is stable if whenever there are two records R and S with the same key and with R appearing before S in the original list, R will appear before S in the sorted list.

Stable sorting algorithms : Insertion sort, Merge sort, Counting sort, Bubble sort

Unstable sorting algorithms : Quick sort, Selection sort, Heap sort

I did not found any standard algorithm to make a non stable algorithm stable, so I think we need to write our own algorithm for this. For maintaining the relative order we need to keep track of the position of element, following is one way to do so :-

Let us assume we need to sort a given array A[i]

step 1) Create an array say B[i] of a new data structure which will store pair of elements VALUE= A[i] and POSITION= i.

step 2)  Sort the array B[i] using any non stable sorting algorithm, by comparing VALUE, if we find VALUE to be equal, then compare and sort those elements using POSITION therefore maintaining the relative order of the elements. 

step 3) After sorting store the array elements(VALUE) of B[i] to A[i]. 

step 4) Exit

In worst case using the above algorithm we have to make n extra comparison therefore in worst case it will take O(n) additional time and because of B[i] it takes O(n) additional space.

edited by

Related questions

1 votes
1 votes
2 answers
1
akash.dinkar12 asked Jun 28, 2019
1,108 views
Show how to sort $n$ integers in the range $0$ to $n^3-1$ in $O(n)$ time.
0 votes
0 votes
0 answers
2
akash.dinkar12 asked Jun 28, 2019
600 views
Use induction to prove that radix sort works. Where does your proof need the assumption that the intermediate sort is stable?
1 votes
1 votes
1 answer
3
akash.dinkar12 asked Jun 28, 2019
683 views
Explain why the worst-case running time for bucket sort is $\Theta(n^2)$. What simple change to the algorithm preserves its linear average-case running time and makes its...
0 votes
0 votes
2 answers
4