VOOZH about

URL: https://www.geeksforgeeks.org/dsa/introduction-to-sorting-algorithm/

⇱ Introduction to Sorting Techniques - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Introduction to Sorting Techniques

Last Updated : 12 May, 2026

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

Sorting algorithms are essential in Computer Science as they simplify complex problems and improve efficiency. They are widely used in searching, databases, divide and conquer strategies, and data structures.

Key Applications:

  • Organizing large datasets for easier handling and printing
  • Enabling quick access to the k-th smallest or largest elements
  • Making binary search possible for fast lookups in sorted data
  • Solving advanced problems in both software and algorithm design

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 sameorder 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
Sorting algorithm

Basics Sorting Algorithms:

Bubble Sort - O(n^2) Time and O(1) Space

It is a simple sorting algorithm that repeatedly swaps adjacent elements if they are in the wrong order. It performs multiple passes through the array, and in each pass, the largest unsorted element moves to its correct position at the end.

After each pass, we ignore the last sorted elements and continue comparing and swapping remaining adjacent pairs. After k passes, the last k elements are sorted. For more details refer here.


Output
11 12 22 25 34 64 90 

Insertion Sort - O(n^2) Time and O(1) Space

It is a simple sorting algorithm that builds the sorted array one element at a time. It works like sorting playing cards in your hand, where each new card is inserted into its correct position among the already sorted cards.

We start with the second element, assuming the first is already sorted. If the second element is smaller, we shift the first element and insert the second in the correct position. Then we move to the third element and place it correctly among the first two. This process continues until the entire array is sorted. For more details refer here.


Output
5 6 11 12 13 

Selection Sort - O(n^2) Time and O(1) Space

It is a comparison-based sorting algorithm that repeatedly selects the smallest (or largest) element from the unsorted part of the array and swaps it with the first unsorted element. This process continues until the array is fully sorted.

We start by finding the smallest element and swap it with the first element. Then we find the next smallest element among the remaining and swap it with the second element. This continues until all elements are placed in their correct positions. For more details refer here.


Output
11 12 22 25 64 

Counting Sort

It is a non-comparison-based sorting algorithm that works efficiently when the range of input values is small relative to the number of elements. It counts the frequency of each distinct element and uses that count to place elements directly into their correct sorted positions.

However, if the maximum value is much larger than the array size (especially more than n log n), then standard comparison-based algorithms are usually more efficient. For more details refer here.


Output
0 0 2 2 3 3 3 5 

Time Complexity: O(n + m), where n and m are the size of arr[] and count[] respectively
Auxiliary Space: O(n + m)

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:

NameBest Case  Average Case  Worst Case MemoryStable   Method Used
Quick SortNoPartitioning
Merge SortnYesMerging
Heap Sort1NoSelection
Insertion Sortn1YesInsertion
Tim SortnnYesInsertion & Merging
Selection Sort1NoSelection
Shell Sort1NoInsertion
Bubble Sortn1YesExchanging
Cycle Sort1NoSelection
Comment
Article Tags:
Article Tags: