-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
147 lines (129 loc) · 3.95 KB
/
Main.java
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
package de.algo;
public class Main {
/* START OF TESTING */
public static int[] emptyArray = {}; // empty
public static int[] singletonArray = {1}; // = (1)
public static int[] generalArray = new int[333];
public static void setUpGeneralArray() {
for (int i=0; i<generalArray.length; i++) {
generalArray[i] = 3*i;
}
}
public static void searchInArray(int[] array, int key) {
int linearResult = linearSearch(array, key);
int binaryResult = binarySearch(array, key);
int interpolResult = interpolationSearch(array, key);
if (linearResult != -1) {
System.out.println("Linear search: " + key + " found in possition " + linearResult);
}
else {
System.out.println("Linear search: " + key + " not found in array");
}
if (binaryResult != -1) {
System.out.println("Binary search: " + key + " found in possition " + binaryResult);
}
else {
System.out.println("Binary search: " + key + " not found in array");
}
if (interpolResult != -1) {
System.out.println("Interpolation search: " + key + " found in possition " + interpolResult);
}
else {
System.out.println("Interpolation search: " + key + " not found in array");
}
System.out.println();
}
public static void main(String[] args) {
setUpGeneralArray();
// the keys to search
int[] keys = {-10000, -1000, -333, -100, -2, -1, 0, 1, 2, 100, 333, 1000, 10000};
// test all keys
for (int i=0; i < keys.length; i++) {
System.out.println("Searching for key " + keys[i] + " in the first array:");
searchInArray(emptyArray, keys[i]);
System.out.println("Searching for key " + keys[i] + " in the second array:");
searchInArray(singletonArray, keys[i]);
System.out.println("Searching for key " + keys[i] + " in the third array:");
searchInArray(generalArray, keys[i]);
System.out.println();
}
}
/* END OF TESTING */
/**
* Implementation of linear search. Return the index of key in the array.
* Return -1 if key is not contained in the array.
* @param array
* @param key
* @return
*/
public static int linearSearch(int[] array, int key) {
for (int i = 0; i < array.length; i++) {
if (array[i] == key) {
return i;
}
}
return -1;
}
/**
* Implementation of binary search. Return the index of key in the array.
* Return -1 if key is not contained in the array.
* @param array
* @param key
* @return
*/
public static int binarySearch(int[] array, int key) {
int low = 0;
int high = array.length - 1;
while (low <= high) {
// key is in array[low ... high] or not in array at all
int middle = low + (high - low) / 2;
if (key < array[middle]) {
high = middle - 1;
} else if (key > array[middle]) {
low = middle + 1;
} else {
return middle;
}
}
return -1;
}
/**
* Implementation of interpolation search. Return the index of key in the array.
* Return -1 if key is not contained in the array.
* @param array
* @param key
* @return
*/
public static int interpolationSearch(int[] array, int key) {
if (isTrivial(array)) {
if (array.length == 0) return -1; // brake if empty arr
if (array.length == 1) {
if (key == array[0]) {
return 0;
}
} else {
return -1;
}
} else {
int low = 0;
int high = array.length - 1;
while (array[low] != array[high] && array[low] <= key && array[high] >= key) {
final int middle = (((key - array[low]) * (high - low)) / (array[high] - array[low])) + low;
if (key < array[middle]) {
high = middle - 1;
} else if (key > array[middle]) {
low = middle + 1;
} else {
return middle;
}
}
if(array[low] == key) {
return low;
}
}
return -1;
}
public static boolean isTrivial(int[] array) {
return array.length < 2;
}
}