# Address Calculation Sort using Hashing

In this sorting algorithm, Hash Function f is used with the property of Order Preserving Function which states that if .

Hash Function:

f(x) = floor( (x/maximum) * SIZE )
where maximum => maximum value in the array,
SIZE => size of the address table (10 in our case),
floor => floor function

This algorithm uses an address table to store the values which is simply a list (or array) of Linked lists. The Hash function is applied to each value in the array to find its corresponding address in the address table. Then the values are inserted at their corresponding addresses in a sorted manner by comparing them to the values which are already present at that address.

Examples:

Input : arr = [29, 23, 14, 5, 15, 10, 3, 18, 1]
Output:
After inserting all the values in the address table, the address table looks like this:

ADDRESS 0: 1 --> 3
ADDRESS 4: 14 --> 15

The below figure shows the representation of the address table for the example discussed above:

After insertion, the values at each address in the address table are sorted. Hence we iterate through each address one by one and insert the values at that address in the input array.

Below is the implementation of the above approach

Output:

Input array: 29 23 14 5 15 10 3 18 1

ADDRESS 0: 1 3
ADDRESS 4: 14 15

Sorted array: 1 3 5 10 14 15 18 23 29

Time Complexity:
The time complexity of this algorithm is in the best case. This happens when the values in the array are uniformly distributed within a specific range.

Whereas the worst case time complexity is . This happens when most of the values occupy 1 or 2 addresses because then significant work is required to insert each value at its proper place.

My Personal Notes arrow_drop_up

I like solving puzzles

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.