Posts

Showing posts from September, 2017

Hashed data structures

Image
Introduction Common way of indexing data is by using hash maps which we use to map keys to values. The indexing of keys to values is built by using a hash function to compute which bucket or location our data will be stored in. If two pieces of data are hashed to the same location, it is called "collision". The Hash Function A hash function is basically a function that takes arbitrary data and returns a fixed length output. We usually call the output of a hash function as hash code. It is one of the most widely used data structures in modern programming. Properties of a hash function : 1. Deterministic :- A given input always generates the same output 2. Uniform :- The output should be probable in a given range  public long DJBHash(String str)    {       long hash = 5381;       for(int i = 0; i < str.length(); i++)       {          hash = ((hash << 5) + hash) + str.charAt(i);       }       return hash;    } Perfect Hashing

Sorting - Merge Sort

Image
Merge sort is a sorting technique based on divide and conquer technique. With worst-case time complexity being Ο(n log n), it is one of the most respected algorithms. Merge sort first divides the array into equal halves and then combines them in a sorted manner. How Merge Sort Works? To understand merge sort, we take an unsorted array as the following − We know that merge sort first divides the whole array iteratively into equal halves unless the atomic values are achieved. We see here that an array of 8 items is divided into two arrays of size 4. This does not change the sequence of appearance of items in the original. Now we divide these two arrays into halves. We further divide these arrays and we achieve atomic value which can no more be divided. Now, we combine them in exactly the same manner as they were broken down. Please note the color codes given to these lists. We first compare the element for each list and then combine them into another list in a sorte