Turn this Wikipedia pseudocode into C#: // initially called with low = 0, high = N-1. A is a sorted list. BinarySearch (
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Turn this Wikipedia pseudocode into C#: // initially called with low = 0, high = N-1. A is a sorted list. BinarySearch (
called when searching for a word. Print that count at the end of
each line.
************ I need to know how I can count how
many times recursive is called with each word word in the
List<string>. ***********
Here is my code currently...
Turn this Wikipedia pseudocode into C#: // initially called with low = 0, high = N-1. A is a sorted list. BinarySearch (A[0..N-1], searchTerm, low, high) { if (high < low) return -1 11 -1 means not found mid = (low + high) / 2 if (searchTerm < A[mid] ) return BinarySearch (A, searchTerm, low, mid-1) else if (searchTerm > A[mid] ) return BinarySearch (A, searchTerm, mid+1, high) else return mid //the searchTerm was found }
Index: 0 Index: 1 Index: 2 Index: 3 Index: 4 Index: 5 Index: 6 Index: 7 Index: 8 Index: 9 Index: 10 Inday: 11 Found Index: 0 Found Index: 1 Found Index: 2 Found Index: 3 Found Index: 4 Found Index: 5 Found Index: 6 Found Index: 7 Found Index: 8 Found Index: 9 Found Index: 10 Found Indey. 11 Recursive calls: 6 Recursive calls: 5 Recursive calls: 6 Recursive calls: 7 Recursive calls: 4 Recursive calls: 6 Recursive calls: 5 Recursive calls: 6 Recursive calls: 7 Recursive calls: 3 Recursive calls: 6 Recursive ralls. 6
public static int BinarySearch(List<string> sortList, string keyword, int low, int high, ref int iteration { iteration++; if (high < low) return -1; } int mid = (Low + high) / 2; int comp = keyword. CompareTo(sortList[mid]); if (comp < 0) { return BinarySearch(sortlist, keyword, low, mid – 1, ref iteration); } else if (comp > 0) { return BinarySearch(sortlist, keyword, mid + 1, high, ref iteration); } else { return mid; }
public static List<string> SearchBinaryList(List<string> list) { List<string> sortlist = Merge SortSplity(list); int iteration = 0; = for (int i = 0; i < sortlist. Count; i++) { int fid = BinarySearch(sortlist, sortList, o, i, ref iteration); Console.WriteLine(); Console.Write(sortList); Console. CursorLeft = 50; Console.Write($"Index: {i}"); Console. CursorLeft = 65; Console.Write($"Found Index: {fid}"); Console. CursorLeft = 86; Console.Write($"Recursive Calls: {iteration}"); } return sortList;