"Basic algorithm" bubble sort + binary search method

Source: Internet
Author: User

  1, Study questions

(1) There are 12 balls, 1 are 9 grams, the rest are 10 grams, how to weigh three times on the balance to find the quality of the small ball of 9 grams?

A: For the first time: divide 12 balls into two halves, half 6, on the balance, take the side of small quality;

The second time: divide 6 balls into two halves, half three, also take the small side of quality;

The third time: take three balls in any one or two to weigh, if the quality is equal, then the one that is not called is the smallest quality (9 grams), if the two masses do not want to wait, take the quality of a small one can also be taken.

(2) There are 1024 people, there is a person blood virus, and 999 people do not have a virus, test a person's blood needs 10 minutes, how to use the shortest time to find the person with blood virus?

A: The normal way, if everyone checks, it takes up to 10,000 minutes for the time to be spoken, it takes a few days.

We do this, 1024 people divided into two groups, each one to draw a blood mix, look at a group of viruses and then divided into two halves, until measured, this method only need to 2x=1024,x=10, only 10 times, 100 minutes

(3) There are 1 to 100 of the number, randomly looking for 1 to 100 of the number, how to find in the shortest time?

A: Divide 1 to 100 into two halves, the middle number and the random number comparison, if the random number is greater than the middle number, select greater than the half and then divided into two halves of the search, and the random number comparison, kept divided into two halves to take the intermediate comparison value until found;

  2. Find by dichotomy

The above study questions is the principle of using the two-point method to find.

Pseudo-code of the dichotomy:

             The input f (x) definition             inputs A and B repeat for the initial interval             as follows:             m: = (A + b)/2             if f (m) * F (a) < 0             B: = m             otherwise             a: = M
    until satisfied

  

  3. Bubble sort

The binary lookup must be for an ordered array, and if it is not ordered, we will sort the arrays. Bubble Sort algorithm:

voidBubble_sort (intArr[],intlength) {     for(inti =0; I < length-1; i++)    {         for(intj =0; J < Length-1I J + +)        {            if(Arr[j]>arr[j +1])            {                //Exchange two numbersARR[J] = Arr[j] + arr[j +1]; Arr[j+1] = Arr[j]-arr[j +1]; ARR[J]= Arr[j]-arr[j +1]; }        }    }}

 4. Find by dichotomy

//binary sort while loop implementationintDichotomy1 (intArr[],intLengthintnum) {    intHead =0; intFoot = Length-1; intMiddle = (head + foot)/2;  while(Head <=foot) {        if(Arr[middle] =num) {            returnMiddle;  Break; }        Else if(Arr[middle] <num) {Head= middle-1; Middle= (head + foot)/2; }        Else if(Arr[middle] >num) {Foot= Middle+1; Middle= (head + foot)/2; }    }    return-1;}//Two-point lookup for loop implementationintDichotomy2 (intArr[],intLengthintnum) {     for(intHead =0, foot = length-1, middle = (length-1)/2; Head <= foot;middle= (head+foot)/2)    {        if(Arr[middle] = =num) {            returnMiddle; }        Else if(arr[middle]>num) {Foot= Middle-1; }        Else if(arr[middle]<num) {Head= Middle +1; }    }    return-1;}

  5. Complete Example

#include <stdio.h>#include<stdlib.h>#include<time.h>#defineN 100//binary sort while loop implementationintDichotomy1 (intArr[],intLengthintnum) {    intHead =0; intFoot = Length-1; intMiddle = (head + foot)/2;  while(Head <=foot) {        if(Arr[middle] =num) {            returnMiddle;  Break; }        Else if(Arr[middle] <num) {Head= middle-1; Middle= (head + foot)/2; }        Else if(Arr[middle] >num) {Foot= Middle+1; Middle= (head + foot)/2; }    }    return-1;}//Two-point lookup for loop implementationintDichotomy2 (intArr[],intLengthintnum) {     for(intHead =0, foot = length-1, middle = (length-1)/2; Head <= foot;middle= (head+foot)/2)    {        if(Arr[middle] = =num) {            returnMiddle; }        Else if(arr[middle]>num) {Foot= Middle-1; }        Else if(arr[middle]<num) {Head= Middle +1; }    }    return-1;}voidBubble_sort (intArr[],intlength) {     for(inti =0; I < length-1; i++)    {         for(intj =0; J < Length-1I J + +)        {            if(Arr[j]>arr[j +1])            {                //Exchange two numbersARR[J] = Arr[j] + arr[j +1]; Arr[j+1] = Arr[j]-arr[j +1]; ARR[J]= Arr[j]-arr[j +1]; }        }    }}voidMain () {time_t t; Srand ((unsignedint) Time (&t)); intA[n];  for(inti =0; i < N; i++) {A[i]= rand ()% -;//generate a random number within 100printf"%-2d\t", A[i]);        } bubble_sort (A,n); printf ("sort after \ n");  for(inti =0; i < N; i++) {printf ("%-2d\t", A[i]); } printf ("Please enter the number you want to find \ n"); intnum; scanf_s ("%d", &num); intFound =Dichotomy2 (A,n,num); if(Found! =-1) {printf ("found!a[%d]=%d", Found,num); }    Else{printf ("Not found!"); } System ("Pause");}

"Basic algorithm" bubble sort + binary search method

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.