05 binarysearch complexity

Upload: selvakumar2k2

Post on 02-Jun-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 05 Binarysearch Complexity

    1/22

    CSE 143

    Lecture 5

    Binary search; complexity

    reading: 13.1 - 13.2

    slides created by Marty Stepp and Hlne Martin

    http:!!""".cs."ashington.ed#!1$3!

    http://www.cs.washington.edu/143/http://www.cs.washington.edu/143/
  • 8/10/2019 05 Binarysearch Complexity

    2/22

    2

    Sequential search

    %sequential search: &ocates a target 'al#e in anarray ! list by examining each element (rom startto (inish. )sed in indexOf.

    * Ho" many elements "ill it need to examine+

    * ,xample: Searching the array belo" (or the 'al#e 42:

    * otice that the array is sorted. o#ld "e ta/e

    ad'antage o( this+

    index

    0 1 2 3 $ 4 5 10

    11

    12

    13

    1$

    1

    1

    'al#e -$ 2 10 1 20 22 2 30 3 $2 0 4 4 52 103

    i

  • 8/10/2019 05 Binarysearch Complexity

    3/22

    3

    Binary search (13.1)

    %binary search: &ocates a target 'al#e in asorted array ! list by s#ccessi'elyeliminating hal( o( the array (romconsideration.

    * Ho" many elements "ill it need to examine+

    * ,xample: Searching the array belo" (or the 'al#e 42:

    index

    0 1 2 3 $ 4 5 10

    11

    12

    13

    1$

    1

    1

    'al#e -$ 2 10 1 20 22 2 30 3 $2 0 4 4 52 103

    min mid max

  • 8/10/2019 05 Binarysearch Complexity

    4/22

    4

    Arrays.binarySearch

    // searches an entire sorted array for a given value

    // returns its index if found; a negative number if not found

    // Precondition: array is sorted

    Arrays.binarySearch(array, value)

    // searches given portion of a sorted array for a given value

    // examines minIndex (inclusive through maxIndex (exclusive

    // returns its index if found; a negative number if not found// Precondition: array is sorted

    Arrays.binarySearch(array, minIne!, ma!Ine!, value)

    %6he binarySearchmethod in the Arraysclass searchesan array 'ery e((iciently i( the array is sorted.

    * 7o# can search the entire array8 or 9#st a range o(indexes#se(#l (or #n(illed arrays s#ch as the one inArrayIntList

  • 8/10/2019 05 Binarysearch Complexity

    5/22

    5

    "sin#binarySearch

    // index ! " # $ % & ' ) * "! "" "# "$ "% "&

    int[] a = {-4, 2, , !, "#, "!, 2#, 2$, %&, %', 42, #&, #', '$, $#, !2

    int index =Arrays.binarySearch(a, &, "', %#) // index" is "!

    int index2 =Arrays.binarySearch(a, &, "', #") // index# is +

    *binarySearchret#rns the index "here the 'al#e is

    (o#nd%i( the 'al#e is not (o#nd8 binarySearchret#rns:

    -(inserti+n+int ")

    % "here inserti+n+intis the index "here the element wouldha'e been8 i( it had been in the array in sorted order.

    % 6o insert the 'al#e into the array8 negate inserti+n+int= 1

    int index,oInsert#" - +(index# "; // '

    i i i

  • 8/10/2019 05 Binarysearch Complexity

    6/22

    6

    $untime E%%iciency(13.2)

    %Ho" m#ch better is binary search than se>#entialsearch+

    %e%%iciency: ? meas#re o( the #se o( comp#ting reso#rcesby code.

    * can be relati'e to speed time

  • 8/10/2019 05 Binarysearch Complexity

    7/227

    E%%iciency e!am&les

    statement1statement2statement3

    f+r (int i = " i = / i) {

    statement4

    f+r (int i = " i = / i) {

    statement5 statement' statement

    3

    3

    $ = 3

  • 8/10/2019 05 Binarysearch Complexity

    8/228

    E%%iciency e!am&les 2

    f+r (int i = " i = / i) { f+r (int 0 = " 0 = / 0) {

    statement1

    f+r (int i = " i = / i) {

    statement2 statement3 statement4

    statement5

    %Ho" many statements "ill exec#te i( 10+ C( 1000+

    2= $

    2

    $

  • 8/10/2019 05 Binarysearch Complexity

    9/229

    l#*rithm #r*+th rates(13.2)

    %De meas#re r#ntime in proportion to the inp#t datasiEe8 .

    * #r*+th rate: hange in r#ntime as changes.

    %Say an algorithm r#ns ,.4-3 25-2 /- 1

    statements.* onsider the r#ntime "hen is extremely large.

    * De ignore constants li/e 2 beca#se they are tiny next to.

    * 6he highest-order term 3< dominates the o'erall r#ntime.

    * De say that this algorithm r#ns on the order o( 3.

    * or 0(-3)(or short Bi#0ho( c#bed#adr#ples 1 min $2sec

    c#bic F3< m#ltiplies by 4 min

    ... ... ... ...

  • 8/10/2019 05 Binarysearch Complexity

    11/2211

    C*m&le!ity classes

    From http://recursive-design.com/blog/2010/12/07/comp-sci-101-big-o-notation/- post about a Google interview

    http://recursive-design.com/blog/2010/12/07/comp-sci-101-big-o-notation/http://recursive-design.com/blog/2010/12/07/comp-sci-101-big-o-notation/
  • 8/10/2019 05 Binarysearch Complexity

    12/22

    12

    Sequential search

    %Dhat is its complexity class+

    1b3ic int indexOf(int a3e) {

    f+r (int i = & i si5e i) {

    if (e3e6ent7ata[i] == a3e) {

    retrn i

    retrn -" 88 n+t f+nd

    %Fn a'erage8 only !2 elements are 'isited

    * 1!2 is a constant that can be ignored

    index

    0 1 2 3 $ 4 5 10

    11

    12

    13

    1$

    1

    1

    'al#e

    -$

    2 10

    1

    20

    22

    2

    30

    3

    $2

    0

    4

    4

    52

    103

  • 8/10/2019 05 Binarysearch Complexity

    13/22

    13

    C*llecti*n e%%iciency

    eth* rrayList

    add

    add(index,

    value)

    indexOf

    9et

    re6+eset

    si5e

    %,((iciency o( o#r ArrayIntListor @a'aAsArrayList:eth* rrayLi

    st

    add F1