# Category Archives: Sorting

## Difference between highest and least frequencies in an array

Given an array, find the difference between highest occurrence and least occurrence of any number in an array Examples: Input : arr[] = [7, 8,… Read More »

## Count points covered by given intervals

Consider an infinite x-y plane. Infinite people walk on the plane in upward or +ve Y direction. At each integer point on the x-axis, only… Read More »

## Sort a binary array using one traversal

Given a binary array, sort it using one traversal and no extra space. Examples: Input : 1 0 0 1 0 1 0 1 1… Read More »

## Check if given array is almost sorted (elements are at-most one position away)

Given an array with n distinct elements. An array is said to be almost sorted (non-decreasing) if any of its elements can occurs maximum of… Read More »

## Maximum area rectangle by picking four sides from array

Given an array of n positive integers that represent lengths. Find out the maximum possible area whose four sides are picked from given array. Note… Read More »

## Sort elements by frequency | Set 4 (Efficient approach using hash)

Print the elements of an array in the decreasing frequency if 2 numbers have same frequency then print the one which came first. Examples: Input… Read More »

## Dual pivot Quicksort

As we know, the single pivot quick sort takes a pivot from one of the ends of the array and partitioning the array, so that… Read More »

## How to make Mergesort to perform O(n) comparisons in best case?

As we know, Mergesort is a divide and conquer algorithm that splits the array to halves recursively until it reaches an array of the size… Read More »

## A sorting algorithm that slightly improves on selection sort

As we know, selection sort algorithm takes the minimum on every pass on the array, and place it at its correct position. The idea is… Read More »

## Sort an array according to absolute difference with a given value “using constant extra space”

Given an array of n distinct elements and a number x, arrange array elements according to the absolute difference with x, i. e., element having… Read More »

## Sorting array of strings (or words) using Trie

Given an array of strings, print them in alphabetical (dictionary) order. If there are duplicates in input array, we need to print them only once.… Read More »

## Sort a linked list of 0s, 1s and 2s by changing links

Given a linked list of 0s, 1s and 2s, sort it. Examples: Input : 2->1->2->1->1->2->0->1->0 Output : 0->0->1->1->1->1->2->2->2 Input : 2->1->0 Output : 0->1->2 Recommended:… Read More »

## Check if a grid can become row-wise and column-wise sorted after adjacent swaps

Given a grid of size n x len filled with lowercase characters. We can swap two adjacent characters in the same row and column. Now… Read More »

## Insertion sort using C++ STL

Implementation of Insertion Sort using STL functions. Pre-requisites : Insertion Sort, std::rotate, std::upper_bound, C++ Iterators. The idea is to use std::upper_bound to find an element… Read More »

## Sort first half in ascending and second half in descending order

Given an array of integers, sort the first half of the array in ascending order and second half in descending order. Examples: Input : arr[]… Read More »