Skip to content
Related Articles

Related Articles

Sort in C++ Standard Template Library (STL)

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 06 Jul, 2022
Improve Article
Save Article

Sorting is one of the most basic functions applied to data. It means arranging the data in a particular fashion, which can be increasing or decreasing. There is a builtin function in C++ STL by the name of sort(). 
This function internally uses IntroSort. In more details it is implemented using hybrid of QuickSort, HeapSort and InsertionSort.By default, it uses QuickSort but if QuickSort is doing unfair partitioning and taking more than N*logN time, it switches to HeapSort and when the array size becomes really small, it switches to InsertionSort. 

The prototype for sort is : 

sort(startaddress, endaddress)

startaddress: the address of the first 
              element of the array
endaddress: the address of the next 
            contiguous location of the 
            last element of the array.
So actually sort() sorts in the 
range of [startaddress,endaddress)

C++




// C++ program to sort an array
#include <algorithm>
#include <iostream>
  
using namespace std;
  
void show(int a[], int array_size)
{
    for (int i = 0; i < array_size; ++i)
        cout << a[i] << " ";
}
  
// Driver code
int main()
{
    int a[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
    
    // size of the array
    int asize = sizeof(a) / sizeof(a[0]);
    cout << "The array before sorting is : \n";
    
    // print the array
    show(a, asize);
  
      // sort the array
    sort(a, a + asize);
  
    cout << "\n\nThe array after sorting is :\n";
    
    // print the array after sorting
    show(a, asize);
  
    return 0;
}

Output

The array before sorting is : 
1 5 8 9 6 7 3 4 2 0 

The array after sorting is :
0 1 2 3 4 5 6 7 8 9 

Time Complexity: O(nlogn)
Auxiliary Space: O(1)

Refer std::sort() for more details.

quick sort code:

C++




//quick sort code in C++
  
#include <iostream>
using namespace std;
   
int partition(int arr[], int start, int end)
{
   
    int pivot = arr[start];
   
    int count = 0;
    for (int i = start + 1; i <= end; i++) {
        if (arr[i] <= pivot)
            count++;
    }
   
    // Giving pivot element its correct position
    int pivotIndex = start + count;
    swap(arr[pivotIndex], arr[start]);
   
    // Sorting left and right parts of the pivot element
    int i = start, j = end;
   
    while (i < pivotIndex && j > pivotIndex) {
   
        while (arr[i] <= pivot) {
            i++;
        }
   
        while (arr[j] > pivot) {
            j--;
        }
   
        if (i < pivotIndex && j > pivotIndex) {
            swap(arr[i++], arr[j--]);
        }
    }
   
    return pivotIndex;
}
   
void quickSort(int arr[], int start, int end)
{
   
    // base case
    if (start >= end)
        return;
   
    // partitioning the array
    int p = partition(arr, start, end);
   
    // Sorting the left part
    quickSort(arr, start, p - 1);
   
    // Sorting the right part
    quickSort(arr, p + 1, end);
}
   
int main()
{
   
    int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
    int n = 10;
   
    quickSort(arr, 0, n - 1);
    cout<<"array after using quick sort: "<<endl;
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
   
    return 0;
}
  
//this code is contributed by Machhaliya muhammad

Output

array after using quick sort: 
0 1 2 3 4 5 6 7 8 9 

CPP-STL-Self-Paced-Course

average case Time Complexity: O(N*logN) 

worst case Time Complexity: O(N^2)
Auxiliary Space: O(1)

Heap sort code:

C++




//C++ program of heap sort
  
#include <iostream>
using namespace std;
    
// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
void heapify(int arr[], int n, int i)
{
    int largest = i; // Initialize largest as root
    int l = 2 * i + 1; // left = 2*i + 1
    int r = 2 * i + 2; // right = 2*i + 2
    
    // If left child is larger than root
    if (l < n && arr[l] > arr[largest])
        largest = l;
    
    // If right child is larger than largest so far
    if (r < n && arr[r] > arr[largest])
        largest = r;
    
    // If largest is not root
    if (largest != i) {
        swap(arr[i], arr[largest]);
    
        // Recursively heapify the affected sub-tree
        heapify(arr, n, largest);
    }
}
    
// main function to do heap sort
void heapSort(int arr[], int n)
{
    // Build heap (rearrange array)
    for (int i = n / 2 - 1; i >= 0; i--)
        heapify(arr, n, i);
    
    // One by one extract an element from heap
    for (int i = n - 1; i >= 0; i--) {
        // Move current root to end
        swap(arr[0], arr[i]);
    
        // call max heapify on the reduced heap
        heapify(arr, i, 0);
    }
}
    
/* A utility function to print array of size n */
void printArray(int arr[], int n)
{
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
    cout <<endl;
}
    
// Driver program
int main()
{
    int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
    int n = 10;
    
    heapSort(arr, n);
    
    cout << "array after using heap sort:"<<endl;
    printArray(arr, n);
}
  
//this code is contributed by Machhaliya Muhammad

Output

array after using heap sort:
0 1 2 3 4 5 6 7 8 9 

Time Complexity: O(N*logN) 
Auxiliary Space: O(1)

insertion sort code:

C++




//C++ program of insertion sort
  
#include <bits/stdc++.h>
using namespace std;
   
// Function to sort an array using
// insertion sort
void insertionSort(int arr[], int n)
{
    int i, key, j;
    for (i = 1; i < n; i++)
    {
        key = arr[i];
        j = i - 1;
   
        // Move elements of arr[0..i-1], 
        // that are greater than key, to one
        // position ahead of their
        // current position
        while (j >= 0 && arr[j] > key)
        {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }
}
   
// A utility function to print an array
// of size n
void printArray(int arr[], int n)
{
    int i;
    for (i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << endl;
}
   
// Driver code
int main()
{
    int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
    int N =10;
   
    insertionSort(arr, N);
    cout<<"array after using insertion sort:"<<endl;
    printArray(arr, N);
   
    return 0;
}
  
//This code is contributed by Machhaliya Muhammad

Output

array after using insertion sort:
0 1 2 3 4 5 6 7 8 9 

Time Complexity: O(N^2) 
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!