# Sorting Algorithms

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.

Table of Content

## What is Sorting?

** 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. Sorting means reordering of all the elements either in ascending or in descending order.

## Sorting Terminology:

An in-place sorting algorithm uses**In-place Sorting:**for producing the output (modifies the given array only). It sorts the list only by modifying the order of the elements within the list.**constant space**Examples: Selection Sort, Bubble Sort Insertion Sort and Heap Sort.Internal Sorting is when all the data is placed in theÂ**Internal Sorting:**Â orÂ**main memory**. In internal sorting, the problem cannot take input beyond its size. Example: heap sort, bubble sort, selection sort, quick sort, shell sort, insertion sort.**internalÂ memory**External Sorting is when all the data that needs to be sorted cannot be placed in memory at a time, the sorting is called external sorting. External Sorting is used for the massive amount of data. Examples: Merge sort, Tag sort, Polyphase sort, Four tape sort, External radix sort, etc.**External Sorting :**When two same data appear in theÂ**Stable sorting:**Â**same**Â in sorted data without changing their position is called stable sort. Examples: Merge Sort, Insertion Sort, Bubble Sort.**order**When two same data appear in theÂ**Unstable sorting:**Â**different**Â in sorted data it is called unstable sort. Examples:Â Quick Sort, Heap Sort, Shell Sort**order***.*

## Characteristics of Sorting Algorithms:

Â Time complexity, a measure of how long it takes to run an algorithm, is used to categorize sorting algorithms. The worst-case, average-case, and best-case performance of a sorting algorithm can be used to quantify the time complexity of the process.**Time Complexity:**Â Sorting algorithms also have space complexity, which is the amount of memory required to execute the algorithm.**Space Complexity:**Â A sorting algorithm is said to be stable if the relative order of equal elements is preserved after sorting. This is important in certain applications where the original order of equal elements must be maintained.**Stability:**Â An in-place sorting algorithm is one that does not require additional memory to sort the data. This is important when the available memory is limited or when the data cannot be moved.**In-Place Sorting:**Â An adaptive sorting algorithm is one that takes advantage of pre-existing order in the data to improve performance.**Adaptivity:**

## Applications of Sorting Algorithms:

Sorting is often a crucial step in search algorithms like binary search, Ternary Search, where the data needs to be sorted before searching for a specific element.**Searching Algorithms:**Sorting data makes it easier to search, retrieve, and analyze.**Data management:**Sorting data in databases improves query performance.**Database optimization:**Sorting is used to prepare data for training machine learning models.**Machine learning:**Sorting helps in identifying patterns, trends, and outliers in datasets. It plays a vital role in statistical analysis, financial modeling, and other data-driven fields.**Data Analysis:**Sorting algorithms are used in operating systems for tasks like task scheduling, memory management, and file system organization.**Operating Systems:**

## Basics of Sorting Algorithms:

- Introduction to Sorting Techniques â€“ Data Structure and Algorithm Tutorials
- Applications, Advantages and Disadvantages of Sorting Algorithm
- What is a real life example of sorting?
- What is Sorting in DSA | Sorting meaning

## Sorting Algorithms:

- Selection Sort
- Bubble Sort
- Insertion Sort
- Merge Sort
- Quick Sort
- Heap Sort
- Counting Sort
- Radix Sort
- Bucket Sort
- Bingo Sort Algorithm
- ShellSort
- TimSort
- Comb Sort
- Pigeonhole Sort
- Cycle Sort
- Cocktail Sort
- Strand Sort
- Bitonic Sort
- Pancake sorting
- BogoSort or Permutation Sort
- Gnome Sort
- Sleep Sort â€“ The King of Laziness
- Structure Sorting in C++
- Stooge Sort
- Tag Sort (To get both sorted and original)
- Tree Sort
- Odd-Even Sort / Brick Sort
- 3-way Merge Sort

**Library Implementations:**

**Library Implementations:**

- Introsort â€“ C++â€™s Sorting Weapon
- Comparator function of qsort() in C
- sort() in C++ STL
- C qsort() vs C++ sort()
- Arrays.sort() in Java with examples
- Collections.sort() in Java with Examples

**Easy Problems on Sorting:**

**Easy Problems on Sorting:**

- Sort elements by frequency
- Sort an array of 0s, 1s and 2s
- Sort numbers stored on different machines
- Sort an array in wave form
- Check if any two intervals overlap among a given set of intervals
- How to sort an array of dates in C/C++?
- Sorting Strings using Bubble Sort
- Find missing elements of a range
- Sort an array according to count of set bits
- Sort even-placed elements in increasing and odd-placed in decreasing order
- Sort an array when two halves are sorted
- Sorting Big Integers
- Sort a linked list of 0s, 1s and 2s

**Medium Problems on Sorting:**

**Medium Problems on Sorting:**

- Inversion count in Array using Merge Sort
- Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
- Sort a nearly sorted (or K sorted) array
- Sort n numbers in range from 0 to n^2 â€“ 1 in linear time
- Sort an array according to the order defined by another array
- Find the point where maximum intervals overlap
- Find a permutation that causes worst case of Merge Sort
- Sort Vector of Pairs in ascending order in C++
- Minimum swaps to make two arrays identical
- Chocolate Distribution Problem
- Permute two arrays such that sum of every pair is greater or equal to K
- Bucket Sort To Sort an Array with Negative Numbers
- Sort a Matrix in all way increasing order
- Convert an Array to reduced form using Vector of pairs
- Smallest Difference Triplet from Three arrays
- Check if it is possible to sort an array with conditional swapping of adjacent allowed

**Hard Problems on Sorting:**

**Hard Problems on Sorting:**

- Find Surpasser Count of each element in array
- Count distinct occurrences as a subsequence
- Count minimum number of subsets (or subsequences) with consecutive numbers
- Chose k array elements such that difference of maximum and minimum is minimized
- Minimum swap required to convert binary tree to binary search tree
- K-th smallest element after removing some integers from natural numbers
- Maximum difference between frequency of two elements such that element having greater frequency is also greater
- Minimum swaps to reach permuted array with at most 2 positions left swaps allowed
- Find whether it is possible to make array elements same using one external number
- Sort an array after applying the given equation
- Print array of strings in sorted order without copying one string into another

**Quick Links :**

**Recommended:**