Skip to content

Commit dcb6597

Browse files
committed
Iterpolate Search
COnvert Array to sawtooth.
1 parent 24cf5bf commit dcb6597

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/chapter10sorting/ConvertArraytoSawToothWaveLinearTime.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
package chapter10sorting;
1616

17-
import java.util.Arrays;
18-
1917
public class ConvertArraytoSawToothWaveLinearTime {
2018
public void convertArraytoSawToothWaveLinearTime(int A[]){
2119
// Traverse all even elements
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*Copyright (c) Dec 12, 2015 CareerMonk Publications and others.
2+
* E-Mail : info@careermonk.com
3+
* Creation Date : 2015-01-10 06:15:46
4+
* Last modification : 2006-05-31
5+
by : Narasimha Karumanchi
6+
* File Name : InterpolationSearch.java
7+
* Book Title : Data Structures And Algorithms Made In Java
8+
* Warranty : This software is provided "as is" without any
9+
* warranty; without even the implied warranty of
10+
* merchantability or fitness for a particular purpose.
11+
*
12+
*/
13+
14+
15+
package chapter11searching;
16+
17+
public class InterpolationSearch {
18+
public static int interpolationSearch(int[] A, int data){
19+
int low = 0, high = A.length - 1, mid;
20+
while (A[low] <= data && A[high] >= data){
21+
if (A[high] - A[low] == 0)
22+
return (low + high)/2;
23+
/** out of range is possible here **/
24+
mid = low + ((data - A[low]) * (high - low)) / (A[high] - A[low]);
25+
26+
if (A[mid] < data)
27+
low = mid + 1;
28+
else if (A[mid] > data)
29+
high = mid - 1;
30+
else
31+
return mid;
32+
}
33+
if (A[low] == data)
34+
return low;
35+
/** not found **/
36+
else
37+
return -1;
38+
}
39+
public static void main(String args[]){
40+
int A[] = {-30,-16,-9,3,10,11,18,22,54,84,105};
41+
for(int i=0; i < A.length; i++){
42+
System.out.print(A[i] + " ");
43+
}
44+
System.out.println();
45+
int data = -9;
46+
int result = InterpolationSearch.interpolationSearch(A, data);
47+
48+
if (result == -1)
49+
System.out.println("\n"+ data +" element not found");
50+
else
51+
System.out.println("\n"+ data +" elemnt found at position "+ result);
52+
}
53+
}

0 commit comments

Comments
 (0)