# Category Archives: Sorting

## 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 »

## Ways to sort list of dictionaries by values in Python – Using itemgetterJuly 31, 2017

Previous article of this segment dealt with sorting list of dictionaries by values using lambda function. Ways to sort list of dictionaries by values in… Read More »

## Merging two unsorted arrays in sorted orderJuly 29, 2017

Write a SortedMerge() function that takes two lists, each of which is unsorted, and merges the two together into one new list which is in… Read More »

## Find first k natural numbers missing in given arrayJuly 25, 2017

Given an array of size n and a number k, we need to print first k natural numbers that are not there in given array.… Read More »

## Sorting Array Elements By Frequency | Set 3 (Using STL)July 24, 2017

Given an array of integers, sort the array according to frequency of elements. If frequencies of two elements are same, print them in increasing order.… Read More »