Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need some java help with a question for my program. I worked with a partner so I'm not 100% sure on my answer I'm mainly

Need some java help with a question for my program. I worked with a partner so I'm not 100% sure on my answer I'm mainly here to double check.

1. Describe how your firstIndexOf() method in BinarySearchDeluxe.jav finds the first index of a key that equals the search key.

Here is the part of my program that relates to the question.

public static int firstIndexOf(Key[] a, Key key, Comparator comparator) {

if (a == null || key == null || comparator == null){

throw new NullPointerException("Cannot find first key index");

}

//if not found

if (a.length == 0){

return -1;

}

//Binary Search Algorithm

int left = 0;

int right = a.length - 1;

while (left + 1 < right){

int mid = left + (right - left) / 2;

if (comparator.compare(key, a[mid]) <= 0){

right = mid;

}

else{

left = mid;

}

}

if (comparator.compare(key, a[left]) == 0){

return left;

}

if (comparator.compare(key, a[right]) == 0) {

return right;

}

return -1;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

13th Edition Global Edition

1292263350, 978-1292263359

More Books

Students also viewed these Databases questions