19204

C# Binary Search [closed]

Question:

public int BinarySearch(int x) { //if (Attendees.Length == 0) // return -1; int mid = (Count/ 2) -1; Student cur = new Student(x); while (cur.CompareTo(Attendees[mid]) != 0) { int sCount = Count; if (cur.CompareTo(Attendees[mid]) < 0) { int NCount = sCount / 2; mid = NCount / 2 - 1; } if (cur.CompareTo(Attendees[mid]) > 0) { int Start = mid +1; mid = (Start + sCount) / 2; } else break; cur = Attendees[mid]; } if (cur.CompareTo(Attendees[x]) == 0) return mid; else return -1; }

Can anyone help me find out why my binary search isn't working? I'm very new to programming so any help would be much appreciated. Thank you.

Answer1:

I think you didn't really grasp what binary search is about. In your code you are searching at which position element x is in an array - guess what? It's at position x!

What binary search is about is to find out the index of an element. Soooo you need to search for a given Student:

public int BinarySearch(Student student) { // Search the entire range int min = 0; int max = Attendees.Length; int mid = 0; do { // Find the pivot element mid = min + ((max - min) / 2); // Compare int comparison = student.CompareTo(Attendees[mid]); if (comparison < 0) { max = mid; } if (comparison > 0) { min = mid; } } while (min < max && comparison != 0); if (comparison != 0) return -1; return mid; }

This code may not work 100% as I haven't tried it and wrote it off my head, but it will point you in the right direction. Now, use the debugger on this code and single step through it to see whether it works as expected.

Recommend

  • select TOP n Rows from table where n is in another table?
  • C++ phrase counter with getche() function
  • How to use hardware H.264 encoder in Windows Media Foundation
  • some bugs appeared when combining PullToRefresh and SwipeListView library
  • Painting on JFrame without extending
  • In C. the name of an array is an pointer to it's first element, isn't it?
  • What does “+=” (plus equals) mean in Ruby? [closed]
  • Backward compatibility of Python 3.5 for external modules
  • How to populate html table with info from list in django
  • Prevent page break in text block with iText, XMLWorker
  • Shouldn't else be indented in the below code
  • git add error : “fatal : malloc, out of memory”
  • Hibernate to update table schema
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • Abort upload large uploads after reading headers
  • Google Custom Search with transparent background
  • Extracting HTML between tags
  • Insert into database using onclick function
  • What is Eclipse's Declaration View used for?
  • javaw.exe and eclipse startup problems
  • Can I make an Android app that runs a web view in Chrome 39?
  • How do you troubleshoot character encoding problems?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Benchmarking RAM performance - UWP and C#
  • python regex in pyparsing
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Android Google Maps API OnLocationChanged only called once
  • Qt: Run a script BEFORE make
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?