# Category Archives: Sorting

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

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 arraySeptember 20, 2017

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 QuicksortSeptember 19, 2017

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?September 17, 2017

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 sortSeptember 15, 2017

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”September 10, 2017

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 linksSeptember 9, 2017

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 swapsSeptember 6, 2017

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++ STLAugust 30, 2017

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 orderAugust 29, 2017

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 »

## Rearrange an array in order – smallest, largest, 2nd smallest, 2nd largest, ..August 26, 2017

Given an array of integers, task is to print the array in the order – smallest number, Largest number, 2nd smallest number, 2nd largest number,… Read More »

## Smallest element in an array that is repeated exactly ‘k’ times.August 23, 2017

Given an array of size n, the goal is to find out the smallest number that is repeated exactly ‘k’ times where k > 0?… Read More »

## Minimum number of distinct elements after removing m itemsAugust 6, 2017

Given an array of items, an i-th index element denotes the item id’s and given a number m, the task is to remove m elements… Read More »