Sorting Algorithms

  • Last Updated : 12 May, 2023

Learn more about Sorting in DSA Self Paced Course
Practice Problems on Sorting
Top Quizzes on Sorting Algorithms

What is Sorting?

A Sorting Algorithm is used to rearrange a given array or list of elements according to a comparison operator on the elements. The comparison operator is used to decide the new order of elements in the respective data structure.

For Example: The below list of characters is sorted in increasing order of their ASCII values. That is, the character with a lesser ASCII value will be placed first than the character with a higher ASCII value.

Sorting

Example of Sorting

Sorting Algorithms:

Table of Complexity Comparison:

NameBest Case  Average Case  Worst Case MemoryStable   Method Used
Quick Sortn log nn log nn^{2}log nNoPartitioning
Merge Sortn log nn log nn log nnYesMerging
Heap Sortn log nn log nn log n1NoSelection
Insertion Sortnn^{2}n^{2}1YesInsertion
Tim Sortnn log nn log nnYesInsertion & Merging
Selection Sortn^{2}n^{2}n^{2}1NoSelection
Shell Sortn log nn^{4/3}n^{3/2}1NoInsertion
Bubble Sortnn^{2}n^{2}1YesExchanging
Tree Sortn log nn log nn log nnYesInsertion
Cycle Sortn^{2}n^{2}n^{2}1NoSelection
Strand Sortnn^{2}n^{2}nYesSelection
Cocktail Shaker Sortnn^{2}n^{2}1YesExchanging
Comb Sortn log nn^{2}n^{2}1NoExchanging
Gnome Sortnn^{2}n^{2}1YesExchanging
Odd–even Sortnn^{2}n^{2}1YesExchanging

Library Implementations:

  1. Introsort – C++’s Sorting Weapon
  2. Comparator function of qsort() in C
  3. sort() in C++ STL
  4. C qsort() vs C++ sort()
  5. Arrays.sort() in Java with examples
  6. Collections.sort() in Java with Examples

Some standard problems on Sorting:

Quick Links :

  1. ‘Practice Problems’ on Sorting
  2. ‘Quizzes’ on Sorting

Recomended:

If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up