Introduction to Sorting Techniques
Data Structure and Algorithm
Tutorials
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.
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
When you have hundreds of datasets you want to print, you might want
to arrange them in some way.
Once we get the data sorted, we can get the k-th smallest and k-th
largest item in O(1) time.
Searching any element in a huge data set becomes easy. We can use
Binary search method for search if we have sorted data. So, Sorting
become important here.
They can be used in software and in conceptual problems to solve
more advanced problems.
Sorting Basics
In-place Sorting: An in-place sorting algorithm uses constant
space for producing the output (modifies the given array only.
Examples: Selection Sort, Bubble Sort, Insertion Sort and Heap Sort.
Internal Sorting: Internal Sorting is when all the data is placed in
the main memory or internal memory. In internal sorting, the problem
cannot take input beyond allocated memory size.
External Sorting : External Sorting is when all the data that needs to
be sorted need not to be placed in memory at a time, the sorting is
called external sorting. External Sorting is used for the massive amount
of data. For example Merge sort can be used in external sorting as the
whole array does not have to be present all the time in memory,
Stable sorting: When two same items appear in the same order in
sorted data as in the original array called stable sort. Examples: Merge
Sort, Insertion Sort, Bubble Sort.
Hybrid Sorting: A sorting algorithm is called Hybrid if it uses more
than one standard sorting algorithms to sort the array. The idea is to
take advantages of multiple sorting algorithms. For
example IntroSort uses Insertions sort and Quick Sort.
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
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:
Average Worst
Name Best Case Case Case Memory Stable Method Used
Quick Sort nlogn nlogn n^2 logn No Partitioning
Merge Sort nlogn nlogn nlogn n Yes Merging
Heap Sort nlogn nlogn nlogn 1 No Selection
Insertion
n n^2 n^2 1 Yes Insertion
Sort
Insertion &
n nlogn nlogn n Yes
Tim Sort Merging
Selection
n^2 n^2 n^2 1 No Selection
Sort
Shell Sort nlogn n^4/3 n^3/2 1 No Insertion
Bubble Sort n n^2 n^2 1 Yes Exchanging
Tree Sort nlogn nlogn nlogn 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
n n^2 n^2 1 Yes Exchanging
Shaker Sort
Comb Sort nlogn n^2 n^2 1 No Exchanging
Gnome Sort n n^2 n^2 1 Yes Exchanging
Odd–even Exchanging
n n^2 n^2 1 Yes
Sort Indexes: