datastructures) lecture11bubble up •...

196
Datastructures Lecture 11 Andreas Wieden

Upload: others

Post on 06-Nov-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Datastructures  Lecture  11  

Andreas  Wieden  

Page 2: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Outline  

• Heap  sort.  • Bucket  sort  • Quick  sort.  

Page 3: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Why  study  Heapsort?

• It  is  a  well-­‐known,  tradi:onal  sor:ng  algorithm  you  will  be  expected  to  know  

• Heapsort  is  always  O(n  log  n),  even  in  worst  case.  

Page 4: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

What  is  a  “heap”? •  Defini:ons  of  heap:  

1.  A  large  area  of  memory  from  which  the  programmer  can  allocate  blocks  as  needed,  and  deallocate  them  (or  allow  them  to  be  garbage  collected)  when  no  longer  needed  

2.  A  balanced,  leK-­‐jus:fied  binary  tree  in  which  no  node  has  a  value  greater  than  the  value  in  its  parent  

•  These  two  defini:ons  have  liNle  in  common  

•  Heapsort  uses  the  second  defini:on  

Page 5: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Balanced  binary  trees • Recall:  

•  The  depth  of  a  node  is  its  distance  from  the  root  •  The  depth  of  a  tree  is  the  depth  of  the  deepest  node  

• A  binary  tree  of  depth  n  is  balanced  if  all  the  nodes  at  depths  0  through  n-2  have  two  children  

Balanced Balanced Not balanced

n-2 n-1 n

Page 6: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Le=-­‐jus@fied  binary  trees • A  balanced  binary  tree  is  leK-­‐jus:fied  if:  

•  all  the  leaves  are  at  the  same  depth,  or  •  all  the  leaves  at  depth  n+1  are  to  the  leK  of  all  the  nodes  at  depth  n

Left-justified Not left-justified

Page 7: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Plan  of  aDack

•  First,  we  will  learn  how  to  turn  a  binary  tree  into  a  heap  • Next,  we  will  learn  how  to  turn  a  binary  tree  back  into  a  heap  aKer  it  has  been  changed  in  a  certain  way  

• We  will  use  these  ideas  to  sort  an  array  

Page 8: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

The  heap  property • A  node  has  the  heap  property  if  the  value  in  the  node  is  as  large  as  or  larger  than  the  values  in  its  children  

• All  leaf  nodes  automa:cally  have  the  heap  property  • A  binary  tree  is  a  heap  if  all  nodes  in  it  have  the  heap  property  

12

8 3 Blue node has heap property

12

8 12 Blue node has heap property

12

8 14 Blue node does not have heap property

Page 9: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Bubble up •  Given  a  node  that  does  not  have  the  heap  property,  you  can  give  it  the  heap  property  by  exchanging  its  value  with  the  value  of  the  larger  child  

•  This  is  some:mes  called  bubble  up  •  No:ce  that  the  child  may  have  lost  the  heap  property  

14

8 12 Blue node has heap property

12

8 14 Blue node does not have heap property

Page 10: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Construc@ng  a  heap  I • A  tree  consis:ng  of  a  single  node  is  automa:cally  a  heap  

• We  construct  a  heap  by  adding  nodes  one  at  a  :me:  

•  Add  the  node  just  to  the  right  of  the  rightmost  node  in  the  deepest  level  

•  If  the  deepest  level  is  full,  start  a  new  level  •  Examples:   Add a new

node here Add a new node here

Page 11: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Construc@ng  a  heap  II

•  Each  :me  we  add  a  node,  we  may  destroy  the  heap  property  of  its  parent  node  

•  To  fix  this,  we  bubble  up  • But  each  :me  we  bubble  up,  the  value  of  the  topmost  node  in  the  swap  may  increase,  and  this  may  destroy  the  heap  property  of  its  parent  node  

• We  repeat  the  bubble  up  process,  moving  up  in  the  tree,  un:l  either  • We  reach  nodes  whose  values  don’t  need  to  be  swapped  (because  the  parent  is  s-ll  larger  than  both  children),  or  

• We  reach  the  root  

Page 12: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Construc@ng  a  heap  III

8 8

10

10

8

10

8 5

10

8 5

12

10

12 5

8

12

10 5

8

1 2 3

4

Page 13: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Other  children  are  not  affected

•  The  node  containing  8  is  not  affected  because  its  parent  gets  larger,  not  smaller  

•  The  node  containing  5  is  not  affected  because  its  parent  gets  larger,  not  smaller  

•  The  node  containing  8  is  s:ll  not  affected  because,  although  its  parent  got  smaller,  its  parent  is  s:ll  greater  than  it  was  originally  

12

10 5

8 14

12

14 5

8 10

14

12 5

8 10

Page 14: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

A  sample  heap •  Here’s  a  sample  binary  tree  aKer  it  has  been  heapified  

•  No:ce  that  heapified  does  not  mean  sorted  •  Heapifying  does  not  change  the  shape  of  the  binary  tree;  this  binary  tree  is  balanced  and  leK-­‐jus:fied  because  it  started  out  that  way  

19

14 18

22

3 21

14

11 9

15

25

17 22

Page 15: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Removing  the  root •  No:ce  that  the  largest  number  is  now  in  the  root  •  Suppose  we  discard  the  root:  

•  How  can  we  fix  the  binary  tree  so  it  is  once  again  balanced  and  le2-­‐jus-fied?  

•  Solu:on:  remove  the  rightmost  leaf  at  the  deepest  level  and  use  it  for  the  new  root  

19

14 18

22

3 21

14

11 9

15

17 22

11

Page 16: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

The  reHeap  method  I •  Our  tree  is  balanced  and  leK-­‐jus:fied,  but  no  longer  a  heap  •  However,  only  the  root  lacks  the  heap  property  

• We  can  bubble down  the  root  •  AKer  doing  this,  one  and  only  one  of  its  children  may  have  lost  the  heap  property  

19

14 18

22

3 21

14

9

15

17 22

11

Page 17: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

The  reHeap  method  II •  Now  the  leK  child  of  the  root  (s:ll  the  number  11)  lacks  the  heap  property  

• We  can  bubble down  this  node  •  AKer  doing  this,  one  and  only  one  of  its  children  may  have  lost  the  heap  property  

19

14 18

22

3 21

14

9

15

17 11

22

Page 18: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

The  reHeap  method  III •  Now  the  right  child  of  the  leK  child  of  the  root  (s:ll  the  number  11)  lacks  the  heap  property:  

• We  can  bubble down this  node  •  AKer  doing  this,  one  and  only  one  of  its  children  may  have  lost  the  heap  property  —but  it  doesn’t,  because  it’s  a  leaf  

19

14 18

11

3 21

14

9

15

17 22

22

Page 19: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

The  reHeap  method  IV •  Our  tree  is  once  again  a  heap,  because  every  node  in  it  has  the  heap  property  

•  Once  again,  the  largest  (or  a  largest)  value  is  in  the  root  • We  can  repeat  this  process  un:l  the  tree  becomes  empty  •  This  produces  a  sequence  of  values  in  order  largest  to  smallest  

19

14 18

21

3 11

14

9

15

17 22

22

Page 20: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Heap  sort

 A  simple  sor+ng  algorith  

 •  Insert  each  element  into  a  binary  heap.  •  Execute  delete-­‐min  and  insert  into  an  new  array  un:l  the  binary  heap  is  empty.  

Page 21: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Heap  sort  

 One  variant  is  to  use  the  same  array  by  inser:ng  the  removed  elements  in  the  end  of  the  array.  

Page 22: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Sor@ng

What  do  heaps  have  to  do  with  sor:ng  an  array?    

•  Because  the  binary  tree  is  balanced  and  le2  jus-fied,  it  can  be  represented  as  an  array  

•  All  our  opera:ons  on  binary  trees  can  be  represented  as  opera:ons  on  arrays  •  To  sort:  

  heapify the array;   while the array isn’t empty {   remove and replace the root;   reheap the new root node;

}

Page 23: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Mapping  into  an  array

• No:ce:  •  The  leK  child  of  index i is  at  index 2*i+1 •  The  right  child  of  index  i  is  at  index 2*i+2 •  Example:  the  children  of  node  3  (19)  are  7  (18)  and  8 (14)  

19

14 18

22

3 21

14

11 9

15

25

17 22

25 22 17 19 22 14 15 18 14 21 3 9 11 0 1 2 3 4 5 6 7 8 9 10 11 12

Page 24: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Removing  and  replacing  the  root •  The  “root”  is  the  first  element  in  the  array  •  The  “rightmost  node  at  the  deepest  level”  is  the  last  element  •  Swap  them...  

•  ...And  pretend  that  the  last  element  in  the  array  no  longer  exists—that  is,  the  “last  index”  is  11  (9)  

25 22 17 19 22 14 15 18 14 21 3 9 11 0 1 2 3 4 5 6 7 8 9 10 11 12

11 22 17 19 22 14 15 18 14 21 3 9 25 0 1 2 3 4 5 6 7 8 9 10 11 12

Page 25: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Reheap  and  repeat •  Reheap  the  root  node  (index  0,  containing  11)...  

•  ...And  again,  remove  and  replace  the  root  node  •  Remember,  though,  that  the  “last”  array  index  is  changed  •  Repeat  un:l  the  last  becomes  first,  and  the  array  is  sorted!    

22 22 17 19 21 14 15 18 14 11 3 9 25 0 1 2 3 4 5 6 7 8 9 10 11 12

9 22 17 19 22 14 15 18 14 21 3 22 25 0 1 2 3 4 5 6 7 8 9 10 11 12

11 22 17 19 22 14 15 18 14 21 3 9 25 0 1 2 3 4 5 6 7 8 9 10 11 12

Page 26: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Time  Complexity

Assump:on:  Compare  opera:ons  take  constant  :me.  

Page 27: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

     Compare  opera:ons  take  constant  :me.  The  depth  of  a  specifik  leaf=  The  number  of  comparisons  to  sort  a  list.  • Hight  of  the  tree  =  Worst  case  •  The  depth  of  the  depest  node  is  log  n.  

Time  Complexity

Page 28: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Time  Complexity

     Sor:ng  using  Heapsort  is  O(n  log  n)  

Page 29: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

Bucket  Sort • Given  that,  we  have  N  integers  in  the  range  0  to  M-­‐1  

• Maintain  an  array  count  of  size  M,  which  is  ini:alized  to  zero.  Thus,  count  has  M  cells  (buckets).  

• Read  Ai    •  Increment  count[Ai]  by  1  • AKer  all  the  input  array  is  read,  scan  the  count  array,  prin:ng  out  a  representa:on  of  sorted  list.  

Page 30: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Bucket  Sort  Example

• A=(0.5, 0.1, 0.3, 0.4, 0.3, 0.2, 0.1, 0.1, 0.5, 0.4, 0.5)  

Sorted  list:                                    0.1, 0.1, 0.1, 0.2, 0.3, 0.3, 0.4, 0.4, 0.5, 0.5, 0.5

bucket in array

B[1] 0.1, 0.1, 0.1

B[2] 0.2

B[3] 0.3, 0.3

B[4] 0.4, 0.4

B[5] 0.5, 0.5, 0.5

Page 31: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

Radix  Sort • Radix  sort  is  generaliza:on  of  bucket  sort.  •  It  uses  several  passes  of  bucket  sort  • Perform  the  bucket  sorts  by  “least  significant  digits”  

•  First  sort  by  digit  in  units  place  •  Second  sort  by  digit  in  tens  place  •  Third  sort  by  digit  in  hundreds  place  •  …….    

Page 32: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

64   8   216   512   27   729   0   1   343   125  

 Radix  sort,  an  example:  

Page 33: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

Pass  1

Page 34: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

Page 35: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

64   8   216   512   27   729   0   1   343   125  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

Page 36: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

64   8   216   512   27   729   0   1   343   125  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

Page 37: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

64   8   216   512   27   729   0   1   343   125  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

Page 38: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

9  

64  

64   8   216   512   27   729   0   1   343   125  

8  

Page 39: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

64   8   216   512   27   729   0   1   343   125  

8  

Page 40: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

64   8   216   512   27   729   0   1   343   125  

8  

Page 41: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

64   8   216   512   27   729   0   1   343   125  

8  

216  

Page 42: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

64   8   216   512   27   729   0   1   343   125  

8  

216  

Page 43: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

64   8   216   512   27   729   0   1   343   125  

8  

216  

512  

Page 44: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

64   8   216   512   27   729   0   1   343   125  

8  

216  

512  

Page 45: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

64   8   216   512   27   729   0   1   343   125  

8  

216  

512  

27  

Page 46: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

64   8   216   512   27   729   0   1   343   125  

8  

216  

512  

27  

Page 47: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

64   8   216   512   27   729   0   1   343   125  

8  

216  

512  

27  

729  

Page 48: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

64   8   216   512   27   729   0   1   343   125  

8  

216  

512  

27  

729  

Page 49: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

64   8   216   512   27   729   0   1   343   125  

8  

216  

512  

27  

729  

0  

Page 50: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

64   8   216   512   27   729   0   1   343   125  

8  

216  

512  

27  

729  

0  

Page 51: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

64   8   216   512   27   729   0   1   343   125  

8  

216  

512  

27  

729  

0  

1  

Page 52: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

64   8   216   512   27   729   0   1   343   125  

8  

216  

512  

27  

729  

0  

1  

Page 53: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

64   8   216   512   27   729   0   1   343   125  

8  

216  

512  

27  

729  

0  

1  

343  

Page 54: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

64   8   216   512   27   729   0   1   343   125  

8  

216  

512  

27  

729  

0  

1  

343  

Page 55: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

64   8   216   512   27   729   0   1   343   125  

8  

216  

512  

27  

729  

0  

1  

343  

125  

Page 56: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

64   8   216   512   27   729   0   1   343   125  

8  

216  

512  

27  

729  

0  

1  

343  

125  

Page 57: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

8  

216  

512  

27  

729  

0  

1  

343  

125  

Page 58: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

8  

216  

512  

27  

729  

1  

343  

125  

0   0  

Page 59: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

0  

8  

216  

512  

27  

729  

1  

343  

125  

0   0  

Page 60: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

1  

2  

3  

4  

5  

6  

7  

8  

9  

64  

0  

8  

216  

512  

27  

729  

1  

343  

125  

0  

Page 61: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

2  

3  

4  

5  

6  

7  

8  

9  

64  

0  

8  

216  

512  

27  

729  

343  

125  

0  

1   1  

Page 62: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

2  

3  

4  

5  

6  

7  

8  

9  

64  

0   1  

8  

216  

512  

27  

729  

343  

125  

0  

1   1  

Page 63: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

2  

3  

4  

5  

6  

7  

8  

9  

64  

0   1  

8  

216  

512  

27  

729  

343  

125  

0  

1  

Page 64: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

2  

3  

4  

5  

6  

7  

8  

9  

64  

0   1   512  

8  

216  

512  

27  

729  

343  

125  

0  

1  

Page 65: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

4  

5  

6  

7  

8  

9  

64  

0   1   512  

8  

216  

27  

729  

343  

125  

0  

1  

2  

Page 66: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

Pass  2

Page 67: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   1   512   343   64   125   216   27   8   729  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

Page 68: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

   00   1   512   343   64   125   216   27   8   729  

0  

1  

2  

3  

4  

5  

6  

7  

8  

9  

Page 69: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   1   512   343   64   125   216   27   8   729  

1  

2  

3  

4  

5  

6  

7  

8  

9  

0   0  

Page 70: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   01   512   343   64   125   216   27   8   729  

1  

2  

3  

4  

5  

6  

7  

8  

9  

0   0  

Page 71: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   1   512   343   64   125   216   27   8   729  

1  

2  

3  

4  

5  

6  

7  

8  

9  

0   1  0  

Page 72: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   1   512   343   64   125   216   27   8   729  

1  

2  

3  

4  

5  

6  

7  

8  

9  

0   1  0  

Page 73: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   1   512   343   64   125   216   27   8   729  

2  

3  

4  

5  

6  

7  

8  

9  

0   1  0  

1   512  

Page 74: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   1   512   343   64   125   216   27   8   729  

2  

3  

4  

5  

6  

7  

8  

9  

0   1  0  

1   512  

Page 75: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   1   512   343   64   125   216   27   8   729  

2  

3  

5  

6  

7  

8  

9  

0   1  0  

1   512  

4   343  

Page 76: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   1   512   343   64   125   216   27   8   729  

2  

3  

5  

6  

7  

8  

9  

0   1  0  

1   512  

4   343  

Page 77: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   1   512   343   64   125   216   27   8   729  

2  

3  

5  

7  

8  

9  

0   1  0  

1   512  

4   343  

6   64  

Page 78: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   1   512   343   64   125   216   27   8   729  

2  

3  

5  

7  

8  

9  

0   1  0  

1   512  

4   343  

6   64  

Page 79: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   1   512   343   64   125   216   27   8   729  

3  

5  

7  

8  

9  

0   1  0  

1   512  

4   343  

6   64  

2   125  

Page 80: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   1   512   343   64   125   216   27   8   729  

3  

5  

7  

8  

9  

0   1  0  

1   512  

4   343  

6   64  

2   125  

Page 81: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   1   512   343   64   125   216   27   8   729  

3  

5  

7  

8  

9  

0   1  0  

1   216  

4   343  

6   64  

2   125  

512  

Page 82: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   1   512   343   64   125   216   27   8   729  

3  

5  

7  

8  

9  

0   1  0  

1   216  

4   343  

6   64  

2   125  

512  

Page 83: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   1   512   343   64   125   216   27   8   729  

3  

5  

7  

8  

9  

0   1  0  

1   216  

4   343  

6   64  

2   27  

512  

125  

Page 84: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   1   512   343   64   125   216   27   08   729  

3  

5  

7  

8  

9  

0   1  0  

1   216  

4   343  

6   64  

2   27  

512  

125  

Page 85: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   1   512   343   64   125   216   27   8   729  

3  

5  

7  

8  

9  

0   8  0  

1   216  

4   343  

6   64  

2   27  

512  

125  

1  

Page 86: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   1   512   343   64   125   216   27   8   729  

3  

5  

7  

8  

9  

0   8  0  

1   216  

4   343  

6   64  

2   27  

512  

125  

1  

Page 87: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   1   512   343   64   125   216   27   8   729  

3  

5  

7  

8  

9  

0   8  0  

1   216  

4   343  

6   64  

2   729  

512  

125  

1  

27  

Page 88: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

0   1   512   343   64   125   216   27   8   729  

3  

5  

7  

8  

9  

0   8  0  

1   216  

4   343  

6   64  

2   729  

512  

125  

1  

27  

Page 89: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

5  

7  

8  

9  

0   8  0  

1   216  

4   343  

6   64  

2   729  

512  

125  

1  

27  

Page 90: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

5  

7  

8  

9  

0   8  0  

1   216  

4   343  

6   64  

2   729  

512  

125  

1  

27  

0   1   8  

Page 91: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

5  

7  

8  

9  

1   216  

4   343  

6   64  

2   729  

512  

125   27  

0   1   8  

0  

Page 92: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

5  

7  

8  

9  

1   216  

4   343  

6   64  

2   729  

512  

125   27  

0   1   8   512   216  

0  

Page 93: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

5  

7  

8  

9  

4   343  

6   64  

2   729  125   27  

0   1   8   512   216  

0  

1  

Page 94: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

5  

7  

8  

9  

4   343  

6   64  

2   729  125   27  

0   1   8   512   216   125   27   729  

0  

1  

Page 95: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

5  

7  

8  

9  

4   343  

6   64  

0   1   8   512   216   125   27   729  

0  

1  

2  

Page 96: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

5  

7  

8  

9  

4   343  

6   64  

0   1   8   512   216   125   27   729   343  

0  

1  

2  

Page 97: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

5  

7  

8  

9  

6   64  

0   1   8   512   216   125   27   729   343  

0  

1  

2  

4  

Page 98: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

5  

7  

8  

9  

6   64  

0   1   8   512   216   125   27   729   343   64  

0  

1  

2  

4  

Page 99: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

Pass  3

Page 100: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

5  

7  

8  

9  

0   1   8   512   216   125   27   729   343   64  

0  

1  

2  

4  

6  

Page 101: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

5  

7  

8  

9  

000   1   8   512   216   125   27   729   343   64  

0  

1  

2  

4  

6  

Page 102: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

5  

7  

8  

9  

0   1   8   512   216   125   27   729   343   64  

1  

2  

4  

6  

0   0  

Page 103: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

5  

7  

8  

9  

0   001   8   512   216   125   27   729   343   64  

1  

2  

4  

6  

0   0  

Page 104: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

5  

7  

8  

9  

0   1   8   512   216   125   27   729   343   64  

1  

2  

4  

6  

0   1  0  

Page 105: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

5  

7  

8  

9  

0   1   008   512   216   125   27   729   343   64  

1  

2  

4  

6  

0   1  0  

Page 106: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

5  

7  

8  

9  

0   1   8   512   216   125   27   729   343   64  

1  

2  

4  

6  

0   8  0   1  

Page 107: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

5  

7  

8  

9  

0   1   8   512   216   125   27   729   343   64  

1  

2  

4  

6  

0   8  0   1  

Page 108: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

7  

8  

9  

0   1   8   512   216   125   27   729   343   64  

1  

2  

4  

6  

0   8  0   1  

5   512  

Page 109: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

7  

8  

9  

0   1   8   512   216   125   27   729   343   64  

1  

2  

4  

6  

0   8  0   1  

5   512  

Page 110: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

7  

8  

9  

0   1   8   512   216   125   27   729   343   64  

1  

4  

6  

0   8  0   1  

5   512  

2   216  

Page 111: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

7  

8  

9  

0   1   8   512   216   125   27   729   343   64  

1  

4  

6  

0   8  0   1  

5   512  

2   216  

Page 112: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

7  

8  

9  

0   1   8   512   216   125   27   729   343   64  

4  

6  

0   8  0   1  

5   512  

2   216  

1   125  

Page 113: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

7  

8  

9  

0   1   8   512   216   125   027   729   343   64  

4  

6  

0   8  0   1  

5   512  

2   216  

1   125  

Page 114: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

7  

8  

9  

0   1   8   512   216   125   27   729   343   64  

4  

6  

0   27  0   1  

5   512  

2   216  

1   125  

8  

Page 115: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

7  

8  

9  

0   1   8   512   216   125   27   729   343   64  

4  

6  

0   27  0   1  

5   512  

2   216  

1   125  

8  

Page 116: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

8  

9  

0   1   8   512   216   125   27   729   343   64  

4  

6  

0   27  0   1  

5   512  

2   216  

1   125  

8  

7   729  

Page 117: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

3  

8  

9  

0   1   8   512   216   125   27   729   343   64  

4  

6  

0   27  0   1  

5   512  

2   216  

1   125  

8  

7   729  

Page 118: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

8  

9  

0   1   8   512   216   125   27   729   343   64  

4  

6  

0   27  0   1  

5   512  

2   216  

1   125  

8  

7   729  

3   343  

Page 119: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

8  

9  

0   1   8   512   216   125   27   729   343   064  

4  

6  

0   27  0   1  

5   512  

2   216  

1   125  

8  

7   729  

3   343  

Page 120: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

8  

9  

0   1   8   512   216   125   27   729   343   64  

4  

6  

0   64  0   1  

5   512  

2   216  

1   125  

8  

7   729  

3   343  

27  

Page 121: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

8  

9  

0   1   8   512   216   125   27   729   343   64  

4  

6  

0   64  0   1  

5   512  

2   216  

1   125  

8  

7   729  

3   343  

27  

Page 122: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

8  

9  

4  

6  

0   64  0   1  

5   512  

2   216  

1   125  

8  

7   729  

3   343  

27  

Page 123: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

8  

9  

4  

6  

0   64  0   1  

5   512  

2   216  

1   125  

8  

7   729  

3   343  

27  

0   1   8   27   64  

Page 124: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

8  

9  

4  

6  

5   512  

2   216  

1   125  

7   729  

3   343  

0   1   8   27   64  

0  

Page 125: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

8  

9  

4  

6  

5   512  

2   216  

1   125  

7   729  

3   343  

0   1   8   27   64   125  

0  

Page 126: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

8  

9  

4  

6  

5   512  

2   216  

7   729  

3   343  

0   1   8   27   64   125  

0  

1  

Page 127: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

8  

9  

4  

6  

5   512  

2   216  

7   729  

3   343  

0   1   8   27   64   125   216  

0  

1  

Page 128: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

8  

9  

4  

6  

5   512  

7   729  

3   343  

0   1   8   27   64   125   216  

0  

1  

2  

Page 129: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

8  

9  

4  

6  

5   512  

7   729  

3   343  

0   1   8   27   64   125   216   343  

0  

1  

2  

Page 130: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

8  

9  

4  

6  

5   512  

7   729  

0   1   8   27   64   125   216   343  

0  

1  

2  

3  

Page 131: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

8  

9  

4  

6  

5   512  

7   729  

0   1   8   27   64   125   216   343   512  

0  

1  

2  

3  

Page 132: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

8  

9  

4  

6  

7   729  

0   1   8   27   64   125   216   343   512  

0  

1  

2  

3  

5  

Page 133: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

8  

9  

4  

6  

7   729  

0   1   8   27   64   125   216   343   512   729  

0  

1  

2  

3  

5  

Page 134: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

January  9,  2015   www.gowrikumar.com  

8  

9  

4  

6  

0   1   8   27   64   125   216   343   512   729  

0  

1  

2  

3  

5  

7  

Page 135: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

What  is  the  @me  Complexity?

• O(k n). • O( k log  n).  • O((  k + n) log(k  +  n)).  • O(k + n).

Page 136: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Ques@on:

Why  use  Radixsort?  

Page 137: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

•  006,  081,  016,  572,  023,  999,  040,  101,  043,  425  

Exercise,  sort  number  using  bucketsort

Page 138: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Quick  sort

• Pick an element from the array, called the pivot and partition the array in two parts:

•  First come all the elements smaller than the pivot, then the pivot, then all the elements greater than the pivot

• Recursively quicksort the two partitions

Page 139: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Quick  sort

Say  the  pivot  is  5.  Par::on  the  array  into:  all  elements  less  than  5,    then  all  elements  greater  than  5  

 

Page 140: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Quick  sort

Now  recursively  quicksort  the  two  par::ons!  

Page 141: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Quicksort  Algorithm

Given  an  array  of  n  elements  (e.g.,  integers):  •  If  array  only  contains  one  element,  return  •  Else  

•  pick  one  element  to  use  as  pivot.  •  Par::on  elements  into  two  sub-­‐arrays:  

•  Elements  less  than  or  equal  to  pivot  •  Elements  greater  than  pivot  

•  Quicksort  two  sub-­‐arrays  •  Return  results  

Page 142: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Example

We  are  given  array  of  n  integers  to  sort:  

40 20 10 80 60 50 7 30 100

Page 143: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Pick  Pivot  Element There  are  a  number  of  ways  to  pick  the  pivot  element.    In  this  example,  we  will  use  the  first  element  in  the  array:  

40 20 10 80 60 50 7 30 100

Page 144: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Par@@oning  Array

Given  a  pivot,  par::on  the  elements  of  the  array  such  that  the  resul:ng  array  consists  of:    1.  One  sub-­‐array  that  contains  elements  >=  pivot    2.  Another  sub-­‐array  that  contains  elements  <  pivot  

The  sub-­‐arrays  are  stored  in  the  original  data  array.        Par::oning  loops  through,  swapping  elements  

below/above  pivot.  

Page 145: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

40 20 10 80 60 50 7 30 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 146: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

40 20 10 80 60 50 7 30 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1.  While data[too_big_index] <= data[pivot] ++too_big_index

Page 147: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

40 20 10 80 60 50 7 30 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1.  While data[too_big_index] <= data[pivot] ++too_big_index

Page 148: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

40 20 10 80 60 50 7 30 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1.  While data[too_big_index] <= data[pivot] ++too_big_index

Page 149: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

40 20 10 80 60 50 7 30 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

Page 150: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

40 20 10 80 60 50 7 30 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

Page 151: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

40 20 10 80 60 50 7 30 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

Page 152: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

40 20 10 30 60 50 7 80 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

Page 153: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

40 20 10 30 60 50 7 80 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, repeat 1.

Page 154: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

40 20 10 30 60 50 7 80 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, repeat1.

Page 155: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

40 20 10 30 60 50 7 80 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1.

Page 156: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

40 20 10 30 60 50 7 80 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1.

Page 157: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

40 20 10 30 60 50 7 80 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1.

Page 158: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

40 20 10 30 60 50 7 80 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1.

Page 159: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 160: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 161: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 162: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 163: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 164: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 165: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 166: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 167: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1.

40 20 10 30 7 50 60 80 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 168: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1. 5.  Swap data[too_small_index] and data[pivot_index]

40 20 10 30 7 50 60 80 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 169: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1. 5.  Swap data[too_small_index] and data[pivot_index]

7 20 10 30 40 50 60 80 100 pivot_index = 4

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 170: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Par@@on  Result

7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]

Page 171: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Recursion:  Quicksort  Sub-­‐arrays

7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]

Page 172: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Quicksort  Analysis

• Assume  that  keys  are  random,  uniformly  distributed.  • What  is  best  case  running  :me?  

Page 173: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Quicksort  Analysis

• Assume  that  keys  are  random,  uniformly  distributed.  • What  is  best  case  running  :me?  

•  Recursion:  1.  Par::on  splits  array  in  two  sub-­‐arrays  of  size  n/2    2.  Quicksort  each  sub-­‐array  

Page 174: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Quicksort  Analysis

•  Assume  that  keys  are  random,  uniformly  distributed.  •  What  is  best  case  running  :me?  

•  Recursion:  1.  Par::on  splits  array  in  two  sub-­‐arrays  of  size  n/2    2.  Quicksort  each  sub-­‐array  

•  Depth  of  recursion  tree?    

Page 175: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Quicksort  Analysis

•  Assume  that  keys  are  random,  uniformly  distributed.  •  What  is  best  case  running  :me?  

•  Recursion:  1.  Par::on  splits  array  in  two  sub-­‐arrays  of  size  n/2    2.  Quicksort  each  sub-­‐array  

•  Depth  of  recursion  tree?  O(log2n)  

Page 176: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Quicksort  Analysis

•  Assume  that  keys  are  random,  uniformly  distributed.  •  What  is  best  case  running  :me?  

•  Recursion:  1.  Par::on  splits  array  in  two  sub-­‐arrays  of  size  n/2    2.  Quicksort  each  sub-­‐array  

•  Depth  of  recursion  tree?  O(log2n)  •  Number  of  accesses  in  par::on?  

Page 177: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Quicksort  Analysis

•  Assume  that  keys  are  random,  uniformly  distributed.  •  What  is  best  case  running  :me?  

•  Recursion:  1.  Par::on  splits  array  in  two  sub-­‐arrays  of  size  n/2    2.  Quicksort  each  sub-­‐array  

•  Depth  of  recursion  tree?  O(log2n)  •  Number  of  accesses  in  par::on?  O(n)  

Page 178: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Quicksort  Analysis

•  Assume  that  keys  are  random,  uniformly  distributed.  •  Best  case  running  :me:  O(n  log2n)    

Page 179: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Quicksort  Analysis

•  Assume  that  keys  are  random,  uniformly  distributed.  •  Best  case  running  :me:  O(n  log2n)  •  Worst  case  running  :me?    

Page 180: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Quicksort:  Worst  Case

• Assume  first  element  is  chosen  as  pivot.  • Assume  we  get  array  that  is  already  in  order:    

2 4 10 12 13 50 57 63 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 181: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1. 5.  Swap data[too_small_index] and data[pivot_index]

2 4 10 12 13 50 57 63 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 182: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1. 5.  Swap data[too_small_index] and data[pivot_index]

2 4 10 12 13 50 57 63 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 183: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1. 5.  Swap data[too_small_index] and data[pivot_index]

2 4 10 12 13 50 57 63 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 184: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1. 5.  Swap data[too_small_index] and data[pivot_index]

2 4 10 12 13 50 57 63 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 185: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1. 5.  Swap data[too_small_index] and data[pivot_index]

2 4 10 12 13 50 57 63 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 186: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1. 5.  Swap data[too_small_index] and data[pivot_index]

2 4 10 12 13 50 57 63 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index too_small_index

Page 187: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

1.  While data[too_big_index] <= data[pivot] ++too_big_index

2.  While data[too_small_index] > data[pivot] --too_small_index

3.  If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]

4.  While too_small_index > too_big_index, go to 1. 5.  Swap data[too_small_index] and data[pivot_index]

2 4 10 12 13 50 57 63 100 pivot_index = 0

[0] [1] [2] [3] [4] [5] [6] [7] [8]

> data[pivot] <= data[pivot]

Page 188: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Quicksort  Analysis

•  Assume  that  keys  are  random,  uniformly  distributed.  •  Best  case  running  :me:  O(n  log2n)  •  Worst  case  running  :me?  

•  Recursion:  1.  Par::on  splits  array  in  two  sub-­‐arrays:  

•  one  sub-­‐array  of  size  0  •  the  other  sub-­‐array  of  size  n-­‐1  

2.  Quicksort  each  sub-­‐array  •  Depth  of  recursion  tree?    

 

Page 189: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Quicksort  Analysis

•  Assume  that  keys  are  random,  uniformly  distributed.  •  Best  case  running  :me:  O(n  log2n)  •  Worst  case  running  :me?  

•  Recursion:  1.  Par::on  splits  array  in  two  sub-­‐arrays:  

•  one  sub-­‐array  of  size  0  •  the  other  sub-­‐array  of  size  n-­‐1  

2.  Quicksort  each  sub-­‐array  •  Depth  of  recursion  tree?  O(n)  

 

Page 190: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Quicksort  Analysis

•  Assume  that  keys  are  random,  uniformly  distributed.  •  Best  case  running  :me:  O(n  log2n)  •  Worst  case  running  :me?  

•  Recursion:  1.  Par::on  splits  array  in  two  sub-­‐arrays:  

•  one  sub-­‐array  of  size  0  •  the  other  sub-­‐array  of  size  n-­‐1  

2.  Quicksort  each  sub-­‐array  •  Depth  of  recursion  tree?  O(n)  •  Number  of  accesses  per  par::on?    

 

Page 191: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Quicksort  Analysis

•  Assume  that  keys  are  random,  uniformly  distributed.  •  Best  case  running  :me:  O(n  log2n)  •  Worst  case  running  :me?  

•  Recursion:  1.  Par::on  splits  array  in  two  sub-­‐arrays:  

•  one  sub-­‐array  of  size  0  •  the  other  sub-­‐array  of  size  n-­‐1  

2.  Quicksort  each  sub-­‐array  •  Depth  of  recursion  tree?  O(n)  •  Number  of  accesses  per  par::on?  O(n)  

 

Page 192: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Quicksort  Analysis

•  Assume  that  keys  are  random,  uniformly  distributed.  •  Best  case  running  :me:  O(n  log2n)  •  Worst  case  running  :me:  O(n2)!!!  

 

Page 193: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Quicksort  Analysis

•  Assume  that  keys  are  random,  uniformly  distributed.  •  Best  case  running  :me:  O(n  log2n)  •  Worst  case  running  :me:  O(n2)!!!  •  What  can  we  do  to  avoid  worst  case?  

 

Page 194: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Improved  Pivot  Selec@on

Pick  median  value  of  three  elements  from  data  array:    data[0],  data[n/2],  and  data[n-­‐1].  

 Use  this  median  value  as  pivot.  

Page 195: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Ques@on:

 • Heapsort  has  a  worst  case  of  O(n  log  n)  • Quick  sort  has  O(n2)    Should’nt  it  be  beNer  to  use  Heapsort  over  Quicksort?        

Page 196: Datastructures) Lecture11Bubble up • Given(anode(thatdoes(nothave(the(heap(property,(you(can(give(itthe(heap(property(by(exchanging(its(value(with(the(value(of(the(larger(child(•

Answer:

• One  of  the  major  factors  is  that  quicksort  has  beNer  locality  of  reference.  Sta:s:cly  the  next  data  to  be  accessed  is  usually  close  in  memory  to  the  data  you  just  looked  at.  Data  that  has  god  locality  is  more  likely  be  cached  together,  and  therefore  quicksort  tends  to  be  faster.  

• On  the  other  hand,  if  you  want  to  perform  good  even  in  worst  case  then  heapsort  is  a  beNer  choice.  This  give  a  beNer  control  of  keeping  cri:cal  deadlines.