aboutsummaryrefslogtreecommitdiffstats
path: root/src/algorithms/searching/InterpolationSearch.java
blob: 82f1d836130ea7e836d1541ce7cf2cddcf5d4b46 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
 * Copyright © 2014  Mattias Andrée (m@maandree.se)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package algorithms.searching;

import java.util.*;
import java.math.*;


/**
 * Interpolation search class. Interpolation search runs in logarithmic
 * time, in average, and linear time in worst case, which will occur with
 * rough distrubitions, constant memory, and requires the list to be sorted
 * and numerical. Interpolation search often out preforms binary search for
 * smoothing distributions. Identity search is not possible, only equality
 * search.<br>
 * The list must be sorted in ascending order.
 */
public class InterpolationSearch
{
£>for T in char byte short int long float double; do
    /**
     * Find the easiest to find occurance of an item in a list
     *
     * @param   item   The item to find
     * @param   array  The list in which to search
     * @return         The index of the easiest to find occurance of the item. The bitwise
     *                 negation of the position it insert it in is returned if it was not found.
     */
    public static int indexOf(£{T} item, £{T}[] array)
    {
	return indexOf(item, array, 0, array.length);
    }
    
    /**
     * Find the easiest to find occurance of an item in a list
     *
     * @param   item   The item to find
     * @param   array  The list in which to search
     * @param   start  The index of the first position to search in the array
     * @return         The index of the easiest to find occurance of the item. The bitwise
     *                 negation of the position it insert it in is returned if it was not found.
     */
    public static int indexOf(£{T} item, £{T}[] array, int start)
    {
	return indexOf(item, array, start, array.length);
    }
    
    /**
     * Find the easiest to find occurance of an item in a list
     *
     * @param   item   The item to find
     * @param   array  The list in which to search
     * @param   start  The index of the first position to search in the array
     * @param   end    The index after the last position to search in the array
     * @return         The index of the easiest to find occurance of the item. The bitwise
     *                 negation of the position it insert it in is returned if it was not found.
     */
    public static int indexOf(£{T} item, £{T}[] array, int start, int end)
    {
	£{T} low, high, at;
	int min = start, mid;
	int max = end - 1;
	
	for (;;)
	{
	    if ((low  = array[min]) > item)  break;
	    if ((high = array[max]) < item)  break;
            
	    if ((at = array[mid = min + (int)((item - low) * (max - min) / (high - low))]) < item)
		min = mid + 1;
	    else if (at > item)
		max = mid - 1;
	    else
		return mid;
	}
        
	return (array[min] == item) ? min : ~min;
    }
£>done
    
£>for T in BigInteger BigDecimal; do
    /**
     * Find the easiest to find occurance of an item in a list
     *
     * @param   item   The item to find
     * @param   array  The list in which to search
     * @return         The index of the easiest to find occurance of the item. The bitwise
     *                 negation of the position it insert it in is returned if it was not found.
     */
    public static int indexOf(£{T} item, £{T}[] array)
    {
	return indexOf(item, array, 0, array.length);
    }
    
    /**
     * Find the easiest to find occurance of an item in a list
     *
     * @param   item   The item to find
     * @param   array  The list in which to search
     * @param   start  The index of the first position to search in the array
     * @return         The index of the easiest to find occurance of the item. The bitwise
     *                 negation of the position it insert it in is returned if it was not found.
     */
    public static int indexOf(£{T} item, £{T}[] array, int start)
    {
	return indexOf(item, array, start, array.length);
    }
    
    /**
     * Find the easiest to find occurance of an item in a list
     *
     * @param   item   The item to find
     * @param   array  The list in which to search
     * @param   start  The index of the first position to search in the array
     * @param   end    The index after the last position to search in the array
     * @return         The index of the easiest to find occurance of the item. The bitwise
     *                 negation of the position it insert it in is returned if it was not found.
     */
    public static int indexOf(£{T} item, £{T}[] array, int start, int end)
    {
        £{T} low, high, at;
        int min = start, mid;
        int max = end - 1;
 
        for (;;)
	{
	    if ((low  = array[min]).compareTo(item) > 0)  break;
	    if ((high = array[max]).compareTo(item) < 0)  break;
            
	    mid = item.subtract(low).multiply(£{T}.valueOf(max - min)).divide(high.subtract(low)).intValue();
            
	    if ((at = array[mid += min]).compareTo(item) < 0)
		min = mid + 1;
	    else if (at.compareTo(item) > 0)
		max = mid - 1;
	    else
		return mid;
	}
        
        return (array[min] == item) ? min : ~min;
    }
£>done
}