Differential search algorithms (non-recursive)

/**
* @param data Array with search (array)
* @param target target data
* @return returns the corresponding subscript, -1 means Not found
*/
public static int binarySearch(int[] data, int target) {
int low = 0;
int high = data.length-1;
// Continue to search
while (high >= low) {
int mid = (low + high) / 2;
if (data[mid] == target) {
return mid;
// find to the left
} else if (data[mid]> target) {
high = mid-1;
// find to the right< br />} else if (data[mid] low = mid + 1;
}
}

return -1;

}

public static void main(String[] args) {
int[] data = {1,6,8,41,68,100};
System.out.println(binarySearch(data, 100));
}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5262 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.