Quick Sort

QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways. Always pick first element as pivot. Always pick last element as pivot (implemented below) Pick a random element as pivot. …

TreeMap in Java

Introduction The TreeMap in Java is used to implement Map interface and NavigableMap along with the AbstractMap Class. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. This proves to be an efficient way of sorting and …

Edit Distance

The edit distance algorithm is very popular among the data scientists. It’s one of the basic algorithms used for evaluation of machine translation and speech recognition. The naive approach would be to check for all possible edit sequences and choose the shortest one in-between. That would result in an exponential complexity and it’s an overkill …

Long VS Integer

Long is the Object form of long, and Integer is the object form of int. Integer is a signed 32 bit integer type Denoted as Int Size = 32 bits (4byte) Can hold integers of range -2,147,483,648 to 2,147,483,647 default value is 0 Long is a signed 64 bit integer type Denoted as Long Size …

How to avoid Integer Overflow when comparing two node valued in BST?

For example, if here are one BST as: [Integer.MIN_VALUE, Integer.MIN_VALUE] If I want to compare if the left child node is smaller or equal to parent nod – 1, the value gonna be overflow. In this case, don’t use Integer.MIN_VALUE, use null as the default instead. Here is an example about using null as comparison …

How to sort 2D array in Java

My code : Arrays.sort(intervals, new Comparator<int[]>(){ @Override public int compare( int[] a, int[] b ){ return a[0]==b[0] ? a[1] – b[1] : a[0] – b[0]; // return Integer.compare( a[0], b[0] ); } }); And here is an simple way: Arrays.sort(myArr, (a, b) -> a[0] – b[0]);