Two- Dimensional Arrays

LOGO STYLE GUIDE Schools within the University Two-­‐Dimensional  Arrays   Two-­‐Dimensional  Arrays   •  A  one-­‐dimensional  array  stores  a  ...
Author: Shannon Benson
33 downloads 0 Views 4MB Size
LOGO STYLE GUIDE

Schools within the University

Two-­‐Dimensional  Arrays  

Two-­‐Dimensional  Arrays   •  A  one-­‐dimensional  array  stores  a  list  of  elements   •  A  two-­‐dimensional  array  can  be  thought  of  as  a   table  of  elements,  with  rows  and  columns     one two   dimension dimensions

LOGO STYLE GUIDE

2  

Schools within the University

Two-­‐Dimensional  Arrays   •  In  Java,  a  two-­‐dimensional  array  is  an  array  of  arrays   LOGO STYLE GUIDE

Schools within the University

•  A  two-­‐dimensional  array  is  declared  by  specifying  the   size  of  each  dimension  separately:   int[][] matrix = new int[12][50]; 3  

Two-­‐Dimensional  Arrays   •  DeclaraCon:   int[][] matrix = new int[12][50]; LOGO STYLE GUIDE

Schools within the University

•  Referencing  a  single  element:   value = matrix[3][6]; •  The  array  stored  in  one  row  can  be  specified  using   one  index   Expression

4  

Type

Description

matrix

int[][]

2D array of integers, or array of integer arrays

matrix[5]

int[]

array of integers

matrix[5][12]

int

integer

Looping  Through  a  2D  Array   int M = 10, N = 5; double[][] a = new double[M][N]; for (int i = 0; i < M; i++) for (int j = 0; j < N; j++) a[i][j] = 0;

LOGO STYLE GUIDE

Schools within the University

int M = 10, N = 5; double[][] a = new double[M][N]; for (int i = 0; i < a.length; i++) for (int j = 0; j < a[i].length; j++) a[i][j] = 0; 5  

Ragged  2D  Arrays   int M = 9; double[][] a = new double[M][]; for (int i = 0; i < M; i++) { a[i] = new double[M-i]; for (int j = 0; j < a[i].length; j++) a[i][j] = 0.0; }

LOGO STYLE GUIDE

Schools within the University

6  

Ragged  2D  Arrays   int scores[][] = { {44, 55, 66, 77}, {36}, {87, 97}, {68, 78, 88} };

LOGO STYLE GUIDE

Schools within the University

7  

LOGO STYLE GUIDE

Schools within the University

AsymptoCc  Analysis  

ComputaConal  Complexity   n  LOGO STYLE GUIDE

n 

n 

How  many  resources  will  it  take  to  solve  a  problem  of  a   given  size?   q  Cme   q  space   Schools within the University

Expressed  in  terms  of  problem  size  (beyond  some  minimum)   q  how  do  requirements  grow  as  size  grows?   Problem  size  (typically  called  n)   q  number  of  elements  to  be  handled   q  size  of  thing  to  be  operated  on   9  

The  Goal  of  AsymptoCc  Analysis   n  LOGO STYLE GUIDE

n 

How  to  analyze  the  running  Cme  (aka  computaConal   complexity)  of  an  algorithm  in  a  theoreCcal  model   Schools within the University

Using  a  theoreCcal  model  allows  us  to  ignore:       q  q 

n 

Which  computer  are  we  using?   How  good  is  our  compiler  at  opCmizaCon?  

We  define  the  running  Cme  of  an  algorithm  with  input   size  n  as  T  (n)  and  examine  the  rate  of  growth  of  T(n)  as  n   grows  larger  and  larger  and  larger.   10  

Growth  FuncCons   n  LOGO STYLE GUIDE

Constant    T(n)  =  c    ex:    ge]ng  array  element  at  known  locaCon          any  simple  Java  statement  (e.g.  assignment)   Schools within the University

   

  n 

Linear    T(n)  =  cn    [+  possible  lower  order  terms]    ex:    finding  parCcular  element  in  array  of  size  n      (i.e.  sequenCal  search)        trying  on  all  of  your  n  shirts  

  11  

Growth  FuncCons  (cont.)   n  LOGO STYLE GUIDE

n 

QuadraCc    T(n)  =  cn2  [  +  possible  lower  order  terms]    ex:    sorCng  all  the  elements  in  an  array  (using  inserCon  sort)          trying  all  your  n  shirts  with  all  your  n  pants       Polynomial    T(n)  =  cnk  [  +  possible  lower  order  terms]    ex:  finding  the  largest  element  of  a  k-­‐dimensional  array        looking  for  maximum  substrings  in  array   Schools within the University

      12  

Growth  FuncCons  (cont.)   n  LOGO STYLE GUIDE

n 

ExponenCal    T(n)  =  cn  [+  possible  lower  order  terms]      ex:  construcCng  all  possible  orders  of  array  elements        Towers  of  Hanoi  (2n)      Recursively  calculaCng  nth  Fibonacci  number  (2n)         Logarithmic    T(n)  =  lg  n  [  +  possible  lower  order  terms]    ex:  finding  a  parCcular  array  element  (binary  search)        any  algorithm  that  conCnually  divides  a  problem  in  half     Schools within the University

  13  

A  Graph  of  Growth  FuncCons   LOGO STYLE GUIDE

Schools within the University

14  

Expanded  Scale   LOGO STYLE GUIDE

Schools within the University

15  

AsymptoCc  Analysis   How  does  the  Cme  (or  space)  requirement  grow  as  the   problem  size  grows  really,  really  large?  

LOGO STYLE GUIDE

Schools within the University

q 

We  are  interested  in  “order  of  magnitude”  growth  rate  

Simplifying  AssumpCons:   We  drop  constant  mulCpliers      T(n)  =  cn2          =>        T(n)  =  n2   q  Lower  order  terms  don’t  mager      T(n)  =  n2  +  n          =>      T(n)  =  n2   q 

  We  call  the  result  “Big-­‐Oh”    

16  

Analysis  Cases   n 

What  parCcular  input  (of  given  size)  gives  worst/best/

average  complexity?  

  Best  Case:  Cme  for  the  absolutely  best  input  for  the  algorithm   Worst  Case:  Cme  for  the  absolutely  worst  input  for  the  algorithm   Average  case  is  the  “run  Cme  efficiency”  over  all  possible  inputs       n  Mileage  example:  how  much  gas  does  it  take  to  go  20  miles?   q  Worst  case:      all  uphill   q  Best  case:      all  downhill,  just  coast   q  Average  case:    “average”  terrain  

LOGO STYLE GUIDE

Schools within the University

17  

Cases  Example   n  LOGO STYLE GUIDE

Consider  sequenCal  search  on  an  unsorted   array  of  length  n,  what  is  Cme  complexity?   Schools within the University

  n 

Best  case:  

  n 

Worst  case:  

  n 

Average  case:  

  18  

Analyzing  the  Complexity  of  Code   1.  All  simple  procedural  Java  statements  take  O(1)     2.  For  consecuCve  statements,  we  add  their  complexiCes   q  Since  lower-­‐order  terms  don’t  mager,  this  is  the  same  as   taking  their  max()     3.  For  nested  statements  (loops,  funcCon  calls),  we  mulCply  their   complexiCes  

LOGO STYLE GUIDE

Schools within the University

19  

Example   n  LOGO STYLE GUIDE

Code:    a = b; Schools within the University

++sum; int y = 42 % 16;

  n 

Complexity:  

20  

Example   n  LOGO STYLE GUIDE

Code:    sum = 0; Schools within the University

for (i = 1; i

Suggest Documents