×

Algorithms Tutorial

Searching Algorithms

Dynamic Programming

Graph Algorithms

Backtracking Algorithms

Recursion

Operating System Algorithms

Miscellaneous Topics

What is stability in sorting?

In this tutorial, we will learn about the sorting and stability of the sorting. By Himanshu Singh Bisht Last updated : August 12, 2023

What is sorting?

It means to arrange data elements in increasing or decreasing order. There are many algorithms to do so like mergesort, quicksort, counting sort etc but there are pros and cons of each algorithm.

Stability of sorting

One way to judge the algorithm is the stability of sorting. Stability means that the relative position of elements remain same after sorting if an equal key element exists.

To demonstrate it suppose a table having name column and section column.

NameSection
HimanshuA
RajuB
AakashA
SamikshaC
AyushB
RaviA
DeekshaB

Consider a scenario of sorting on basis of the name of student and section also. Now the first sort according to the name of the student Table will be like:

NameSection
AakashA
AyushB
DeekshaB
HimanshuA
RajuB
RaviA
SamikshaC

Now sort according to the section, there will be two output based on the algorithm is stable or not. Due to unstable algorithm now the name has become unsorted so either it will be sorted in name order or section order.

Example of unstable algorithm

NameSection
HimanshuA
AakashA
RaviA
RajuB
DeekshaB
AyushB
SamikshaC

Example of stable algorithm

NameSection
AakashA
HimanshuA
RaviA
AyushB
DeekshaB
RajuB
SamikshaC

As observed with unstable sort task cannot be achieved because one sorting is disordering previous sorting that is why stable sort will be preferred in the database.

Related Tutorials

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.