0% found this document useful (0 votes)
14 views3 pages

Stable N Inplace Algos

An in-place algorithm transforms input data without requiring extra space, allowing only a small constant amount for variables. Sorting algorithms like Bubble Sort, Selection Sort, and Heapsort are in-place, while Merge Sort is not due to its O(n) space requirement. Stability in sorting algorithms ensures that equal keys maintain their relative order, with stable algorithms including Bubble Sort and Merge Sort, while Quick Sort and Heap Sort are unstable but can be modified to be stable.

Uploaded by

Chitra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Stable N Inplace Algos

An in-place algorithm transforms input data without requiring extra space, allowing only a small constant amount for variables. Sorting algorithms like Bubble Sort, Selection Sort, and Heapsort are in-place, while Merge Sort is not due to its O(n) space requirement. Stability in sorting algorithms ensures that equal keys maintain their relative order, with stable algorithms including Bubble Sort and Merge Sort, while Quick Sort and Heap Sort are unstable but can be modified to be stable.

Uploaded by

Chitra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

What is an in-place Algorithm?

An in-place algorithm is an algorithm that does not need an extra space and produces an
output in the same memory that contains the data by transforming the input ‘in-place’.
However, a small constant extra space used for variables is allowed.

In-place means that the algorithm does not use extra space for manipulating the input but may
require a small though non-constant extra space for its operation. Usually, this space is O(log
n), though sometimes anything in O(n) (Smaller than linear) is allowed.

Which Sorting Algorithms are In-Place and which are not?


In Place : Bubble sort, Selection Sort, Insertion Sort, Heapsort.

Not In-Place : Merge Sort. Note that merge sort requires O(n) extra space.

What about QuickSort? Why is it called In-Place?


QuickSort uses extra space for recursive function calls. It is called in-place according to
broad definition as extra space required is not used to manipulate input, but only for recursive
calls.

Stability in sorting algorithms


What is a stable algorithm?

Stability is mainly important when we have key value pairs with duplicate keys possible (like
people names as keys and their details as values). And we wish to sort these objects by keys.

What is it?
A sorting algorithm is said to be stable if two objects with equal keys appear in the same
order in sorted output as they appear in the input array to be sorted.

Formally stability may be defined as,


Let A be an array, and let < be a strict weak ordering on the elements of A (i.e a[0] < a[1] <
… < a[n-1])
A sorting algorithm is stable iff-

i < j and Ai = Aj (2 values are the same but at different positions in the array)
implies π(i) < π(j)
where π is the sorting permutation (sorting moves A[i] to position π[i] )

Informally, stability means that equivalent elements retain their relative positions, after
sorting.

A sorting algorithm is said to be stable if it maintains the relative order of numbers/records in


the case of tie i.e. if you need to sort 1 1 2 3 then if you don't change order of those first two
ones than your algorithm is stable, but if you swap them then it becomes unstable, despite the
overall result or sorting order remain same.

Which sorting algorithms are stable?


Some Sorting Algorithms are stable by nature, such as Bubble Sort, Insertion Sort, Merge

Compiled by: CSD, SPC [for M.Sc Part – I students only] 2020
Sort, Count Sort and Binary Tree Sort.
Comparison based stable sorts such as Merge Sort and Insertion Sort, maintain stability

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, Selection Sort are unstable, can be made stable by also taking the
position of the elements into consideration. This change may be done in a way which does
not compromise a lot on the performance and takes some extra space, possibly θ(n).

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.

One of the main difference between quicksort and mergesort is that the quicksort is unstable
but merge sort is a stable sorting algorithm.

Example: Stable vs Unstable Algorithm


Suppose you need to sort following key-value pairs in the increasing order of keys:

INPUT: (4,5), (3, 2) (4, 3) (5,4) (6,4)

Now, there is two possible solution for the two pairs where the key is the same i.e. (4,5) and
(4,3) as shown below:

OUTPUT1: (3, 2), (4, 5), (4,3), (5,4), (6,4)


OUTPUT2: (3, 2), (4, 3), (4,5), (5,4), (6,4)

The sorting algorithm which will produce the first output will be known as stable sorting
algorithm because the original order of equal keys are maintained, you can see that (4, 5)
comes before (4,3) in the sorted order, which was the original order i.e. in the given input, (4,
5) comes before (4, 3).

On the other hand, the algorithm which produces second output will know as an unstable
sorting algorithm because the order of objects with the same key is not maintained in the
sorted order. You can see that in the second output, the (4, 3) comes before (4, 5) which was
not the case in the original input.

Remember, Collections.sort() method from Java Collection framework uses iterative merge
sort which is a stable algorithm. It also does far fewer comparison than NLog(N) in case
input array is partially sorted.

Compiled by: CSD, SPC [for M.Sc Part – I students only] 2020
Count Sort n = 8
A
12 56 34 89 22 43 9 16

C count array
6 1 3 0 4 2 7 5
Descending order sort
89, 56, 43, 34, 22, 16, 12, 9 = Sorted array

Ascending order sort


9, 12, …..

In a day
12 (25000), 100(5000), 90(1000), 12 (20000),

12(25000), 12(20000), 90(1000), 100 (5000)


12(20000), 12(25000), 90(1000), 100 (5000) Sorted not stable

Compiled by: CSD, SPC [for M.Sc Part – I students only] 2020

You might also like