Category Archives: Sorting

Recursive Bubble Sort

Background : Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.

Possible to form a triangle from array values

Given an array of integers, we need to find out whether it is possible to construct at least one non-degenerate triangle using array values as its sides. In other words, we need to find out 3 such array indices which can become sides of a non-degenerate triangle. Examples: Input : [4, 1, 2] Output :… Read More »

Tag Sort (To get both sorted and original)

This is not a new sorting algorithm, but an idea when we need to avoid swapping of large objects or need to access elements of a large array in both original and sorted orders. A common sorting task is to sort elements of an array using a sorting algorithm like Quick Sort, Bubble Sort.. etc,… Read More »

Bucket Sort To Sort an Array with Negative Numbers

We have discussed bucket sort in the main post on Bucket Sort . Bucket sort is mainly useful when input is uniformly distributed over a range. For example, consider the problem of sorting a large set of floating point numbers which are in range from 0.0 to 1.0 and are uniformly distributed across the range.… Read More »

Hoare’s vs Lomuto partition scheme in QuickSort

We have discussed implementation of QuickSort using Lomuto partition scheme. Lomuto’s partition scheme is easy to implement as compare to Hoare scheme. Lomuto’s Partition Scheme partition(arr[], lo, hi) pivot = arr[hi] i = lo // place for swapping for j := lo to hi – 1 do if arr[j] <= pivot then swap arr[i] with… Read More »

Cycle Sort

Cycle sort is an in-place sorting Algorithm, unstable sorting algorithm, a comparison sort that is theoretically optimal in terms of the total number of writes to the original array. It is optimal in terms of number of memory writes. It minimizes the number of memory writes to sort (Each value is either written zero times,… Read More »

Find pair with greatest product in array

Given an array of n elements, the task is to find the greatest number such that it is product of two elements of given array. If no such element exists, print -1. Elements are within the range of 1 to 10^5. Input : arr[] = {10, 3, 5, 30, 35} Output: 30 Explanation: 30 is… Read More »