After-class homework 07--binary method find algorithm code

Source: Internet
Author: User
Tags array sort

            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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.