Introduction to Sorting Techniques – Data Structure and Algorithm Tutorials (original) (raw)

Last Updated : 21 Oct, 2024

**Sorting refers to rearrangement of 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.

Introduction-to-Sorting-Techniques

Why Sorting Algorithms are Important

The sorting algorithm is important in Computer Science because it reduces the complexity of a problem. There is a wide range of applications for these algorithms, including searching algorithms, database algorithms, divide and conquer methods, and data structure algorithms.

In the following sections, we list some important scientific applications where sorting algorithms are used

Sorting Basics

Types of Sorting Techniques

There are various sorting algorithms are used in data structures. The following two types of sorting algorithms can be broadly classified:

  1. **Comparison-based: We compare the elements in a comparison-based sorting algorithm)
  2. **Non-comparison-based: We do not compare the elements in a non-comparison-based sorting algorithm)

Sorting algorithm

Sorting algorithm

Some of the most common sorting algorithms are:

Selection sort, Bubble sort, Insertion Sort, Cycle Sort, Merge Sort, 3-way Merge Sort, Quick sort, Heap sort and Counting sort

Some other Sorting algorithms:

Radix sort, Bucket sort, Shell sort, Tim Sort, Comb Sort, Pigeonhole sorting, Cocktail Sort, Strand sort, Bitonic Sort, Stooge Sort, Tag Sort, Tree sort, Cartesian Sort, Odd-Even Sort / Brick Sort, Gnome sort, Cocktail shaker sort

Comparison of Complexity Analysis of Sorting Algorithms:

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 Indexes: