Quickly find out if there are two elements in array A that are equal to the integer x

Source: Internet
Author: User

public class quicksearch {      public static int  Getmiddle (List<integer> list, int low, int high)  {           integer temp = list.get (High);                while  (Low < high)  {                  while   (Low < high && list.get (Low)  < temp)  {                       low++;                  }                  List.set (High, list.get (Low));                 while   (Low < high && list.get (high)  > temp)  {                       high--;                  }                  List.set (Low, list.get (high));                 if  (List.get (High)  == temp)  {                     break;                 }          &nbsP;  }             list.set (High, &NBSP;TEMP);        return low;                       }          //first position 3 points     public static void  quicksearch (List<integer> list, int low, int high)  {          if  (Low < high)  {              int target = getmiddle (List, low,  high);             if  (target<2 )  {                return ;            }             int middle = getmiddle (list, low, target-1);             int lwoMiddle = 0;             if  (middle>0)  {                 lwomiddle = getmiddle (List, low, &NBSP;MIDDLE-1);            }             int highMiddle = target-1;             if (Middle+1 != target) {                 highMiddle =  Getmiddle (List, middle+1, target-1);            }             int lowLow = low;             int lowHigh = target-1;             int highLow = target+1;             int highHigh = high;             numsearch (List,target,lwomiddle,highmiddle,lowlow,lowhigh,highlow,highhigh );        }     }           //find matching add-ons     public static void  Numsearch (List<integer> list,int target,int lwomiddle,int highmiddle,int lowlow ,  int lowhigh,inT highlow,int highhigh) {        integer targetnum  = list.get (target);        integer lwomiddlenum =  List.get (Lwomiddle);        integer highmiddlenum =  List.get (Highmiddle);        boolean flag = true;         while (flag) {             if  (Lwomiddlenum + highmiddlenum > targetnum)  {                 recursionsearch (list, Lwomiddle+1,lowhigh,highlow,highmiddle-1,target);                 recursionsearch (List,lowlow,lwomiddle-1,highmiddle+1,highhigh,target);     &Nbsp;           recursionsearch (list,lowLow,lwoMiddle-1 , highlow,highmiddle-1,target);             }else  if  (Lwomiddlenum + highmiddlenum < targetnum)  {                 recursionsearch (List,lwoMiddle+1, Lowhigh,highmiddle+1,highhigh,target);             } else{                 System.out.println (lwomiddlenum+ " + " +highmiddlenum+ " = " +targetnum);                 flag = false;             }         }   &nBSP;}          public static void recursionsearch (List <integer> list, int lowlow, int lowhigh,int highlow,int highhigh,int  target) {        int lwomiddle = -1;         int highMiddle = -1;         if  (Lowlow < lowhigh)  {             lwomiddle = getmiddle (List, lowlow, lowhigh);            }        if  ( Highlow < highhigh)  {              highmiddle = getmiddle (List, highlow, highhigh);         }        if  (lwomiddle == -1 | |  highMiddle == -1 )  {             return;        }         numsearch (List,target,lwomiddle,highmiddle, lowlow, lowhigh, highlow, highhigh);     }               Public static void quick (List<integer> list,int search)  {          list.add (search);         if   (List.size ()  > 0)  {               quicksearch (List, 0, list.size ()  - 1);          }     }&nbsP;    public static void main (String[] args)  {         List<Integer> list = new ArrayList<Integer> ();         list.add (+);         List.add (3);         list.add (;      )   list.add (1);         list.add (9);         list.add (7);         list.add (one);         list.add (+);         list.add (5);         list.add (6);         Quick (list, 10);     }}

Complexity: n+ (N/2) LG (N/2).

Quickly find out if there are two elements in array A that are equal to the integer x

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.