Prog460a.java - Hunter Hildebran Anson High School Prog...

Info icon This preview shows pages 1–2. Sign up to view the full content.

/** * Hunter Hildebran * 3-9-18 * Anson High School * * Prog Desc: This program implements an iterative * binary search of a sorted array. * * Difficulties: * It took me a while to figure out how * not to do it recursively. * * What I Learned: * I learned about sorting algorithms * and how to implement them iteratively. * */ import java.util.*; import java.io.*; class Prog460a { public static int midpoint(int num1, int num2) { return (num1 + num2) / 2;//integral midpoint } public static int binarySearch(int[] array, int target) { int imin = 0;//the beginning int imax = array.length - 1;//the end int imid = midpoint(imin, imax);//roughly the middle while (imin < imax) //until range is 0 and we've found the number { if (array[imid] == target) //we've found our number { return imid;//return index } else if (array[imid] < target)//need to look in top half of selection { imin = imid + 1;//change selection to top half of array } else if (array[imid] > target)//need to look in bottom half of selection { imax = imid - 1;//reset range to lower half } } return -1; //if key not found return bad value } public static void main(String[] args) {
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern