Permalink
Browse files

Fix Interpolation Search

  • Loading branch information...
floyernick committed Apr 4, 2018
1 parent 560cb12 commit 7da6d7312be40eb56a0756f290f515c3580859ed
Showing with 5 additions and 12 deletions.
  1. +5 −12 InterpolationSearch/InterpolationSearch.go
@@ -3,23 +3,16 @@ package InterpolationSearch
func InterpolationSearch(array []int, number int) int {
minIndex := 0
maxIndex := len(array) - 1
for minIndex < maxIndex {
midIndex := minIndex + ((maxIndex - minIndex) / (array[maxIndex] - array[minIndex]) * (number - array[minIndex]))
for minIndex <= maxIndex && number >= array[minIndex] && number <= array[maxIndex] {
midIndex := minIndex + (number-array[minIndex])*(maxIndex-minIndex)/(array[maxIndex]-array[minIndex])
midItem := array[midIndex]
if number == midItem {
if midItem == number {
return midIndex
}
if midItem < number {
} else if midItem < number {
minIndex = midIndex + 1
} else if midItem > number {
maxIndex = midIndex - 1
}
}
if array[minIndex] == number {
return minIndex
} else if array[maxIndex] == number {
return maxIndex
} else {
return -1
}
return -1
}

0 comments on commit 7da6d73

Please sign in to comment.