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

  Mathematics in computing

Binary search tree, and postorder and preorder traversal Determine the shortest path in Graph

  Ict governance

ICT is defined as the term of Information and communication technologies, it is diverse set of technical tools and resources used by the government agencies to communicate and produce, circulate, store, and manage all information.

  Implementation of memory management

Assignment covers the following eight topics and explore the implementation of memory management, processes and threads.

  Realize business and organizational data storage

Realize business and organizational data storage and fast access times are much more important than they have ever been. Compare and contrast magnetic tapes, magnetic disks, optical discs

  What is the protocol overhead

What are the advantages of using a compiled language over an interpreted one? Under what circumstances would you select to use an interpreted language?

  Implementation of memory management

Paper describes about memory management. How memory is used in executing programs and its critical support for applications.

  Define open and closed loop control systems

Define open and closed loop cotrol systems.Explain difference between time varying and time invariant control system wth suitable example.

  Prepare a proposal to deploy windows server

Prepare a proposal to deploy Windows Server onto an existing network based on the provided scenario.

  Security policy document project

Analyze security requirements and develop a security policy

  Write a procedure that produces independent stack objects

Write a procedure (make-stack) that produces independent stack objects, using a message-passing style, e.g.

  Define a suitable functional unit

Define a suitable functional unit for a comparative study between two different types of paint.

  Calculate yield to maturity and bond prices

Calculate yield to maturity (YTM) and bond prices

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