# Category Archives: Sorting

## Insertion Sort

Insertion Sort In Insertion Sort, we grow the sorted array one element at a time. The array is divided into two parts, sorted part and unsorted part, sorted part being the part we have already traversed i. e. to the left of current index. At this point, it’d be well to mention that a single number… Read More »

## Bubble Sort

Bubble Sort Bubble Sort (for ascending order) can be seen as the following steps: Algorithm for Bubble Sort: Traverse the given array n = size of array times. Compare adjacent elements of the array. If they are in the wrong order i.e. in descending order then swap them as we want the elements to be in… Read More »

## Selection Sort

Selection Sort Selection Sort (for sorting in ascending order) can be seen as the following steps: Algorithm for Selection sort: Find the smallest element in the array. Put the element at the beginning of the array. Continue the steps 1 and 2 by incrementing the starting point in each iteration. The array is divided into… Read More »

## Segregate Even and Odd numbers

Problem: Given an array of integers, we need to sort all the odd and even values, such that all odd values come before even ones. Example: a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9} Answer: 1 9 3 7 5 6 4 8 2 Solution: This post is very similar to Segregate… Read More »

## Segregate 0 and 1 in an array

Problem: Given an array of 0’s and 1’s, we need to segregate all 0’s and 1’s together such that all 0’s come first. Example: a[] = {0, 1, 0, 1, 1, 1, 0, 0} Answer 0 0 0 0 1 1 1 1 Solution: Approach 1: (Counting Method) Algo: Calculate the number of 0’s and… Read More »

## Two elements whose sum is closest to zero

Problem: Given an array(or vector), we need to find a pair whose sum is closest to 0. Example: arr[] = {1, 50, -10, 75, -72, 85} Answer: 75 and -72 (sum =3) Solution: Approach 1: Algo: Initialise ANS to large value. Store indices of the two numbers. For every Element in the array, find its sum with… Read More »