< Nafees />
github
  • Assignments - DSA
  • Long Integer Operations
  • OOP
    • Introduction
    • Implementation
  • Sorting Algorithms
    • Selection Sort
    • Bubble Sort
    • Insertion Sort
    • Shell Sort
    • Shuffle
    • Merge Sort
    • Convex Hull
    • Quick sort
    • System Sort
    • Heap-Sort
  • Binary Search
  • Binary Search By Recursion
  • Two-Pointer
  • String
  • LeetCode 75
    • Array/String
    • Hash Map
    • BST
    • Binary Search
  • Top Interview 150
    • Linked-List
  • Leetcode Programming Skill
    • Math
  • Leetcode 75
  • 🤡Leet Code Extra
  • Arrays
    • 1-D Array
    • 2-D Arrays
  • 👨‍🍳Basic Math - Codechef
  • ☠️Recursion
  • 😁Public Member Functions
    • Strings Functions
  • 👾Linked List
    • What's Linked List & Implementation?
      • Singly Linked List
      • Doubly Linked List
    • Problems
  • 📚Stack
    • What's Stack & Implementation?
      • Stack Using Array
      • Stack using LL
    • Problems
  • 🏧Queue
    • What's Queue & Implementation?
      • Simple Queue
      • Queue using LL
      • Circular Queue
      • Deque using Linked List
      • STL Deque
    • Problems
  • 🏧Priority Queue
    • What's Priority Queue & Implementation
      • OrderedArrayMaxPQ.Java
      • Maximum-Oriented PQ using Binary Heap
      • Minimum-Oriented PQ using Binary Heap
    • Problems
  • 🗓️Hash Table
    • What's Hash Table & Implementation
      • ST - Seperate Chaining
      • ST - Linear Probing
    • Problems
  • 🎴Symbol Table
    • What's Symbol Table and implementation
      • ST Using Binary search (ordered array)
      • ST Using Binary Search Tree
      • ST Using Left-Leaning Red-Black Tree
      • ST Using Hash Table
    • Problems
  • 🔗Union-Find (Dynamic Connectivity problem)
    • What is a Union Find Data Structure?
    • Implementation
  • 🎋Binary Tree
    • What's Binary Tree & Implementation?
      • Traversal
      • Red-Black BST
  • 🌴Trie
    • What's Trie & Implementation?
    • Problems
  • 😎Project
    • Expression Evaluation
Powered by GitBook
On this page

Was this helpful?

Sorting Algorithms

Sorting algorithms are used to rearrange the elements of an array in a specific order. In C++, many sorting algorithms are available, each with its advantages and disadvantages.

PreviousImplementationNextSelection Sort

Last updated 10 months ago

Was this helpful?

Some of the most common sorting algorithms in C++ include:

  • Bubble sort: This simple sorting algorithm works by repeatedly comparing adjacent elements in an array and swapping them if they are in the wrong order. Bubble sort is not very efficient, but it is easy to implement.

  • Selection sort: This sorting algorithm works by repeatedly finding the smallest element in an array and swapping it with the first element. Selection sort is more efficient than bubble sort, but it is still not very efficient for large arrays.

  • Insertion sort: This sorting algorithm works by repeatedly inserting elements into their correct position in an array. Insertion sort is more efficient than bubble sort and selection sort, but it is still not very efficient for large arrays.

  • Merge sort: This sorting algorithm works by recursively splitting an array in half and merging the two halves back together in sorted order. Merge sort is very efficient for large arrays, but it is more complex to implement than the other sorting algorithms mentioned above.

  • Quick sort: This sorting algorithm works by choosing a pivot element in an array and partitioning the array around the pivot element. Quick sort is very efficient for large arrays, and it is also relatively easy to implement.

The best sorting algorithm to use in a particular situation depends on the size of the array and the desired level of efficiency. For small arrays, bubble sort or selection sort may be sufficient. For large arrays, merge sort or quick sort may be a better choice.

Here is a table that summarizes the time complexity of the sorting algorithms mentioned above:

Algorithm
Best case(Already sorted array )
Worst case
Average case(Reversed array)

O(n)

O(n^2)

O(n^2)

O(n^2)

O(n^2)

O(n^2)

O(n)

O(n^2)

O(n^2)

O(n log n)

O(n log n)

O(n log n)

O(n log n)

O(n^2)

O(n log n)

Bubble sort
Selection sort
Insertion sort
Merge sort
Quick sort
4MB
ElementarySorts.pdf
pdf
Page cover image