Binary Search Algorithm Causes Error - Using an Unassigned Local Variable

I followed a tutorial that showed how to create a binary search algorithm from scratch. However, I get the error “Using an unassigned local variable“ Pivot. ”I am new to this language and have tried much simpler languages ​​before.

I apologize for the lack of internal documentation and the use of white space.

The error is at the bottom of the code using "//"

Here is the program:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Binary_Search_2 { class Program { static void Main(string[] args) { int[] arr = new int[10]; Random rnd = new Random(); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(1, 10); } Array.Sort(arr); for (int i = 0; i < arr.Length; i++) { Console.Write("{0},", arr[i]); } int Start = 0; int End = arr.Length; int Center = Start + End / 2; int Pivot; while (arr[6] > 0) { while (arr[6] < arr[Center]) { End = Center; Center = (End + Start) / 2; if (Pivot == arr[Center]) { Console.WriteLine("The Index is {0}", arr[Center]); } break; } while (arr[6] > arr[Center]) { Start = Center; Center = (End + Start) / 2; if (Pivot == arr[Center]) //**This is where the error occurs.** { Console.WriteLine("The index is {0}", arr[Center]); } } } } } } 

Sorry if this is actually something really simple, but I don’t have anyone to teach me directly, and I have no ideas.

+6
source share
3 answers

The error is caused by this line:

 int Pivot; 

Pivot must be set before use in an if .

This indicates an error in the code: you check the Pivot' value in an if` statement, but you never assign it. Browse through the tutorial to find a line that looks like this:

 Pivot = ... // <<=== some expression here 

Most likely, you did not enter this line into your program when you followed the tutorial.

+3
source

Binary search explained:

A common problem is simply finding a specific item in a bunch of items. Binary search does this by cutting the "vision" in half until it finds an element, or find out that it does not exist. By checking the center of the current view, you can determine if the element is on the left or right, but for this the elements must be sorted.

An example in theory (an element exists):

Suppose we were looking for 1 in this array of elements:

 0, 1, 4, 4, 6, 7, 9, 10 

At every step, we mark our field of vision, and it looks like this:

  • S = beginning of field of view
  • E = end of field field
  • M = average field of view = (S + E + 1) / 2
 SME 0, 1, 4, 4, 6, 7, 9, 10 

The value in M ​​is 6, which is more than 1, so we know that 1 is on M on the left. Therefore, we set E as M-1 and recount M:

 SME 0, 1, 4, 4, 6, 7, 9, 10 

The value in M ​​is now 4 - even more than 1, so we go further:

  M SE 0, 1, 4, 4, 6, 7, 9, 10 

The value in M ​​is now 1, this is the value we were looking for, so we are done!

An example in theory (an element does not exist):

Suppose we were looking for 5 in this array of elements:

 0, 1, 4, 4, 6, 7, 9, 10 

At every step, we mark our field of vision, and it looks like this:

  • S = beginning of field of view
  • E = end of field field
  • M = average field of view = (S + E + 1) / 2
 SME 0, 1, 4, 4, 6, 7, 9, 10 

The value in M ​​is 6, which is more than 5, so we know that 5 is on M on the left. Therefore, we set E as M-1 and recount M:

 SME 0, 1, 4, 4, 6, 7, 9, 10 

The value in M ​​is now 4, which is less than 5, so we know that 5 must be on M on the right. Therefore, we set S as M + 1 and recount M:

  S E M 0, 1, 4, 4, 6, 7, 9, 10 

The value in M ​​is now 4, which is less than 5, so we must go further to the right, but oops! S = E means that if we move on, they will cross each other, that is, we already looked there, so the element, of course, does not exist, and the search ends.

Pseudo-code + explanations:

 arr = 0, 1, 4, 4, 6, 7, 9, 10; wanted = 5; // Holds the value to look for index = -1; // Will hold the index of the found value (-1 means not found) s = 0; // Initialize S with the beginning of all array e = arr.Length - 1; // Initialize E with the last element of the array // As long as we don't cross ourselves while (s <= e) { // Calculate M (rounded) m = (s + e + 1) / 2; // Get the current middle value curr = arr[m]; // Check to see if we found our wanted value if (curr == wanted) { index = m; break; } else if (curr < wanted) { // Wanted value is further to the right s = m + 1; } else { // Wanted value is further to the left e = m - 1; } } // Here, if index is -1, the wanted value does not exist in arr. // Otherwise, it holds it index. 

Common C # code:

 public int BinarySearch<T>(this ICollection<T> elements, T item) where T : IComparable { // Assumes that elements is already sorted! var s = 0; var e = elements.Count - 1; while (s <= e) { var m = (s + e + 1) / 2; var cmp = elements[m].CompareTo(item); switch (cmp) { case -1: s = m + 1; break; case 1: e = m - 1; break; default: return m; } } return -1; } 

Application:

 int[] nums = ...; List<double> doubles = ...; SortedSet<People> people = ...; // People compared by ID var i0 = nums.Sort().ToArray().BinarySearch(5); doubles.Sort(); var i2 = doubles.BinarySearch(5.3d); var i3 = people.BinarySearch(new People{ID = 5}); 
+2
source

You do not assign a value to the Pivot variable after the declaration. When evaluating it in an if statement, it is still not initialized.

+1
source

Source: https://habr.com/ru/post/926846/


All Articles