Console.Write ("Please enter the length of the array:"); int k = Int. Parse (Console.ReadLine ()); int[] arr1 = new Int[k]; Console.Write ("array contains values in sequence:"); for (int i = 0; i < K; i++)//array Assignment {Random rd = new random (); int j = Rd. Next (100); Thread.Sleep (200); Arr1[i] = j; Console.Write (j + ""); } int s; Console.Write ("\ n the number you are looking for:"); try {s = Convert.ToInt32 (Console.ReadLine ()); } catch {Console.WriteLine ("You entered the wrong!)"; Press ENTER to exit "); Console.ReadLine (); Return } Console.WriteLine ("Array from small to large:"); Array sort for (int i = k-1, i > 0; i--) {for (int j = 0; J <= K-2; j + +) {if (Arr1[j] > arr1[j + 1]) { int m = arr1[j]; ARR1[J] = arr1[j + 1]; Arr1[j + 1] = m; }}} for (int i = 0; I <= k-1; i++) {Console.Write (AR R1[i] + ""); } int a = 0, B = k-1; while (arr1[(A + B)/2]! = s)//dichotomy {if (arr1[a] = = S | | arr1[b] = = S | | b-a = = 1) Break while (arr1[(A + B)/2] > s) {if (arr1[b] = = s) break; B = (A + b)/2; } while (arr1[(A + B)/2] < s) {if (arr1[a] = = S | | b-a = = 1)//When input Arrays do not contain numbers, using b-a to judge can jump out of the loop break; A = (A + b)/2; }} if (arr1[(A + B)/2] = = s) console.write ("\ nthe number you are looking for is an array of" + ((A + B)/2 + 1) + "bit ! "); else if (arr1[a] = = s) console.write ("\ nthe number you are looking for is the" + "+ (A + 1) +" bit of the array!) "); else if (arr1[b] = = s) console.write ("\ nthe number you are looking for is the" + "+ (b + 1) +" bit of the array!) "); else Console.Write ("\ n the number you entered does not exist!) "); Console.ReadLine ();
After-class homework 07--binary method find algorithm code