Algorithm Interview
12 OctoberShell sort in java
Shell sort is in place comparison based sorting algorithm. It is generalization of insertion sort. It was invented by Donald shell. It allows to sort elements which are far apart. In case of insertion sort, comparison happens between only adjacent elements but in shell sort, it avoid comparing adjacent elements until last steps. Last step of shell […]
12 OctoberQueue implementation in java
If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. In this post , we will see how to implement Queue using Array in java. Queue is abstract data type which demonstrates First in first out (FIFO) behavior. We will implement same behavior using Array. Although java provides implementation for […]
12 OctoberQuick Sort in java
If you want to practice data structure and algorithm programs, you can go through data structure and algorithm interview questions. Quick sort or partition-exchange sort, is a sorting algorithm, which is using divide and conquer algorithm. [toc] In quick sort, we first choose a pivot and divide into two sublists,one will contain elements lower than pivot […]
12 OctoberCounting Sort in java
Counting sort is special sorting technique used to sort elements between specific range. Lets say elements belong to range 1 to K , then Counting sort can be used to sort elements in O(N) times. Basic idea of counting sort to find number of elements less than X, so X can be put to its […]
12 OctoberHeap sort in java
In this post, we will see how to implement heap sort in java. I will divide heap sort in multiple parts to make it more understandable. [toc] What is heap? A heap is a tree with some special properties, so value of node should be greater than or equal to(less than or equal to in […]
10 OctoberConvert sorted Linked List to balanced BST
If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. In this post , we will see how to convert sorted LinkedList to balanced binary search tree. There are two ways to do it. Solution 1: It is very much similar to convert sorted array to BST. […]
06 OctoberFind the Contiguous Subarray with Sum to a Given Value in an array
Problem : Given an array of positive integer and given value X, find Contiguous sub array whose sum is equal to X. For example: arr[]={14, 12, 70, 15, 99, 65, 21, 90}; X =97. Sum found between index 1 to 3 Elements are 12, 17 and 15 Solution: Solution 1: Check all sub arrays and […]
06 OctoberSeparate 0s and 1s in an array
Problem : Given an array of 0’s and 1’s in random order , you need to separate 0’s and 1’s in an array. For example: arr[] = {0,1,0,0,1,1,1,0,1} Array after separating odd and even numbers : {0,0,0,0,1,1,1,1,1} Solution : Solution 1: Count number of 0’s in the array. Lets say we get X 0’s Once […]
06 Octoberfind minimum element in a sorted and rotated array
If you want to practice data structure and algorithm programs, you can go through data structure and algorithm interview questions. In this post, we will see how to find minimum element in sorted and rotated array. Problem: You are given an sorted and rotated array as below: int arr[]={16,19,21,25,3,5,8,10}; If you note that array is sorted […]