Insertion Sort Algorithm

Insertion sort is a simple sorting algorithm which work mechanism in which sorted array is built having one item at a time.  The array elements are compared with each other sequentially Upon each iteration, an item is taken from the list and inserted into the correct position by comparison with next elements of an array. This process is repeated until we reach the last item … Continue reading Insertion Sort Algorithm

Selection Sort Algorithm

Selection sort is a simple sorting algorithm. Divide the array into two arrays, sorted and unsorted. Then   selects the smallest element from an unsorted list in each iteration and places that element at the sorted array one by one. This process continues moving unsorted array boundary by one element to the sorted array. Let’s consider an array with values { 12, 4, 45, 2, … Continue reading Selection Sort Algorithm

Quick Sort Algorithm

Quicksort is an algorithm use divide and conquer approach in which the array is split into subarrays by selecting value from array as pivot and then the sub-arrays are recursively called to sort the elements. Steps for quick sort are 1. Pick an element from an array called a pivot. (Different versions of Quicksort pick pivot in different ways) 2. Partitioning- Divide the array into two … Continue reading Quick Sort Algorithm

Bubble Sort algorithm

Bubble Sort is the simplest sorting algorithm that works by iterating through the array from the first index to the last index and comparing adjacent elements and then swapping them if they appear in the wrong order. I.e. If the next element is smaller than the current element, they are swapped.  As it iterates for all the array elements with repeatedly swapping the adjacent elements. … Continue reading Bubble Sort algorithm

Sock Merchant – HackerRank Problem Solution

Question: John works at a clothing store. He has a large pile of socks that he must pair by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. For example, there are n=7  socks  with colors  ar = [1,2,1,2,1,3,2]. There is one pair of color 1 and one of color 2 . There … Continue reading Sock Merchant – HackerRank Problem Solution