Home » Algorithms

Stability in sorting

Here, we are going to learn about the sorting and stability of the sorting.
Submitted by Himanshu Singh Bisht, on November 09, 2018

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.

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.

Unstable Algorithm

NameSection
HimanshuA
AakashA
RaviA
RajuB
DeekshaB
AyushB
SamikshaC

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.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.