I have received several comments asking for a clarification and elaboration on how modsort works.

The general concept behind mod sort is that it inserts elements into their sorted location. How does it do this? It creates a list equal to the input size. Next it finds the max and min in order to determine how big the intervals between the max and min are. Because there are only n-2 elements left after finding min and max and fixing them at the beginning and end of the array respectively, the interval size is (max-min)/(n-2). This interval size corresponds to the range that each array element in this new list represents. Say we have min = 0, max = 100 and n = 12. Then the interval size would be (100-0)/(12-2) = 10. So the first array element would represent the numbers 1-10, the second 11-20, and so on until 100.

Once we have the max min and intervals set up, we can begin to insert the other elements (not max and min) into our data structure. We index each element by subtracting the minimum value from it first, and then dividing it by the interval size. Until two elements map to the same index, we continue to insert elements into their sorted locations.

If a conflict occurs then we take one of two steps. Either the elements around the conflict are null and we can reindex the element we are trying to place into the array to a different index in the same array as the conflict element or the elements around the conflict are occupied and we must create a sublist or subarray.

It is easy to see that reindexing does nothing to the data structure as the new element is inserted into an existing array. However, if we must create a new subarray then there are several considerations we must keep in mind.

Either we must create a subarray because

The new value is less than the conflict and the conflict has no available neighbors The new value is greater than the conflict and the conflict has no available neighbors

In the first case, lets say we have …10, 28, 39,… at the current array we are examining. Then we insert 38. We must make sure that when we create the subarray, it has enough spaces to fit all of the remaining elements, therefore we make it of this size. Next we reindex the new element, 38, into the new subarray, namely, that of 39.

In the second case, lets say we have the same situation as before only that now we insert 41. At this point we do not need to create a subarray of the size of the remaining elements. We simply create an array of size one with the value of the conflict element, in this case 39. Then we make this array be the subarray of 41, which we locate where 39 used to be.

When a sublist is needed, the element containing the conflict location either has or does not have a sublist itself. Because only one pointer is created with every addition, this is not an issue in terms of record keeping.

If we take the case in which there is a subarray chain in which every chain contains one element then it is true that this chain itself is of size O(n). However, there can only be one such chain because it contains all of the elements of the list. Moreover, reading it is O(1) because we keep a pointer to this element. Similarly, if there is a chain that has 2 elements then it still takes O(n) time to traverse it because only two elements must be examined to read the entire subarray and again these are indexed. Once we enter such a chain where there are three elements at every stage, the rate at which every stage decreases is at least 3 (it could be less if there are other insertions at different indexes and subarrays) and the odds that such a chain will exist are perhaps almost insignificant.

At the end of the creation of the structure, we traverse the structure using a type of depth first search. The worst possible part of this algorithm is the fact that it must sometimes examine a list with null elements. However, the amount of such elements it must examine is rather small. Hopefully. This is where I am not entirely sure.