Sorting Algorithms

  • Last Updated : 26 Sep, 2023

DSA for Beginners
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 Algorithms

Sorting Algorithms:

Table of Complexity Comparison:

Name Best Case   Average Case   Worst Case  Memory Stable    Method Used
Quick Sort n log n n log n n^{2} log n No Partitioning
Merge Sort n log n n log n n log n n Yes Merging
Heap Sort n log n n log n n log n 1 No Selection
Insertion Sort n n^{2} n^{2} 1 Yes Insertion
Tim Sort n n log n n log n n Yes Insertion & Merging
Selection Sort n^{2} n^{2} n^{2} 1 No Selection
Shell Sort n log n n^{4/3} n^{3/2} 1 No Insertion
Bubble Sort n n^{2} n^{2} 1 Yes Exchanging
Tree Sort n log n n log n n log n n Yes Insertion
Cycle Sort n^{2} n^{2} n^{2} 1 No Selection
Strand Sort n n^{2} n^{2} n Yes Selection
Cocktail Shaker Sort n n^{2} n^{2} 1 Yes Exchanging
Comb Sort n log n n^{2} n^{2} 1 No Exchanging
Gnome Sort n n^{2} n^{2} 1 Yes Exchanging
Odd–even Sort n n^{2} n^{2} 1 Yes Exchanging

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.