Perform binary search and should return the index

Assignment Help Computer Engineering
Reference no: EM132207558

Write a program that

a. Calls a method that takes two parameters, an integer and an integer array. The call should perform binary search and should return the index of the integer in the array if found and should return -(insertionPoint+ 1) if the number is not found. Print out the index in the main method.

b. Calls a second method that performs binary search recursively. It should take 4 parameters: 3 integers and an array of integers. It should return the index of the element if found, if not then it should return -1.

Part A JAVA Code:

import java. util.*;
public class BinarySearch {
public static void insertionSort( int arr[] ) {
int n = arr.length;
int i, j, temp;
for (i = 1; i < n; i++) {
j = i;
temp = arr[i];
while (j > 0 && temp < arr[j - 1]) {
arr[j] = arr[j - 1];
j = j - 1;
}
arr[j] = temp;
}
}
public static int binarySearch(int x, int[] arr) {

int first, last, middle;
first = 1;
last = arr.length - 1 ;
boolean flag = true;
middle = (first + last) / 2;
for (int i = 1; i < arr.length; i++) {
while (first <= last && flag == true) {
if (arr[middle] < x) {
first = middle + 1;
} else if (arr[middle] == x) {
first = 1;
last = arr.length - 1;
break;
} else {
last = middle - 1;
}
middle = (first + last) / 2;
}
}
if (first > last) {
System.out.println(x + " is not present in the list.");
flag = false;
return last + 2;
} else {
System.out.println(x + " found at location " + (middle + 1) + ".");
return middle + 1;
}
}

public static void main(String[] args) {
Scanner scan = new Scanner( System.in );
System.out.println("Insertion Sort Test");

int n, i, x;
System.out.print("Enter number of integer elements: ");
n = scan.nextInt();
int arr[] = new int[n];
System.out.println("Enter "+ n +" integer elements: ");
for (i = 0; i < n; i++)
arr[i] = scan.nextInt();
System.out.println("Elements before sorting: ");
for (i = 0; i < n; i++)
System.out.print(arr[i]+" ");
System.out.println();

insertionSort(arr);
System.out.println("Elements after sorting: ");
for (i = 0; i < n; i++)
System.out.print(arr[i]+" ");
System.out.println();
System.out.println("Enter the search element: ");
x = scan.nextInt();
int y = binarySearch(x,arr);
System.out.println("Index of the search integer: " +y);
}
}

Reference no: EM132207558

Questions Cloud

Back pain using their proprietary myofascial therapy : A specialty pain clinic treats patients with severe chronic back pain using their proprietary Myofascial Therapy
Explain diference between the mpc and the multiplier : Explain the diference between the MPC and the multiplier. Actual investment and planned investment. The response must be typed.
Write a procedure in assembly to read a decimal number : Write a procedure in assembly language to read a decimal number and convert it to a binary equivalent. The program should print the binary number.
Derive corresponding total cost and variable cost curves : Consider the short run total product of labor diagram on the board. Derive (show) the corresponding total cost, and variable cost curves.
Perform binary search and should return the index : The call should perform binary search and should return the index of the integer in the array if found and should return.
Which may include journal articles from academic journals : You should use 2 credible sources, which may include journal articles from Academic Journals. You can access these through the Rasmussen Online Library database
Provide explanation that distinguishes information from data : Provide explanation that distinguishes information from data. Provide detail explanation about how data is made analytics ready and why this is important.
Categories of waste for different types of organizations : Give examples of the four types of waste and the seven categories of waste for different types of organizations.
Do you think monetary policy is more effective : After you respond to this question I would like you to go back and consider fiscal policy. Do you think monetary policy is more effective than fiscal policy.

Reviews

Write a Review

Computer Engineering Questions & Answers

  Draw the unsorted list containing the given strings

Draw the unsorted list containing the following strings: blue, black, green, yellow, red, purple, white, and violet.

  Give regular expression for the given statements

All lines that contain the word ‘index' followed by any odd number/digit followed by any even number/digit (ignore 0's).

  How many cycles does this loop take to execute

Assume that the branch is handled by flushing the pipeline. If all memory references take 1 cycle, how many cycles does this loop take to execute?

  What does one need to know about the element data

What does one need to know about the element data type in order for a bstree data type to be possible.

  Why keys usually partitioned first by significant position

In radix sort, why are the keys usually partitioned first by the least significant position, not the most significant?

  Discuss now considering expanding internationally

A merged company continues to grow. More stores have been added and Internet sales are growing. The company is now considering expanding internationally

  Create an integer array of size 10 using malloc

Create an integer array of size 10 using malloc and populate it with random numbers between 500 & 1000.

  Define individual project deliverable length

Juan reached the end of his online course program. His family was so proud of him. Juan's wife wanted to throw a party to celebrate Juan's online graduation together with all of his family, and started planning the special event.

  Create research proposal to present to a fictitious company

Take a chapter from the course textbook, IT Strategy Issues and Practice, to focus on for your Assignment. You will create a research proposal to present.

  Program to print the input provided by the user

Write down a program whose main function is merely a collection of the variable declarations and the function calls.

  Convert the matrix from the csv file into a uint

Read in the csv file and store the resulting matrix in a variable. Convert the matrix from the csv file into a uint8.

  What are the maximum and minimum real numbers can be

What are the maximum and minimum real numbers can be represented by IEEE 754 double precision number?

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd