SlideShare a Scribd company logo
Data structures
 & algorithms
  Basics: Part I
  By Daniel Gomez-Prado
        Sept 2012

      Disclaimer: This tutorial may contain
      errors, use it at your own discretion.

      The slides were prepared for a class
       review on basic data structures at
     University of Massachusetts, Amherst.

           http://www.dgomezpr.com
Outline
•   Analysis of complexity
•   Classes, objects and containers
•   Array
•   Stack
•   Queue
•   (Single) Linked and Double Linked List
•   Iterators (overview)
•   Linear and Binary search
•   Merge sort
•   Quick sort

                                             2
Analysis big-Oh
 Random access memory
              Your program                  Memory

                 Import java.util.*
                 class review {
                   Static public main() {
                     // this is a review
                     // for ECE242 exam
                   }
                 }




• Assumptions:
  o Unlimited memory

  o All memory accesses takes 1 unit time


                                                     3
Analysis big-Oh
 Random access memory
              Your program                                 Memory

                 Import java.util.*
                 class review {
                   Static public main() {
                     // this is a review
                     // for ECE242 exam
                   }
                 }




• Assumptions:
  o Unlimited memory                        We have what we need: 1 Gb or 1,000 Tb


  o All memory accesses takes 1 unit time


                                                                                     4
Analysis big-Oh
 Random access memory
              Your program                                 Memory

                 Import java.util.*
                 class review {
                   Static public main() {
                     // this is a review
                     // for ECE242 exam
                   }
                 }




• Assumptions:
  o Unlimited memory                        We have what we need: 1 Gb or 1,000 Tb

                                                                No hierarchical memory,
  o All memory accesses takes 1 unit time
                                                                Cache, L1, L2, hard drive

                                                                                      5
Analysis big-Oh
                Running time
PROGRAM                        OPERATIONS                 STEPS
int sum = 0;                   1 assignment               1
for (i=0;i<128;i=++)           i =1, 2, 3, 4 … 128        128
 for (j = 128; j>0; j=j/2)     j = 128,64,32,16,8,4,2,1   log2128+1
        sum = sum + a[i][j];   1 addition, 1 assignment   2




                                                                6
Analysis big-Oh
                Running time
PROGRAM                        OPERATIONS                         STEPS
int sum = 0;                   1 assignment                       1
for (i=0;i<128;i=++)           i =1, 2, 3, 4 … 128                128
 for (j = 128; j>0; j=j/2)     j = 128,64,32,16,8,4,2,1           log2128+1
        sum = sum + a[i][j];   1 addition, 1 assignment           2
                                                          1+128*(log2128+1)*2




                                                                         7
Analysis big-Oh
                Running time
PROGRAM                             OPERATIONS                          STEPS
int sum = 0;                        1 assignment                        1
for (i=0;i<128;i=++)                i =1, 2, 3, 4 … 128                 128
 for (j = 128; j>0; j=j/2)          j = 128,64,32,16,8,4,2,1            log2128+1
        sum = sum + a[i][j];        1 addition, 1 assignment            2
                                                               1+128*(log2128+1)*2


 In general we have an arbitrary number “n” instead of 128, in that case:

                             1+n*(log2n+1)*2
                             1+2n+2n*log2n


                                                                              8
Analysis big-Oh
                 Running time
PROGRAM                               OPERATIONS                            STEPS
int sum = 0;                          1 assignment                          1
for (i=0;i<128;i=++)                  i =1, 2, 3, 4 … 128                   128
 for (j = 128; j>0; j=j/2)            j = 128,64,32,16,8,4,2,1              log2128+1
         sum = sum + a[i][j];         1 addition, 1 assignment              2
                                                                   1+128*(log2128+1)*2

 n could be the size of the stack, queue, list or the dimension of a matrix, etc.
 In general we have an arbitrary number “n” instead of 128, in that case:

                             1+n*(log2n+1)*2        can we simplify the expression?
                             1+2n+2n*log2n          YES!, By using big-Oh notation
                                                    we can specify the asymptotic
                                                    complexity of the algorithm
                                                                                    9
Analysis big-Oh
             Definition
• Given functions f(n) and g(n):

  o f(n) is said to be O(g(n))

  o if and only if
       • there are (exist) 2 positive constants, C>0 and N>0

  o such that
     • f(n) ≤ Cg(n)     for every n>N




                                                               10
Analysis big-Oh
Example of definition usage




                              11
Analysis big-Oh
Example of definition usage




                  Relationship between C & n




                                        12
Analysis big-Oh
Example of definition usage




                  Relationship between C & n




                                        13
Analysis big-Oh
Example of definition usage




                  Relationship between C & n




                  O(n*log2n) is true
                  for C=3 and n≥32      14
Analysis big-Oh
Example of definition usage
       keywords




                  Relationship between C & n




                  O(n*log2n) is true
                  for C=3 and n≥32      15
Analysis big-Oh
Example of definition usage
             keywords




   f(n) is given    g(n) is given




                                    Relationship between C & n




                                    O(n*log2n) is true
                                    for C=3 and n≥32      16
Analysis big-Oh
              Example 1
State the asymptotic complexity of:

i.   Print out middle element of an array of size n




                                                      17
Analysis big-Oh
              Example 1
State the asymptotic complexity of:
                    big-Oh
i.   Print out middle element of an array of size n




                                                      18
Analysis big-Oh
              Example 1
State the asymptotic complexity of:
                    big-Oh
i.   Print out middle element of an array of size n
                                   arrays allow access to
                                   any position randomly




                                                            19
Analysis big-Oh
              Example 1
State the asymptotic complexity of:
                    big-Oh
i.   Print out middle element of an array of size n
                                   arrays allow access to
                                   any position randomly




                                                            20
Analysis big-Oh
                  Example 1
State the asymptotic complexity of:
                               big-Oh
i.   Print out middle element of an array of size n
                                           arrays allow access to
                                           any position randomly

       recall
                Your program      Memory




      o All memory accesses takes 1
        unit time

                                                                    21
Analysis big-Oh
                  Example 1
State the asymptotic complexity of:
                               big-Oh
i.   Print out middle element of an array of size n
                                           arrays allow access to
                                           any position randomly

       recall
                Your program      Memory




                                                 Solution is:

                                                          O(1)
      o All memory accesses takes 1
        unit time

                                                                    22
Analysis big-Oh
               Example 1
ii.   Print out the middle element of a linked list of size n




                                                                23
Analysis big-Oh
               Example 1
ii.   Print out the middle element of a linked list of size n
                                         recall a linked list




                                                                24
Analysis big-Oh
                        Example 1
ii.       Print out the middle element of a linked list of size n



      head                                            tail
               next            next            next            next



      object          object          object          object




                                                                      25
Analysis big-Oh
                        Example 1
ii.       Print out the middle element of a linked list of size n



      head                                            tail
               next            next            next            next



      object          object          object          object




                                                                      26
Analysis big-Oh
                        Example 1
ii.       Print out the middle element of a linked list of size n



      head                                                        tail
               next            next            next                        next

                                                         n/2
      object          object          object          memory      object
                                                      locations




                                                                                  27
Analysis big-Oh
                        Example 1
ii.       Print out the middle element of a linked list of size n



      head                                                        tail
               next            next            next                        next

                                                         n/2
      object          object          object          memory      object
                                                      locations




                                                                                  28
Analysis big-Oh
                        Example 1
ii.       Print out the middle element of a linked list of size n



      head                                                              tail
               next            next               next                           next

                                                            n/2
      object          object             object          memory         object
                                                         locations




                                                         f(n) = n/2
                                      Solution is: the asymptotic complexity is O(n)
                                                                                        29
Analysis big-Oh
             Example 1
iii. Print out the odd elements of an array of size n
             f(n) = n/2
             Solution: the asymptotic complexity is O(n)


iv. Pop 10 elements from a stack that is implemented
    with an array. Assume that the stacks contains n
    elements and n > 10.

             When in doubt, ASK! is n = 11 or is n > 1000 ?

             f(n) = 10
             Solution: the asymptotic complexity is O(1)


                                                              30
Classes and
                        objects
• The goal of a “class” (in object-oriented language)
   o Encapsulate state and behavior

• A class is a blueprint that has
   o   a constructor to initialize its data members
   o   a destructor to tear down the object
   o   A coherent interface to interact with the object (public methods)
   o   Private methods unreachable from the outside
   o   The possibility to extend and inherit members from other classes

• An object is an instant of a class
• What are the benefits:
   o Through inheritance, extensions, packages allows to structure a program
   o Exposes behavior that could be reused
   o Alleviates the problem of understanding somebody else code


                                                                               31
ADT
     (Abstract Data Type)
• ADTs are containers

• ADTs are primarily concern in:
  o Aggregation of data
  o Access of data

  o Efficiency of memory is used
  o Efficiency of the container access




                                         32
Arrays
• Contiguous blocks of memory of a data type
  o Any position can be randomly access



• Example
  o Int[] integer_array = new int[1024];

              0                              1023



  o ObjectY[] object_y_array = new ObjectY[512];




                                                    33
Arrays
• Contiguous blocks of memory of a data type
  o Any position can be randomly access



• Example
  o Int[] integer_array = new int[1024];
                           int size
              0                              1023



  o ObjectY[] object_y_array = new ObjectY[512];




                                                    34
Arrays
• Contiguous blocks of memory of a data type
  o Any position can be randomly access



• Example
  o Int[] integer_array = new int[1024];

              0                              1023



  o ObjectY[] object_y_array = new ObjectY[512];


              0                                     511



                                                          35
Arrays
• Contiguous blocks of memory of a data type
  o Any position can be randomly access



• Example
  o Int[] integer_array = new int[1024];

              0                                   1023



  o ObjectY[] object_y_array = new ObjectY[512];

                                   ObjectY size
              0                                          511



                                                               36
Arrays
• Contiguous blocks of memory of a data type
  o Any position can be randomly access



• Example
  o Int[] integer_array = new int[1024];

              0                                   1023
                                                         In Java memory
                                                         management is
  o ObjectY[] object_y_array = new ObjectY[512];         taken care for you.

                                   ObjectY size
              0                                          511



                                                                               37
Arrays
• Contiguous blocks of memory of a data type
   o Any position can be randomly access



• Example
   o Int[] integer_array = new int[1024];

               0                              1023



   o ObjectY[] object_y_array = new ObjectY[512];
    1,934,218
ObjectX       0                                      511 0   ObjectZ
 …                                                                …


                                                                       38
Arrays
• Contiguous blocks of memory of a data type
   o Any position can be randomly access



• Example
   o Int[] integer_array = new int[1024];

               0                              1023



   o ObjectY[] object_y_array = new ObjectY[512];
    1,934,218
ObjectX       0                                      511 0   ObjectZ
 …                                                                …


                                                                       39
Arrays
• Contiguous blocks of memory of a data type
   o Any position can be randomly access



• Example
   o Int[] integer_array = new int[1024];

               0                                1023



   o ObjectY[] object_y_array = new ObjectY[512];
    1,934,218
ObjectX       0                                        511 0   ObjectZ
 …                                                                  …
                                 512 ObjectsY
                               fixed boundary                            40
Stacks
• Enforce a LIFO behavior (last in, first out)
   o It is based on an array
   o It overrides the random access of an array by a LIFO access


        1023




           0                                                       41
Stacks
• Enforce a LIFO behavior (last in, first out)
   o It is based on an array
   o It overrides the random access of an array by a LIFO access


        1023                    1023




                             peek          push




                                           push
           0                        0      push                    42
Stacks
• Enforce a LIFO behavior (last in, first out)
   o It is based on an array
   o It overrides the random access of an array by a LIFO access


        1023                    1023                   1023




                             peek          push                    pop



                                                                   pop
                                                     peek

                                           push
           0                        0      push             0            43
Stacks
• Enforce a LIFO behavior (last in, first out)
   o It is based on an array
   o It overrides the random access of an array by a LIFO access


          1023                  1023                   1023
isEmpty                 isEmpty                   isEmpty
 true                    false                     false

                             peek          push                    pop



                                                                   pop
                                                     peek

                                           push
            0                       0      push             0            44
Stacks
• Enforce a LIFO behavior (last in, first out)
   o It is based on an array
   o It overrides the random access of an array by a LIFO access


          1023                  1023                   1023
isEmpty                 isEmpty                   isEmpty
 true                    false                     false

                             peek          push                    pop



                                                                   pop
                                                     peek            index

                                           push
            0                       0      push             0                45
Stacks
• Enforce a LIFO behavior (last in, first out)
   o It is based on an array
   o It overrides the random access of an array by a LIFO access


          1023                  1023                   1023
isEmpty                 isEmpty                   isEmpty
 true                    false                     false

                             peek          push                    pop



                                                                   pop
                                                     peek            index

                                           push
            0                       0      push             0                46
Stacks
• Enforce a LIFO behavior (last in, first out)
   o It is based on an array
   o It overrides the random access of an array by a LIFO access


          1023                  1023                   1023
isEmpty                 isEmpty                   isEmpty
 true                    false                     false

                             peek          push                    pop



                                                                   pop status
                                                     peek            index

                                           push
            0                       0      push             0             47
Stacks
• Enforce a LIFO behavior (last in, first out)
   o It is based on an array
   o It overrides the random access of an array by a LIFO access
                                                                     1024
                                                       1023
Recall what a class encapsulates                 isEmpty
o Status &                                        false
o Behavior
                                                                      pop




                                                     peek                   index



                                                            0                       48
                                                                -1
Stacks
• Enforce a LIFO behavior (last in, first out)
   o It is based on an array
   o It overrides the random access of an array by a LIFO access
                                                                     1024
                                                       1023
Recall what a class encapsulates                 isEmpty
o Status &                                        false
o Behavior
                                                                      pop
Does it mean we are always safe
o index = -1,
    stack is empty, good
o index = 1024,                                      peek                   index
    o refuse to push objects
    o overflow, runtime exception

                                                            0                       49
                                                                -1
Stacks
• Enforce a LIFO behavior (last in, first out)
   o It is based on an array
   o It overrides the random access of an array by a LIFO access
                                                                     1024
                                                       1023
Recall what a class encapsulates                 isEmpty
o Status &                                        false
o Behavior
                                                                      pop
Does it mean we are always safe
o index = -1,
    stack is empty, good
o index = 1024,                                      peek                   index
    o refuse to push objects
    o overflow, runtime exception

                                                            0                       50
                                                                -1
Stacks




                                                                …
                                                                     ObjectZ
• Enforce a LIFO behavior (last in, first out)
   o It is based on an array
   o It overrides the random access of an array by a LIFO access
                                                         0           1024
                                                       1023
Recall what a class encapsulates                 isEmpty
o Status &                                        false
o Behavior
                                                                      pop
Does it mean we are always safe
o index = -1,
    stack is empty, good
o index = 1024,                                      peek                   index
    o refuse to push objects
    o overflow, runtime exception

                                                            0                       51
                                                                -1
Queues
• Enforce a FIFO behavior (first in, first out)
   o It is based on an array
   o It overrides the random access of an array by a FIFO access


        1023




           0                                                       52
Queues
• Enforce a FIFO behavior (first in, first out)
   o It is based on an array
   o It overrides the random access of an array by a FIFO access


        1023                    1023




                             peek          enqueue




                                           enqueue
           0                        0      enqueue                 53
Queues
• Enforce a FIFO behavior (first in, first out)
   o It is based on an array
   o It overrides the random access of an array by a FIFO access


        1023                    1023                   1023




                             peek          enqueue peek




                                                                   dequeue

                                           enqueue
           0                        0      enqueue        0        dequeue   54
Queues
• Enforce a FIFO behavior (first in, first out)
   o It is based on an array
   o It overrides the random access of an array by a FIFO access


          1023                  1023                   1023
isEmpty                 isEmpty                  isEmpty
 true                    false                    false

                             peek          enqueue peek




                                                                   dequeue

                                           enqueue
            0                       0      enqueue         0       dequeue   55
Queues
• Enforce a FIFO behavior (first in, first out)
   o It is based on an array
   o It overrides the random access of an array by a FIFO access


          1023                  1023                   1023
isEmpty                 isEmpty                  isEmpty
 true                    false                    false              status

                             peek          enqueue peek              index1



                                                                     index2
                                                                   dequeue

                                           enqueue
            0                       0      enqueue         0       dequeue    56
Queues
• Enforce a FIFO behavior (first in, first out)
   o It is based on an array
   o It overrides the random access of an array by a FIFO access

                                                       1023
Recall what a class encapsulates                 isEmpty
o Status &                                        false              status
o Behavior
                                                     peek            index1



                                                                     index2
                                                                   dequeue



                                                            0      dequeue    57
Queues
• Enforce a FIFO behavior (first in, first out)
   o It is based on an array
   o It overrides the random access of an array by a FIFO access

                                                       1023
Recall what a class encapsulates                 isEmpty
o Status &                                        false              status
o Behavior
                                                     peek            index1
Does it mean we are always safe
o index1 = index2,
    stack is empty, good                                             index2
o index1 or index2 = 1024,                                         dequeue
    o rewind to 0
         o test condition
         o Increment using mod 1024
o What if index2 > index1                                   0      dequeue    58
Queues
• Enforce a FIFO behavior (first in, first out)
   o It is based on an array
   o It overrides the random access of an array by a FIFO access

                                                       1023
Recall what a class encapsulates                 isEmpty
o Status &                                        false              status
o Behavior
                                                     peek            index1
Does it mean we are always safe                                        >
o index1 = index2,
    stack is empty, good                                             index2
o index1 or index2 = 1024,                                         dequeue
    o rewind to 0
         o test condition
         o Increment using mod 1024
o What if index2 > index1                                   0      dequeue    59
Queues
• Enforce a FIFO behavior (first in, first out)
   o It is based on an array
   o It overrides the random access of an array by a FIFO access

          1023                                         1023
                        status
                        index2                                       status

                                                     peek            index1
                          >                                            >
                                                                     index2
        peek            index1                                     dequeue


               0                                            0      dequeue    60
Is everything an Array?
 can we use something else?
                                    beginning                     end
• Recall an array
   o Contiguous memory        ObjectX        0
                                                   ObjectY         N-1   0   ObjectZ
                               …                                                   …

   o Fixed bound size
                                                 fixed boundary
   o Random access


• Let’s use another construct
   o Non contiguous memory
   o Unlimited size                ObjectX              ObjectY                 ObjectZ
                              …
   o Sequential access only
                                                                                            …




                                                                                       61
Is everything an Array?
 can we use something else?
                                    beginning                     end
• Recall an array
   o Contiguous memory        ObjectX        0
                                                   ObjectY         N-1   0   ObjectZ
                               …                                                   …

   o Fixed bound size
                                                 fixed boundary
   o Random access


• Let’s use another construct
   o Non contiguous memory
   o Unlimited size                ObjectX              ObjectY                 ObjectZ
                              …
   o Sequential access only
                                                                                            …




                                                                                       62
Is everything an Array?
 can we use something else?
                                    beginning                          end
• Recall an array
   o Contiguous memory        ObjectX        0
                                                        ObjectY         N-1   0   ObjectZ
                               …                                                        …

   o Fixed bound size
                                                      fixed boundary
   o Random access
                                                 How do we know in this container?
• Let’s use another construct                    o the beginning, the end
                                                 o which element is next
   o Non contiguous memory
   o Unlimited size                ObjectX                   ObjectY                 ObjectZ
                              …
   o Sequential access only
                                                                                                 …




                                                                                            63
Is everything an Array?
 can we use something else?
                                    beginning                          end
• Recall an array
   o Contiguous memory        ObjectX        0
                                                        ObjectY         N-1   0   ObjectZ
                               …                                                        …

   o Fixed bound size
                                                      fixed boundary
   o Random access
                                                 How do we know in this container?
• Let’s use another construct                    o the beginning, the end
                                                 o which element is next
   o Non contiguous memory
   o Unlimited size                ObjectX                   ObjectY                 ObjectZ
                              …
   o Sequential access only
                                                                                                 …




                                                                                            64
Is everything an Array?
 can we use something else?
                                    beginning                          end
• Recall an array
   o Contiguous memory        ObjectX        0
                                                        ObjectY         N-1   0   ObjectZ
                               …                                                        …

   o Fixed bound size
                                                      fixed boundary
   o Random access
                                                 How do we know in this container?
• Let’s use another construct                    o the beginning, the end
                                                 o which element is next
   o Non contiguous memory
   o Unlimited size                ObjectX                   ObjectY                 ObjectZ
                              …
   o Sequential access only
                                                                                                 …



                                    head                  next




                                                                                            65
Is everything an Array?
 can we use something else?
                                    beginning                            end
• Recall an array
   o Contiguous memory        ObjectX        0
                                                         ObjectY          N-1   0   ObjectZ
                               …                                                          …

   o Fixed bound size
                                                      fixed boundary
   o Random access
                                                 How do we know in this container?
• Let’s use another construct                    o the beginning, the end
                                                 o which element is next
   o Non contiguous memory
   o Unlimited size                ObjectX                     ObjectY                 ObjectZ
                              …
   o Sequential access only
                                                                                                   …



                                    head                  next


                                                                  next



                                                      object

                                                                                              66
Is everything an Array?
 can we use something else?
                                    beginning                            end
• Recall an array
   o Contiguous memory        ObjectX        0
                                                         ObjectY          N-1   0   ObjectZ
                               …                                                          …

   o Fixed bound size
                                                      fixed boundary
   o Random access
                                                 How do we know in this container?
• Let’s use another construct                    o the beginning, the end
                                                 o which element is next
   o Non contiguous memory
   o Unlimited size                ObjectX                     ObjectY                 ObjectZ
                              …
   o Sequential access only
                                                                                                   …



                                    head                  next


                                                                  next

                                                                         head
                                                      object

                                                                                              67
Is everything an Array?
 can we use something else?
                                    beginning                              end
• Recall an array
   o Contiguous memory        ObjectX        0
                                                           ObjectY          N-1   0   ObjectZ
                               …                                                            …

   o Fixed bound size
                                                        fixed boundary
   o Random access
                                                 How do we know in this container?
• Let’s use another construct                    o the beginning, the end
                                                 o which element is next
   o Non contiguous memory
   o Unlimited size                ObjectX                       ObjectY                 ObjectZ
                              …
   o Sequential access only
                                                                                                     …



                                    head                    next


                                                 prev               next

                                                                           head
                                                        object

                                                                                                68
Is everything an Array?
 can we use something else?
                                    beginning                              end
• Recall an array
   o Contiguous memory        ObjectX        0
                                                           ObjectY          N-1   0   ObjectZ
                               …                                                            …

   o Fixed bound size
                                                        fixed boundary
   o Random access
                                                 How do we know in this container?
• Let’s use another construct                    o the beginning, the end
                                                 o which element is next
   o Non contiguous memory
   o Unlimited size                ObjectX                       ObjectY                 ObjectZ
                              …
   o Sequential access only
                                                                                                     …



                                    head                    next


                                                 prev               next

                                             Node                          head
                                                        object

                                                                                                69
Is everything an Array?
 can we use something else?
                                     beginning                              end
• Recall an array
   o Contiguous memory         ObjectX        0
                                                            ObjectY          N-1   0   ObjectZ
                                …                                                            …

   o Fixed bound size
                                                         fixed boundary
   o Random access
                                                  How do we know in this container?
• Let’s use another construct                     o the beginning, the end
                                                  o which element is next
   o Non contiguous memory
   o Unlimited size                 ObjectX                       ObjectY                 ObjectZ
                               …
   o Sequential access only
                                                                                                      …



                                  head                       next
                              edges

                                                  prev               next

                                              Node                          head
                                                         object

                                                                                                 70
Linked List
• Use the prior construct (node and edge)
 head
        next            next            next                next


                                               …
               object          object              object




                                                                   71
Linked List
• Use the prior construct (node and edge)
 head
        next            next            next                next


                                               …
               object          object              object




   push                 next



               object




                                                                   72
Linked List
• Use the prior construct (node and edge)
 head
        next            next            next                next


                                               …
               object          object              object




   push                 next



               object




                                                                   73
Linked List
• Use the prior construct (node and edge)
 head
        next            next            next                next


                                               …
               object          object              object




   push                 next



               object




                                                                   74
Linked List
• Use the prior construct (node and edge)
 head
        next            next            next                next


                                               …
               object          object              object




                        next
   pop


               object




                                                                   75
Linked List
• Use the prior construct (node and edge)
 head
        next            next            next                next


                                               …
               object          object              object




                        next
   pop


               object




                                                                   76
Linked List
• Use the prior construct (node and edge)
 head
        next            next            next                next


                                               …
               object          object              object




                        next
   pop


               object




                                                                   77
Linked List
• Use the prior construct (node and edge)
 head
        next            next            next                next


                                               …
               object          object              object




                        next
   pop


               object




                                                                   78
Linked List
• Use the prior construct (node and edge)
 head
        next            next            next                next


                                               …
               object          object              object




   pop




                                                                   79
Linked List
• Use the prior construct (node and edge)
 head
        next            next            next                next


                                               …
               object          object              object




   peek




                                                                   80
Linked List
• Use the prior construct (node and edge)
 head
        next            next            next                next


                                               …
               object          object              object




   peek




                                                                   81
Double linked List
   • Use the prior construct (node and edge)



       head
prev          next   prev            next   prev            next …   prev            next



                            object                 object                   object




                                                                                     82
Quick Questions
Can we do:

•   a linked list or double linked list from an array
    o Yes


• a queue with nodes and edges
    o Why not.



• a stack with nodes and edges
    o Sure




                                                        83
Iterators encapsulate
            container traversals
• we have two implementations of a stack


 1023




                     pop                     prev       next



                                             prev       next

                               head
                               prev   next   prev   4   next
peek                   index
           1 2 3 4




                                             prev   3   next


                                             prev   2   next
       0             push
                                             prev   1   next   84
Iterators encapsulate
            container traversals
• we have two implementations of a stack


 1023




                     pop                     prev       next



                                             prev       next

                               head
                               prev   next   prev   4   next
peek                   index
           1 2 3 4




                                             prev   3   next


                                             prev   2   next
       0             push
                                             prev   1   next   85
Iterators encapsulate
            container traversals
• we have two implementations of a stack


 1023



                               peek push pop
                     pop                           prev       next



                                                   prev       next

                                     head
                                    prev    next   prev   4   next
peek                   index
           1 2 3 4




                                                   prev   3   next


                                                   prev   2   next
       0             push
                                                   prev   1   next   86
Iterators encapsulate
            container traversals
• we have two implementations of a stack


 1023




                                               prev       next


                                 head
                                 prev   next   prev   5   next

peek                     index
           1 2 3 4 5




                                               prev   4   next


                                               prev   3   next


                                               prev   2   next
       0               push
                                               prev   1   next   87
Iterators encapsulate
            container traversals
• we have two implementations of a stack


 1023



                                          update next
                                          update prev     prev       next


                         increment by 1     head
                         decrement by 1     prev   next   prev   5   next

peek                     index
           1 2 3 4 5




                                                          prev   4   next


                                                          prev   3   next


                                                          prev   2   next
       0               push
                                                          prev   1   next   88
Iterators encapsulate
            container traversals
• we have two implementations of a stack

                                   Traverse container
 1023                         according to container rules



                                                  update next
                                                  update prev       prev       next


                         increment by 1             head
                         decrement by 1              prev    next   prev   5   next

peek                     index
           1 2 3 4 5




                                                                    prev   4   next


                                                                    prev   3   next


                                                                    prev   2   next
       0               push
                                                                    prev   1   next   89
Iterators encapsulate
            container traversals
• we have two implementations of a stack

                                     Traverse container      behavior
 1023                           according to container rules

                              state

                                                   update next
                                                   update prev      prev       next


                         increment by 1              head
                         decrement by 1               prev   next   prev   5   next

peek                     index
           1 2 3 4 5




                                                                    prev   4   next


                                                                    prev   3   next


                                                                    prev   2   next
       0               push
                                                                    prev   1   next   90
Searching
             Linear vs Binary
• If you make no assumptions
   o iterate (traverse) all elements to find an existing element
   o iterate (traverse) all elements to realize you don’t have an element


                  a x z b n m l         j   i b c u

• If you assume the container is already order
  (according to certain rule)
   o Iterate back/forth skipping some elements to speed up the process




                                                                            91
Searching
             Linear vs Binary
• If you make no assumptions
   o iterate (traverse) all elements to find an existing element
   o iterate (traverse) all elements to realize you don’t have an element

   Looking for u                                         worst case all elements
                   a x z b n m l        j   i b c u      are visited. O(n)

• If you assume the container is already order
  (according to certain rule)
   o Iterate back/forth skipping some elements to speed up the process




                                                                                   92
Searching
             Linear vs Binary
• If you make no assumptions
   o iterate (traverse) all elements to find an existing element
   o iterate (traverse) all elements to realize you don’t have an element

   Looking for u                                         worst case all elements
                   a x z b n m l        j   i b c u      are visited. O(n)

• If you assume the container is already order
  (according to certain rule)
   o Iterate back/forth skipping some elements to speed up the process




                                                                                   93
Searching
             Linear vs Binary
• If you make no assumptions
   o iterate (traverse) all elements to find an existing element
   o iterate (traverse) all elements to realize you don’t have an element

   Looking for u                                         worst case all elements
                   a x z b n m l        j   i b c u      are visited. O(n)

• If you assume the container is already order
  (according to certain rule)
   o Iterate back/forth skipping some elements to speed up the process



                   a b b c i      j   l n m u x z




                                                                                   94
Searching
              Linear vs Binary
• If you make no assumptions
   o iterate (traverse) all elements to find an existing element
   o iterate (traverse) all elements to realize you don’t have an element

   Looking for u                                         worst case all elements
                   a x z b n m l        j   i b c u      are visited. O(n)

• If you assume the container is already order
  (according to certain rule)
   o Iterate back/forth skipping some elements to speed up the process



                   a b b c i      j   l n m u x z
   Looking for u



                                                                                   95
Searching
              Linear vs Binary
• If you make no assumptions
   o iterate (traverse) all elements to find an existing element
   o iterate (traverse) all elements to realize you don’t have an element

   Looking for u                                         worst case all elements
                   a x z b n m l        j   i b c u      are visited. O(n)

• If you assume the container is already order
  (according to certain rule)
   o Iterate back/forth skipping some elements to speed up the process



                   a b b c i      j   l n m u x z
   Looking for u



                                                                                   96
Searching
              Linear vs Binary
• If you make no assumptions
   o iterate (traverse) all elements to find an existing element
   o iterate (traverse) all elements to realize you don’t have an element

   Looking for u                                         worst case all elements
                   a x z b n m l        j   i b c u      are visited. O(n)

• If you assume the container is already order
  (according to certain rule)
   o Iterate back/forth skipping some elements to speed up the process



                   a b b c i      j   l n m u x z
   Looking for u



                                                                                   97
Searching
              Linear vs Binary
• If you make no assumptions
   o iterate (traverse) all elements to find an existing element
   o iterate (traverse) all elements to realize you don’t have an element

   Looking for u                                         worst case all elements
                   a x z b n m l        j   i b c u      are visited. O(n)

• If you assume the container is already order
  (according to certain rule)
   o Iterate back/forth skipping some elements to speed up the process


                                                         worst case there are log(n)+1
                   a b b c i      j   l n m u x z        element visited. O(log(n))
   Looking for u



                                                                                   98
So binary search is faster
 but the assumption is…
• The container is already order, so
  o how do we sort a container
  o how do insert elements in a sorted container
  o How do we remove elements in a sorted container


• What is more expensive (big-Oh)
  o A linear search
  o Order a container and then a binary search
  o Maintain a container sorted and then a binary search




                                                           99
Sorting a container
            Merge sort
• Use the divide and conquer approach
  o Divide the problem into 2 subsets (unless you have a base case)
  o Recursively solve each subset
  o Conquer: Solve the sub-problem and merge them into a solution




                                                                      100
Sorting a container
            Merge sort
• Use the divide and conquer approach
  o Divide the problem into 2 subsets (unless you have a base case)
  o Recursively solve each subset
  o Conquer: Solve the sub-problem and merge them into a solution


                    85 24 63 45 19 37 91 56




                                                                      101
Sorting a container
            Merge sort
• Use the divide and conquer approach
  o Divide the problem into 2 subsets (unless you have a base case)
  o Recursively solve each subset
  o Conquer: Solve the sub-problem and merge them into a solution


                    85 24 63 45 19 37 91 56


               85 24 63 45




                                                                      102
Sorting a container
            Merge sort
• Use the divide and conquer approach
  o Divide the problem into 2 subsets (unless you have a base case)
  o Recursively solve each subset
  o Conquer: Solve the sub-problem and merge them into a solution


                    85 24 63 45 19 37 91 56


               85 24 63 45              19 37 91 56




                                                                      103
Sorting a container
            Merge sort
• Use the divide and conquer approach
  o Divide the problem into 2 subsets (unless you have a base case)
  o Recursively solve each subset
  o Conquer: Solve the sub-problem and merge them into a solution


                    85 24 63 45 19 37 91 56


               85 24 63 45              19 37 91 56


           85 24




                                                                      104
Sorting a container
            Merge sort
• Use the divide and conquer approach
  o Divide the problem into 2 subsets (unless you have a base case)
  o Recursively solve each subset
  o Conquer: Solve the sub-problem and merge them into a solution


                    85 24 63 45 19 37 91 56


               85 24 63 45              19 37 91 56


           85 24        63 45




                                                                      105
Sorting a container
            Merge sort
• Use the divide and conquer approach
  o Divide the problem into 2 subsets (unless you have a base case)
  o Recursively solve each subset
  o Conquer: Solve the sub-problem and merge them into a solution


                    85 24 63 45 19 37 91 56


               85 24 63 45              19 37 91 56


           24 85        63 45




                                                                      106
Sorting a container
            Merge sort
• Use the divide and conquer approach
  o Divide the problem into 2 subsets (unless you have a base case)
  o Recursively solve each subset
  o Conquer: Solve the sub-problem and merge them into a solution


                    85 24 63 45 19 37 91 56


               85 24 63 45              19 37 91 56


           24 85        45 63




                                                                      107
Sorting a container
            Merge sort
• Use the divide and conquer approach
  o Divide the problem into 2 subsets (unless you have a base case)
  o Recursively solve each subset
  o Conquer: Solve the sub-problem and merge them into a solution


                    85 24 63 45 19 37 91 56


               24 45 63 85              19 37 91 56


           24 85        45 63




                                                                      108
Sorting a container
                  Merge sort
• Use the divide and conquer approach
    o Divide the problem into 2 subsets (unless you have a base case)
    o Recursively solve each subset
    o Conquer: Solve the sub-problem and merge them into a solution


                      85 24 63 45 19 37 91 56


Wait, what?      24 45 63 85              19 37 91 56


              24 85       45 63




                                                                        109
Sorting a container
                  Merge sort
• Use the divide and conquer approach
    o Divide the problem into 2 subsets (unless you have a base case)
    o Recursively solve each subset
    o Conquer: Solve the sub-problem and merge them into a solution


                      85 24 63 45 19 37 91 56


Wait, what?      24 45 63 85              19 37 91 56


              24 85       45 63




                                                                        110
Sorting a container
                  Merge sort
• Use the divide and conquer approach
    o Divide the problem into 2 subsets (unless you have a base case)
    o Recursively solve each subset
    o Conquer: Solve the sub-problem and merge them into a solution


                      85 24 63 45 19 37 91 56


Wait, what?      24 45 63 85              19 37 91 56


              24 85       45 63



                                          24 85

                                          45 63
                                                                        111
Sorting a container
                  Merge sort
• Use the divide and conquer approach
    o Divide the problem into 2 subsets (unless you have a base case)
    o Recursively solve each subset
    o Conquer: Solve the sub-problem and merge them into a solution


                      85 24 63 45 19 37 91 56


Wait, what?      24 45 63 85              19 37 91 56


              24 85       45 63
                                    take the min

                                          24 85

                                          45 63
                                                                        112
Sorting a container
                  Merge sort
• Use the divide and conquer approach
    o Divide the problem into 2 subsets (unless you have a base case)
    o Recursively solve each subset
    o Conquer: Solve the sub-problem and merge them into a solution


                      85 24 63 45 19 37 91 56


Wait, what?      24 45 63 85              19 37 91 56


              24 85         45 63



                                         85 85
                                         24
                       24
                                          45 63
                                                                        113
Sorting a container
                  Merge sort
• Use the divide and conquer approach
    o Divide the problem into 2 subsets (unless you have a base case)
    o Recursively solve each subset
    o Conquer: Solve the sub-problem and merge them into a solution


                      85 24 63 45 19 37 91 56


Wait, what?      24 45 63 85              19 37 91 56


              24 85         45 63
                                    take the min

                                         85 85
                                         24
                       24
                                          45 63
                                                                        114
Sorting a container
                  Merge sort
• Use the divide and conquer approach
    o Divide the problem into 2 subsets (unless you have a base case)
    o Recursively solve each subset
    o Conquer: Solve the sub-problem and merge them into a solution


                      85 24 63 45 19 37 91 56


Wait, what?      24 45 63 85              19 37 91 56


              24 85       45 63



                                         85 85
                                         24
                      24 45
                       24
                                         63 63
                                         45
                                                                        115
Sorting a container
                  Merge sort
• Use the divide and conquer approach
    o Divide the problem into 2 subsets (unless you have a base case)
    o Recursively solve each subset
    o Conquer: Solve the sub-problem and merge them into a solution


                      85 24 63 45 19 37 91 56


Wait, what?      24 45 63 85              19 37 91 56


              24 85       45 63
                                    take the min

                                         85 85
                                         24
                      24 45
                       24
                                         63 63
                                         45
                                                                        116
Sorting a container
            Merge sort
• Use the divide and conquer approach
  o Divide the problem into 2 subsets (unless you have a base case)
  o Recursively solve each subset
  o Conquer: Solve the sub-problem and merge them into a solution


                    85 24 63 45 19 37 91 56


               24 45 63 85              19 37 91 56


           24 85        45 63




                                                                      117
Sorting a container
            Merge sort
• Use the divide and conquer approach
  o Divide the problem into 2 subsets (unless you have a base case)
  o Recursively solve each subset
  o Conquer: Solve the sub-problem and merge them into a solution


                    85 24 63 45 19 37 91 56
                            DIVIDE

               24 45 63 85              19 37 91 56
               CONQUER                    RECUR

           24 85        45 63




                                                                      118
Sorting a container
             Merge sort
• What is the complexity of merge sort
   o Divide the problem into 2 subsets (2 times half the problem)
   o Recursively solve each subset (keep subdividing the problem)
   o Conquer: Solve the sub-problem and merge (the merge running time)


                    85 24 63 45 19 37 91 56


               85 24 63 45             19 37 91 56


           85 24        63 45



                                       24 85
                    24 45
                                       45 63
                                                                         119
Sorting a container
             Merge sort
• What is the complexity of merge sort
   o Divide the problem into 2 subsets (2 times half the problem)
   o Recursively solve each subset (keep subdividing the problem)
   o Conquer: Solve the sub-problem and merge (the merge running time)

  f(n)              85 24 63 45 19 37 91 56


               85 24 63 45             19 37 91 56


           85 24        63 45



                                       24 85
                    24 45
                                       45 63
                                                                         120
Sorting a container
                Merge sort
• What is the complexity of merge sort
   o Divide the problem into 2 subsets (2 times half the problem)
   o Recursively solve each subset (keep subdividing the problem)
   o Conquer: Solve the sub-problem and merge (the merge running time)

  f(n)              85 24 63 45 19 37 91 56


  2f(n/2)      85 24 63 45             19 37 91 56


            85 24       63 45



                                       24 85
                    24 45
                                       45 63
                                                                         121
Sorting a container
                        Merge sort
     • What is the complexity of merge sort
           o Divide the problem into 2 subsets (2 times half the problem)
           o Recursively solve each subset (keep subdividing the problem)
           o Conquer: Solve the sub-problem and merge (the merge running time)

          f(n)              85 24 63 45 19 37 91 56


          2f(n/2)      85 24 63 45             19 37 91 56


          4f(n/4)   85 24       63 45
log2(n)




                                               24 85
                            24 45
                                               45 63
                                                                                 122
Sorting a container
                        Merge sort
     • What is the complexity of merge sort
           o Divide the problem into 2 subsets (2 times half the problem)
           o Recursively solve each subset (keep subdividing the problem)
           o Conquer: Solve the sub-problem and merge (the merge running time)

          f(n)              85 24 63 45 19 37 91 56


          2f(n/2)      85 24 63 45             19 37 91 56


          4f(n/4)   85 24       63 45
log2(n)




                                               24 85   x elements
                            24 45
                                               45 63   y elements
                                                                                 123
Sorting a container
                        Merge sort
     • What is the complexity of merge sort
           o Divide the problem into 2 subsets (2 times half the problem)
           o Recursively solve each subset (keep subdividing the problem)
           o Conquer: Solve the sub-problem and merge (the merge running time)

          f(n)                85 24 63 45 19 37 91 56


          2f(n/2)         85 24 63 45          19 37 91 56


          4f(n/4)     85 24       63 45
log2(n)




                                               24 85    x elements
                 O(x+y)       24 45
                                               45 63    y elements
                                                                                 124
Sorting a container
                        Merge sort
     • What is the complexity of merge sort
           o Divide the problem into 2 subsets (2 times half the problem)
           o Recursively solve each subset (keep subdividing the problem)
           o Conquer: Solve the sub-problem and merge (the merge running time)

          f(n)                85 24 63 45 19 37 91 56


          2f(n/2)         85 24 63 45          19 37 91 56


          4f(n/4)     85 24       63 45
log2(n)




                                               24 85
                 O(x+y)       24 45
                                               45 63
                                                                                 125
Sorting a container
    Merge sort




            O(nlogn)

                       126
Sorting a container
    Merge sort




            O(nlogn)

                       127
Sorting a container
    Merge sort




            O(nlogn)

                       128
Sorting a container
    Merge sort




            O(nlogn)

                       129
Sorting a container
            Merge sort
• Drawback of merge sort algorithm
  o The merge is not in place


                                  take the min
            Additional memory
                                      85
                          24 45
                                      63




• The merge could be modified to be in place, but
  the overhead will slow down the running time.


                                                    130
Sorting a container
            Quick sort
• Use the divide and conquer approach
  o Divide the problem into 3 subsets (unless you have a base case)
      • A (random) pivot x
      • A subset with numbers lower than x
      • A subset with numbers greater than x
  o Recursively solve each subset
  o Conquer: Solve the sub-problem and merge them into a solution


                     85 24 63 19 37 91 56 45


                24 37     19             85 63 91 56


                     24 37
                                                                      131
Sorting a container
            Quick sort
• Use the divide and conquer approach
  o Divide the problem into 3 subsets (unless you have a base case)
      • A (random) pivot x
      • A subset with numbers lower than x
      • A subset with numbers greater than x
  o Recursively solve each subset
  o Conquer: Solve the sub-problem and merge them into a solution


                     85 24 63 19 37 91 56 45


                24 37     19             85 63 91 56


                                   In place sorting,
                     24 37
                                   it does not require additional memory
                                                                           132
Sorting a container
            Quick sort
• Complexity
  o Quick sort (with random pivot) is O(nlogn)




• Drawback
  o No replicated element allowed in the container

      * The pivot is randomly chosen,
      * if an element occurs twice, in different divisions
      * then the merging mechanism won’t work




                                                             133
Arrays
                   Example 2
Accept 2 integer Arrays: A and B. And find the number of
common elements in both assuming no duplicates in each array.


o Brute force                                    A, n elements
      O(nm)
                                                          B, m elements


o Merge-sort modified
                                                          C, n+m elements



        Instead of merging compare and increment count when equal

        O( (n+m)log(n+m) )

                                                                      134
Arrays
                   Example 2
Accept 2 integer Arrays: A and B. And find the number of
common elements in both assuming no duplicates in each array.


o Brute force                                    A, n elements
      O(nm)
                                                          B, m elements


o Merge-sort modified
                                                          C, n+m elements



        Instead of merging compare and increment count when equal

        O( (n+m)log(n+m) )

                                                                      135
Stacks and Queues
           Example 3
Write a reverse_Queue method using only stacks and
queues

                   in
        a b c d e f g h




                          out

       Queue
       FIFO
                                                     136
Stacks and Queues
           Example 3
Write a reverse_Queue method using only stacks and
queues

                   in           in                     out   in
        a b c d e f g h




                                     a b c d e f g h




                                                              h g f e d c b a
                                                                                      O(n)




                          out                                                   out

       Queue                     Stack                       Queue
       FIFO                      LIFO                        FIFO
                                                                                             137
Stacks and Queues
            Example 4
Write a method cut_Queue that adds an element to the head of
the queue using only stacks and queues

       N
                      in
           a b c d e f g h




                             out

        Queue
        FIFO
                                                               138
Stacks and Queues
            Example 4
Write a method cut_Queue that adds an element to the head of
the queue using only stacks and queues

       N                                                                                                                  O(n)
                      in           in                       out   in                       out   in




                                                                       h g f e d c b a N
           a b c d e f g h




                                        N a b c d e f g h




                                                                                                      N a b c d e f g h
                             out
                                                                                                                          out
        Queue
        FIFO                        Stack                          Stack                         Queue
                                    LIFO                           LIFO                          FIFO                           139
List
                     Example 5
Write a method is_Sorted_Ascedent to check if a
single linked list is sorted in non-decreasing order

 head
        next         next            next         …    next




  Java pseudo code   while ( node.next ) {
                       if (node.next.key < node.key)
                           return false;
                       node = node.next;
                     }
                     return true;

                                                              140
List
                     Example 6
Write a method compress to remove duplicated
elements in a single linked list

 head
        next         next              next           …    next




  Java pseudo code   while ( node.next ) {
                     if ( node.key == node.next.key) ) {
                         node.next = node.next.next;
                       } else {
                         node = node.next;
                       }
                     }

                                                                  141

More Related Content

What's hot

4213ijaia05
4213ijaia054213ijaia05
4213ijaia05
ijaia
 
Lecture 3: Storage and Variables
Lecture 3: Storage and VariablesLecture 3: Storage and Variables
Lecture 3: Storage and Variables
Eelco Visser
 
Salt Identification Challenge
Salt Identification ChallengeSalt Identification Challenge
Salt Identification Challenge
kenluck2001
 
Querying UML Class Diagrams - FoSSaCS 2012
Querying UML Class Diagrams - FoSSaCS 2012Querying UML Class Diagrams - FoSSaCS 2012
Querying UML Class Diagrams - FoSSaCS 2012
Giorgio Orsi
 
Lecture02 class -_templatev2
Lecture02 class -_templatev2Lecture02 class -_templatev2
Lecture02 class -_templatev2
Hariz Mustafa
 
ICME 2013
ICME 2013ICME 2013
ICME 2013
nlab_utokyo
 
Landmark Retrieval & Recognition
Landmark Retrieval & RecognitionLandmark Retrieval & Recognition
Landmark Retrieval & Recognition
kenluck2001
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
Intro C# Book
 
Java tut1
Java tut1Java tut1
Java tut1
Ajmal Khan
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
Abdul Aziz
 
19 algorithms-and-complexity-110627100203-phpapp02
19 algorithms-and-complexity-110627100203-phpapp0219 algorithms-and-complexity-110627100203-phpapp02
19 algorithms-and-complexity-110627100203-phpapp02
Muhammad Aslam
 
NeuralProcessingofGeneralPurposeApproximatePrograms
NeuralProcessingofGeneralPurposeApproximateProgramsNeuralProcessingofGeneralPurposeApproximatePrograms
NeuralProcessingofGeneralPurposeApproximatePrograms
Mohid Nabil
 
December 7, Projects
December 7, ProjectsDecember 7, Projects
December 7, Projects
University of Colorado at Boulder
 
Sdtl manual
Sdtl manualSdtl manual
Sdtl manual
qaz8989
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
K Hari Shankar
 
L Fu - Dao: a novel programming language for bioinformatics
L Fu - Dao: a novel programming language for bioinformaticsL Fu - Dao: a novel programming language for bioinformatics
L Fu - Dao: a novel programming language for bioinformatics
Jan Aerts
 
03slide
03slide03slide
Nx tutorial basics
Nx tutorial basicsNx tutorial basics
Nx tutorial basics
Deepakshankar S
 
NAS EP Algorithm
NAS EP Algorithm NAS EP Algorithm
NAS EP Algorithm
Jongsu "Liam" Kim
 

What's hot (19)

4213ijaia05
4213ijaia054213ijaia05
4213ijaia05
 
Lecture 3: Storage and Variables
Lecture 3: Storage and VariablesLecture 3: Storage and Variables
Lecture 3: Storage and Variables
 
Salt Identification Challenge
Salt Identification ChallengeSalt Identification Challenge
Salt Identification Challenge
 
Querying UML Class Diagrams - FoSSaCS 2012
Querying UML Class Diagrams - FoSSaCS 2012Querying UML Class Diagrams - FoSSaCS 2012
Querying UML Class Diagrams - FoSSaCS 2012
 
Lecture02 class -_templatev2
Lecture02 class -_templatev2Lecture02 class -_templatev2
Lecture02 class -_templatev2
 
ICME 2013
ICME 2013ICME 2013
ICME 2013
 
Landmark Retrieval & Recognition
Landmark Retrieval & RecognitionLandmark Retrieval & Recognition
Landmark Retrieval & Recognition
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
 
Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 
19 algorithms-and-complexity-110627100203-phpapp02
19 algorithms-and-complexity-110627100203-phpapp0219 algorithms-and-complexity-110627100203-phpapp02
19 algorithms-and-complexity-110627100203-phpapp02
 
NeuralProcessingofGeneralPurposeApproximatePrograms
NeuralProcessingofGeneralPurposeApproximateProgramsNeuralProcessingofGeneralPurposeApproximatePrograms
NeuralProcessingofGeneralPurposeApproximatePrograms
 
December 7, Projects
December 7, ProjectsDecember 7, Projects
December 7, Projects
 
Sdtl manual
Sdtl manualSdtl manual
Sdtl manual
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
 
L Fu - Dao: a novel programming language for bioinformatics
L Fu - Dao: a novel programming language for bioinformaticsL Fu - Dao: a novel programming language for bioinformatics
L Fu - Dao: a novel programming language for bioinformatics
 
03slide
03slide03slide
03slide
 
Nx tutorial basics
Nx tutorial basicsNx tutorial basics
Nx tutorial basics
 
NAS EP Algorithm
NAS EP Algorithm NAS EP Algorithm
NAS EP Algorithm
 

Viewers also liked

Slideshare with animations
Slideshare with animationsSlideshare with animations
Slideshare with animations
Daniel Gomez-Prado
 
Meet Dave Meet SlideShare
Meet Dave Meet SlideShareMeet Dave Meet SlideShare
Meet Dave Meet SlideShare
Rashmi Sinha
 
Kinetic advisory boards
Kinetic advisory boardsKinetic advisory boards
Kinetic advisory boards
Ashfield, part of UDG Healthcare
 
The 5 big pitfalls to avoid for successful patient support programmes", by Na...
The 5 big pitfalls to avoid for successful patient support programmes", by Na...The 5 big pitfalls to avoid for successful patient support programmes", by Na...
The 5 big pitfalls to avoid for successful patient support programmes", by Na...
Ashfield, part of UDG Healthcare
 
Fotossíntese
FotossínteseFotossíntese
Fotossíntese
Cidalia Aguiar
 
Animation
AnimationAnimation
Animation
Avinash
 
B 2.1.FotossíNtese
B 2.1.FotossíNteseB 2.1.FotossíNtese
B 2.1.FotossíNtese
Cidalia Aguiar
 
Creating Animated PPT presentations
Creating Animated PPT presentationsCreating Animated PPT presentations
Creating Animated PPT presentations
SOAP Presentations
 
The age of personalisation by Lee Wales, VP Strategy at Ashfield Healthcare C...
The age of personalisation by Lee Wales, VP Strategy at Ashfield Healthcare C...The age of personalisation by Lee Wales, VP Strategy at Ashfield Healthcare C...
The age of personalisation by Lee Wales, VP Strategy at Ashfield Healthcare C...
Ashfield, part of UDG Healthcare
 
Work Rules!
Work Rules!Work Rules!
Work Rules!
Laszlo Bock
 
6 Questions to Lead You to a Social Media Strategy
6 Questions to Lead You to a Social Media Strategy6 Questions to Lead You to a Social Media Strategy
6 Questions to Lead You to a Social Media Strategy
Mark Schaefer
 
How to Determine the ROI of Anything
How to Determine the ROI of AnythingHow to Determine the ROI of Anything
How to Determine the ROI of Anything
Gary Vaynerchuk
 
Slides That Rock
Slides That RockSlides That Rock
Slides That Rock
Slides That Rock
 
All About Beer
All About Beer All About Beer
All About Beer
Ethos3
 
State of the Word 2011
State of the Word 2011State of the Word 2011
State of the Word 2011
photomatt
 
SlideShare 101
SlideShare 101SlideShare 101
SlideShare 101
Amit Ranjan
 
Build Features, Not Apps
Build Features, Not AppsBuild Features, Not Apps
Build Features, Not Apps
Natasha Murashev
 
2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare
SlideShare
 
What to Upload to SlideShare
What to Upload to SlideShareWhat to Upload to SlideShare
What to Upload to SlideShare
SlideShare
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShare
SlideShare
 

Viewers also liked (20)

Slideshare with animations
Slideshare with animationsSlideshare with animations
Slideshare with animations
 
Meet Dave Meet SlideShare
Meet Dave Meet SlideShareMeet Dave Meet SlideShare
Meet Dave Meet SlideShare
 
Kinetic advisory boards
Kinetic advisory boardsKinetic advisory boards
Kinetic advisory boards
 
The 5 big pitfalls to avoid for successful patient support programmes", by Na...
The 5 big pitfalls to avoid for successful patient support programmes", by Na...The 5 big pitfalls to avoid for successful patient support programmes", by Na...
The 5 big pitfalls to avoid for successful patient support programmes", by Na...
 
Fotossíntese
FotossínteseFotossíntese
Fotossíntese
 
Animation
AnimationAnimation
Animation
 
B 2.1.FotossíNtese
B 2.1.FotossíNteseB 2.1.FotossíNtese
B 2.1.FotossíNtese
 
Creating Animated PPT presentations
Creating Animated PPT presentationsCreating Animated PPT presentations
Creating Animated PPT presentations
 
The age of personalisation by Lee Wales, VP Strategy at Ashfield Healthcare C...
The age of personalisation by Lee Wales, VP Strategy at Ashfield Healthcare C...The age of personalisation by Lee Wales, VP Strategy at Ashfield Healthcare C...
The age of personalisation by Lee Wales, VP Strategy at Ashfield Healthcare C...
 
Work Rules!
Work Rules!Work Rules!
Work Rules!
 
6 Questions to Lead You to a Social Media Strategy
6 Questions to Lead You to a Social Media Strategy6 Questions to Lead You to a Social Media Strategy
6 Questions to Lead You to a Social Media Strategy
 
How to Determine the ROI of Anything
How to Determine the ROI of AnythingHow to Determine the ROI of Anything
How to Determine the ROI of Anything
 
Slides That Rock
Slides That RockSlides That Rock
Slides That Rock
 
All About Beer
All About Beer All About Beer
All About Beer
 
State of the Word 2011
State of the Word 2011State of the Word 2011
State of the Word 2011
 
SlideShare 101
SlideShare 101SlideShare 101
SlideShare 101
 
Build Features, Not Apps
Build Features, Not AppsBuild Features, Not Apps
Build Features, Not Apps
 
2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare
 
What to Upload to SlideShare
What to Upload to SlideShareWhat to Upload to SlideShare
What to Upload to SlideShare
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShare
 

Similar to Basic data structures part I

Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdf
abay golla
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
Sreedhar Chowdam
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
subhashchandra197
 
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaAlgorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Priyanka Rana
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
pallavidhade2
 
2 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart12 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart1
SSE_AndyLi
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
sohelranasweet
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
Sajid Marwat
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
YekoyeTigabuYeko
 
Stack squeues lists
Stack squeues listsStack squeues lists
Stack squeues lists
James Wong
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
Harry Potter
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
Luis Goldster
 
Stacksqueueslists
StacksqueueslistsStacksqueueslists
Stacksqueueslists
Fraboni Ec
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
Young Alista
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
Tony Nguyen
 
DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptx
NishaS88
 
Designing Architecture-aware Library using Boost.Proto
Designing Architecture-aware Library using Boost.ProtoDesigning Architecture-aware Library using Boost.Proto
Designing Architecture-aware Library using Boost.Proto
Joel Falcou
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
KarthikeyaLanka1
 
Lec1
Lec1Lec1

Similar to Basic data structures part I (20)

Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdf
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaAlgorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
 
2 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart12 chapter2 algorithm_analysispart1
2 chapter2 algorithm_analysispart1
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
Stack squeues lists
Stack squeues listsStack squeues lists
Stack squeues lists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Stacksqueueslists
StacksqueueslistsStacksqueueslists
Stacksqueueslists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptx
 
Designing Architecture-aware Library using Boost.Proto
Designing Architecture-aware Library using Boost.ProtoDesigning Architecture-aware Library using Boost.Proto
Designing Architecture-aware Library using Boost.Proto
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
 
Lec1
Lec1Lec1
Lec1
 

More from Daniel Gomez-Prado

Tutorial on FPGA Routing
Tutorial on FPGA RoutingTutorial on FPGA Routing
Tutorial on FPGA Routing
Daniel Gomez-Prado
 
Design of an Embedded Micro controller
Design of an Embedded Micro controllerDesign of an Embedded Micro controller
Design of an Embedded Micro controller
Daniel Gomez-Prado
 
TDS Manual
TDS ManualTDS Manual
TDS Manual
Daniel Gomez-Prado
 
Brief GAUT tutorial
Brief GAUT tutorialBrief GAUT tutorial
Brief GAUT tutorial
Daniel Gomez-Prado
 
TDS show bug 106
TDS show bug 106TDS show bug 106
TDS show bug 106
Daniel Gomez-Prado
 
TDS decompose bug 111
TDS decompose bug 111TDS decompose bug 111
TDS decompose bug 111
Daniel Gomez-Prado
 
TDS decompose bug 119
TDS decompose bug 119TDS decompose bug 119
TDS decompose bug 119
Daniel Gomez-Prado
 
TDS Bug 221
TDS Bug 221TDS Bug 221
TDS Bug 221
Daniel Gomez-Prado
 

More from Daniel Gomez-Prado (8)

Tutorial on FPGA Routing
Tutorial on FPGA RoutingTutorial on FPGA Routing
Tutorial on FPGA Routing
 
Design of an Embedded Micro controller
Design of an Embedded Micro controllerDesign of an Embedded Micro controller
Design of an Embedded Micro controller
 
TDS Manual
TDS ManualTDS Manual
TDS Manual
 
Brief GAUT tutorial
Brief GAUT tutorialBrief GAUT tutorial
Brief GAUT tutorial
 
TDS show bug 106
TDS show bug 106TDS show bug 106
TDS show bug 106
 
TDS decompose bug 111
TDS decompose bug 111TDS decompose bug 111
TDS decompose bug 111
 
TDS decompose bug 119
TDS decompose bug 119TDS decompose bug 119
TDS decompose bug 119
 
TDS Bug 221
TDS Bug 221TDS Bug 221
TDS Bug 221
 

Recently uploaded

Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Diana Rendina
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
S. Raj Kumar
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 

Recently uploaded (20)

Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 

Basic data structures part I

  • 1. Data structures & algorithms Basics: Part I By Daniel Gomez-Prado Sept 2012 Disclaimer: This tutorial may contain errors, use it at your own discretion. The slides were prepared for a class review on basic data structures at University of Massachusetts, Amherst. http://www.dgomezpr.com
  • 2. Outline • Analysis of complexity • Classes, objects and containers • Array • Stack • Queue • (Single) Linked and Double Linked List • Iterators (overview) • Linear and Binary search • Merge sort • Quick sort 2
  • 3. Analysis big-Oh Random access memory Your program Memory Import java.util.* class review { Static public main() { // this is a review // for ECE242 exam } } • Assumptions: o Unlimited memory o All memory accesses takes 1 unit time 3
  • 4. Analysis big-Oh Random access memory Your program Memory Import java.util.* class review { Static public main() { // this is a review // for ECE242 exam } } • Assumptions: o Unlimited memory We have what we need: 1 Gb or 1,000 Tb o All memory accesses takes 1 unit time 4
  • 5. Analysis big-Oh Random access memory Your program Memory Import java.util.* class review { Static public main() { // this is a review // for ECE242 exam } } • Assumptions: o Unlimited memory We have what we need: 1 Gb or 1,000 Tb No hierarchical memory, o All memory accesses takes 1 unit time Cache, L1, L2, hard drive 5
  • 6. Analysis big-Oh Running time PROGRAM OPERATIONS STEPS int sum = 0; 1 assignment 1 for (i=0;i<128;i=++) i =1, 2, 3, 4 … 128 128 for (j = 128; j>0; j=j/2) j = 128,64,32,16,8,4,2,1 log2128+1 sum = sum + a[i][j]; 1 addition, 1 assignment 2 6
  • 7. Analysis big-Oh Running time PROGRAM OPERATIONS STEPS int sum = 0; 1 assignment 1 for (i=0;i<128;i=++) i =1, 2, 3, 4 … 128 128 for (j = 128; j>0; j=j/2) j = 128,64,32,16,8,4,2,1 log2128+1 sum = sum + a[i][j]; 1 addition, 1 assignment 2 1+128*(log2128+1)*2 7
  • 8. Analysis big-Oh Running time PROGRAM OPERATIONS STEPS int sum = 0; 1 assignment 1 for (i=0;i<128;i=++) i =1, 2, 3, 4 … 128 128 for (j = 128; j>0; j=j/2) j = 128,64,32,16,8,4,2,1 log2128+1 sum = sum + a[i][j]; 1 addition, 1 assignment 2 1+128*(log2128+1)*2 In general we have an arbitrary number “n” instead of 128, in that case: 1+n*(log2n+1)*2 1+2n+2n*log2n 8
  • 9. Analysis big-Oh Running time PROGRAM OPERATIONS STEPS int sum = 0; 1 assignment 1 for (i=0;i<128;i=++) i =1, 2, 3, 4 … 128 128 for (j = 128; j>0; j=j/2) j = 128,64,32,16,8,4,2,1 log2128+1 sum = sum + a[i][j]; 1 addition, 1 assignment 2 1+128*(log2128+1)*2 n could be the size of the stack, queue, list or the dimension of a matrix, etc. In general we have an arbitrary number “n” instead of 128, in that case: 1+n*(log2n+1)*2 can we simplify the expression? 1+2n+2n*log2n YES!, By using big-Oh notation we can specify the asymptotic complexity of the algorithm 9
  • 10. Analysis big-Oh Definition • Given functions f(n) and g(n): o f(n) is said to be O(g(n)) o if and only if • there are (exist) 2 positive constants, C>0 and N>0 o such that • f(n) ≤ Cg(n) for every n>N 10
  • 11. Analysis big-Oh Example of definition usage 11
  • 12. Analysis big-Oh Example of definition usage Relationship between C & n 12
  • 13. Analysis big-Oh Example of definition usage Relationship between C & n 13
  • 14. Analysis big-Oh Example of definition usage Relationship between C & n O(n*log2n) is true for C=3 and n≥32 14
  • 15. Analysis big-Oh Example of definition usage keywords Relationship between C & n O(n*log2n) is true for C=3 and n≥32 15
  • 16. Analysis big-Oh Example of definition usage keywords f(n) is given g(n) is given Relationship between C & n O(n*log2n) is true for C=3 and n≥32 16
  • 17. Analysis big-Oh Example 1 State the asymptotic complexity of: i. Print out middle element of an array of size n 17
  • 18. Analysis big-Oh Example 1 State the asymptotic complexity of: big-Oh i. Print out middle element of an array of size n 18
  • 19. Analysis big-Oh Example 1 State the asymptotic complexity of: big-Oh i. Print out middle element of an array of size n arrays allow access to any position randomly 19
  • 20. Analysis big-Oh Example 1 State the asymptotic complexity of: big-Oh i. Print out middle element of an array of size n arrays allow access to any position randomly 20
  • 21. Analysis big-Oh Example 1 State the asymptotic complexity of: big-Oh i. Print out middle element of an array of size n arrays allow access to any position randomly recall Your program Memory o All memory accesses takes 1 unit time 21
  • 22. Analysis big-Oh Example 1 State the asymptotic complexity of: big-Oh i. Print out middle element of an array of size n arrays allow access to any position randomly recall Your program Memory Solution is: O(1) o All memory accesses takes 1 unit time 22
  • 23. Analysis big-Oh Example 1 ii. Print out the middle element of a linked list of size n 23
  • 24. Analysis big-Oh Example 1 ii. Print out the middle element of a linked list of size n recall a linked list 24
  • 25. Analysis big-Oh Example 1 ii. Print out the middle element of a linked list of size n head tail next next next next object object object object 25
  • 26. Analysis big-Oh Example 1 ii. Print out the middle element of a linked list of size n head tail next next next next object object object object 26
  • 27. Analysis big-Oh Example 1 ii. Print out the middle element of a linked list of size n head tail next next next next n/2 object object object memory object locations 27
  • 28. Analysis big-Oh Example 1 ii. Print out the middle element of a linked list of size n head tail next next next next n/2 object object object memory object locations 28
  • 29. Analysis big-Oh Example 1 ii. Print out the middle element of a linked list of size n head tail next next next next n/2 object object object memory object locations f(n) = n/2 Solution is: the asymptotic complexity is O(n) 29
  • 30. Analysis big-Oh Example 1 iii. Print out the odd elements of an array of size n f(n) = n/2 Solution: the asymptotic complexity is O(n) iv. Pop 10 elements from a stack that is implemented with an array. Assume that the stacks contains n elements and n > 10. When in doubt, ASK! is n = 11 or is n > 1000 ? f(n) = 10 Solution: the asymptotic complexity is O(1) 30
  • 31. Classes and objects • The goal of a “class” (in object-oriented language) o Encapsulate state and behavior • A class is a blueprint that has o a constructor to initialize its data members o a destructor to tear down the object o A coherent interface to interact with the object (public methods) o Private methods unreachable from the outside o The possibility to extend and inherit members from other classes • An object is an instant of a class • What are the benefits: o Through inheritance, extensions, packages allows to structure a program o Exposes behavior that could be reused o Alleviates the problem of understanding somebody else code 31
  • 32. ADT (Abstract Data Type) • ADTs are containers • ADTs are primarily concern in: o Aggregation of data o Access of data o Efficiency of memory is used o Efficiency of the container access 32
  • 33. Arrays • Contiguous blocks of memory of a data type o Any position can be randomly access • Example o Int[] integer_array = new int[1024]; 0 1023 o ObjectY[] object_y_array = new ObjectY[512]; 33
  • 34. Arrays • Contiguous blocks of memory of a data type o Any position can be randomly access • Example o Int[] integer_array = new int[1024]; int size 0 1023 o ObjectY[] object_y_array = new ObjectY[512]; 34
  • 35. Arrays • Contiguous blocks of memory of a data type o Any position can be randomly access • Example o Int[] integer_array = new int[1024]; 0 1023 o ObjectY[] object_y_array = new ObjectY[512]; 0 511 35
  • 36. Arrays • Contiguous blocks of memory of a data type o Any position can be randomly access • Example o Int[] integer_array = new int[1024]; 0 1023 o ObjectY[] object_y_array = new ObjectY[512]; ObjectY size 0 511 36
  • 37. Arrays • Contiguous blocks of memory of a data type o Any position can be randomly access • Example o Int[] integer_array = new int[1024]; 0 1023 In Java memory management is o ObjectY[] object_y_array = new ObjectY[512]; taken care for you. ObjectY size 0 511 37
  • 38. Arrays • Contiguous blocks of memory of a data type o Any position can be randomly access • Example o Int[] integer_array = new int[1024]; 0 1023 o ObjectY[] object_y_array = new ObjectY[512]; 1,934,218 ObjectX 0 511 0 ObjectZ … … 38
  • 39. Arrays • Contiguous blocks of memory of a data type o Any position can be randomly access • Example o Int[] integer_array = new int[1024]; 0 1023 o ObjectY[] object_y_array = new ObjectY[512]; 1,934,218 ObjectX 0 511 0 ObjectZ … … 39
  • 40. Arrays • Contiguous blocks of memory of a data type o Any position can be randomly access • Example o Int[] integer_array = new int[1024]; 0 1023 o ObjectY[] object_y_array = new ObjectY[512]; 1,934,218 ObjectX 0 511 0 ObjectZ … … 512 ObjectsY fixed boundary 40
  • 41. Stacks • Enforce a LIFO behavior (last in, first out) o It is based on an array o It overrides the random access of an array by a LIFO access 1023 0 41
  • 42. Stacks • Enforce a LIFO behavior (last in, first out) o It is based on an array o It overrides the random access of an array by a LIFO access 1023 1023 peek push push 0 0 push 42
  • 43. Stacks • Enforce a LIFO behavior (last in, first out) o It is based on an array o It overrides the random access of an array by a LIFO access 1023 1023 1023 peek push pop pop peek push 0 0 push 0 43
  • 44. Stacks • Enforce a LIFO behavior (last in, first out) o It is based on an array o It overrides the random access of an array by a LIFO access 1023 1023 1023 isEmpty isEmpty isEmpty true false false peek push pop pop peek push 0 0 push 0 44
  • 45. Stacks • Enforce a LIFO behavior (last in, first out) o It is based on an array o It overrides the random access of an array by a LIFO access 1023 1023 1023 isEmpty isEmpty isEmpty true false false peek push pop pop peek index push 0 0 push 0 45
  • 46. Stacks • Enforce a LIFO behavior (last in, first out) o It is based on an array o It overrides the random access of an array by a LIFO access 1023 1023 1023 isEmpty isEmpty isEmpty true false false peek push pop pop peek index push 0 0 push 0 46
  • 47. Stacks • Enforce a LIFO behavior (last in, first out) o It is based on an array o It overrides the random access of an array by a LIFO access 1023 1023 1023 isEmpty isEmpty isEmpty true false false peek push pop pop status peek index push 0 0 push 0 47
  • 48. Stacks • Enforce a LIFO behavior (last in, first out) o It is based on an array o It overrides the random access of an array by a LIFO access 1024 1023 Recall what a class encapsulates isEmpty o Status & false o Behavior pop peek index 0 48 -1
  • 49. Stacks • Enforce a LIFO behavior (last in, first out) o It is based on an array o It overrides the random access of an array by a LIFO access 1024 1023 Recall what a class encapsulates isEmpty o Status & false o Behavior pop Does it mean we are always safe o index = -1, stack is empty, good o index = 1024, peek index o refuse to push objects o overflow, runtime exception 0 49 -1
  • 50. Stacks • Enforce a LIFO behavior (last in, first out) o It is based on an array o It overrides the random access of an array by a LIFO access 1024 1023 Recall what a class encapsulates isEmpty o Status & false o Behavior pop Does it mean we are always safe o index = -1, stack is empty, good o index = 1024, peek index o refuse to push objects o overflow, runtime exception 0 50 -1
  • 51. Stacks … ObjectZ • Enforce a LIFO behavior (last in, first out) o It is based on an array o It overrides the random access of an array by a LIFO access 0 1024 1023 Recall what a class encapsulates isEmpty o Status & false o Behavior pop Does it mean we are always safe o index = -1, stack is empty, good o index = 1024, peek index o refuse to push objects o overflow, runtime exception 0 51 -1
  • 52. Queues • Enforce a FIFO behavior (first in, first out) o It is based on an array o It overrides the random access of an array by a FIFO access 1023 0 52
  • 53. Queues • Enforce a FIFO behavior (first in, first out) o It is based on an array o It overrides the random access of an array by a FIFO access 1023 1023 peek enqueue enqueue 0 0 enqueue 53
  • 54. Queues • Enforce a FIFO behavior (first in, first out) o It is based on an array o It overrides the random access of an array by a FIFO access 1023 1023 1023 peek enqueue peek dequeue enqueue 0 0 enqueue 0 dequeue 54
  • 55. Queues • Enforce a FIFO behavior (first in, first out) o It is based on an array o It overrides the random access of an array by a FIFO access 1023 1023 1023 isEmpty isEmpty isEmpty true false false peek enqueue peek dequeue enqueue 0 0 enqueue 0 dequeue 55
  • 56. Queues • Enforce a FIFO behavior (first in, first out) o It is based on an array o It overrides the random access of an array by a FIFO access 1023 1023 1023 isEmpty isEmpty isEmpty true false false status peek enqueue peek index1 index2 dequeue enqueue 0 0 enqueue 0 dequeue 56
  • 57. Queues • Enforce a FIFO behavior (first in, first out) o It is based on an array o It overrides the random access of an array by a FIFO access 1023 Recall what a class encapsulates isEmpty o Status & false status o Behavior peek index1 index2 dequeue 0 dequeue 57
  • 58. Queues • Enforce a FIFO behavior (first in, first out) o It is based on an array o It overrides the random access of an array by a FIFO access 1023 Recall what a class encapsulates isEmpty o Status & false status o Behavior peek index1 Does it mean we are always safe o index1 = index2, stack is empty, good index2 o index1 or index2 = 1024, dequeue o rewind to 0 o test condition o Increment using mod 1024 o What if index2 > index1 0 dequeue 58
  • 59. Queues • Enforce a FIFO behavior (first in, first out) o It is based on an array o It overrides the random access of an array by a FIFO access 1023 Recall what a class encapsulates isEmpty o Status & false status o Behavior peek index1 Does it mean we are always safe > o index1 = index2, stack is empty, good index2 o index1 or index2 = 1024, dequeue o rewind to 0 o test condition o Increment using mod 1024 o What if index2 > index1 0 dequeue 59
  • 60. Queues • Enforce a FIFO behavior (first in, first out) o It is based on an array o It overrides the random access of an array by a FIFO access 1023 1023 status index2 status peek index1 > > index2 peek index1 dequeue 0 0 dequeue 60
  • 61. Is everything an Array? can we use something else? beginning end • Recall an array o Contiguous memory ObjectX 0 ObjectY N-1 0 ObjectZ … … o Fixed bound size fixed boundary o Random access • Let’s use another construct o Non contiguous memory o Unlimited size ObjectX ObjectY ObjectZ … o Sequential access only … 61
  • 62. Is everything an Array? can we use something else? beginning end • Recall an array o Contiguous memory ObjectX 0 ObjectY N-1 0 ObjectZ … … o Fixed bound size fixed boundary o Random access • Let’s use another construct o Non contiguous memory o Unlimited size ObjectX ObjectY ObjectZ … o Sequential access only … 62
  • 63. Is everything an Array? can we use something else? beginning end • Recall an array o Contiguous memory ObjectX 0 ObjectY N-1 0 ObjectZ … … o Fixed bound size fixed boundary o Random access How do we know in this container? • Let’s use another construct o the beginning, the end o which element is next o Non contiguous memory o Unlimited size ObjectX ObjectY ObjectZ … o Sequential access only … 63
  • 64. Is everything an Array? can we use something else? beginning end • Recall an array o Contiguous memory ObjectX 0 ObjectY N-1 0 ObjectZ … … o Fixed bound size fixed boundary o Random access How do we know in this container? • Let’s use another construct o the beginning, the end o which element is next o Non contiguous memory o Unlimited size ObjectX ObjectY ObjectZ … o Sequential access only … 64
  • 65. Is everything an Array? can we use something else? beginning end • Recall an array o Contiguous memory ObjectX 0 ObjectY N-1 0 ObjectZ … … o Fixed bound size fixed boundary o Random access How do we know in this container? • Let’s use another construct o the beginning, the end o which element is next o Non contiguous memory o Unlimited size ObjectX ObjectY ObjectZ … o Sequential access only … head next 65
  • 66. Is everything an Array? can we use something else? beginning end • Recall an array o Contiguous memory ObjectX 0 ObjectY N-1 0 ObjectZ … … o Fixed bound size fixed boundary o Random access How do we know in this container? • Let’s use another construct o the beginning, the end o which element is next o Non contiguous memory o Unlimited size ObjectX ObjectY ObjectZ … o Sequential access only … head next next object 66
  • 67. Is everything an Array? can we use something else? beginning end • Recall an array o Contiguous memory ObjectX 0 ObjectY N-1 0 ObjectZ … … o Fixed bound size fixed boundary o Random access How do we know in this container? • Let’s use another construct o the beginning, the end o which element is next o Non contiguous memory o Unlimited size ObjectX ObjectY ObjectZ … o Sequential access only … head next next head object 67
  • 68. Is everything an Array? can we use something else? beginning end • Recall an array o Contiguous memory ObjectX 0 ObjectY N-1 0 ObjectZ … … o Fixed bound size fixed boundary o Random access How do we know in this container? • Let’s use another construct o the beginning, the end o which element is next o Non contiguous memory o Unlimited size ObjectX ObjectY ObjectZ … o Sequential access only … head next prev next head object 68
  • 69. Is everything an Array? can we use something else? beginning end • Recall an array o Contiguous memory ObjectX 0 ObjectY N-1 0 ObjectZ … … o Fixed bound size fixed boundary o Random access How do we know in this container? • Let’s use another construct o the beginning, the end o which element is next o Non contiguous memory o Unlimited size ObjectX ObjectY ObjectZ … o Sequential access only … head next prev next Node head object 69
  • 70. Is everything an Array? can we use something else? beginning end • Recall an array o Contiguous memory ObjectX 0 ObjectY N-1 0 ObjectZ … … o Fixed bound size fixed boundary o Random access How do we know in this container? • Let’s use another construct o the beginning, the end o which element is next o Non contiguous memory o Unlimited size ObjectX ObjectY ObjectZ … o Sequential access only … head next edges prev next Node head object 70
  • 71. Linked List • Use the prior construct (node and edge) head next next next next … object object object 71
  • 72. Linked List • Use the prior construct (node and edge) head next next next next … object object object push next object 72
  • 73. Linked List • Use the prior construct (node and edge) head next next next next … object object object push next object 73
  • 74. Linked List • Use the prior construct (node and edge) head next next next next … object object object push next object 74
  • 75. Linked List • Use the prior construct (node and edge) head next next next next … object object object next pop object 75
  • 76. Linked List • Use the prior construct (node and edge) head next next next next … object object object next pop object 76
  • 77. Linked List • Use the prior construct (node and edge) head next next next next … object object object next pop object 77
  • 78. Linked List • Use the prior construct (node and edge) head next next next next … object object object next pop object 78
  • 79. Linked List • Use the prior construct (node and edge) head next next next next … object object object pop 79
  • 80. Linked List • Use the prior construct (node and edge) head next next next next … object object object peek 80
  • 81. Linked List • Use the prior construct (node and edge) head next next next next … object object object peek 81
  • 82. Double linked List • Use the prior construct (node and edge) head prev next prev next prev next … prev next object object object 82
  • 83. Quick Questions Can we do: • a linked list or double linked list from an array o Yes • a queue with nodes and edges o Why not. • a stack with nodes and edges o Sure 83
  • 84. Iterators encapsulate container traversals • we have two implementations of a stack 1023 pop prev next prev next head prev next prev 4 next peek index 1 2 3 4 prev 3 next prev 2 next 0 push prev 1 next 84
  • 85. Iterators encapsulate container traversals • we have two implementations of a stack 1023 pop prev next prev next head prev next prev 4 next peek index 1 2 3 4 prev 3 next prev 2 next 0 push prev 1 next 85
  • 86. Iterators encapsulate container traversals • we have two implementations of a stack 1023 peek push pop pop prev next prev next head prev next prev 4 next peek index 1 2 3 4 prev 3 next prev 2 next 0 push prev 1 next 86
  • 87. Iterators encapsulate container traversals • we have two implementations of a stack 1023 prev next head prev next prev 5 next peek index 1 2 3 4 5 prev 4 next prev 3 next prev 2 next 0 push prev 1 next 87
  • 88. Iterators encapsulate container traversals • we have two implementations of a stack 1023 update next update prev prev next increment by 1 head decrement by 1 prev next prev 5 next peek index 1 2 3 4 5 prev 4 next prev 3 next prev 2 next 0 push prev 1 next 88
  • 89. Iterators encapsulate container traversals • we have two implementations of a stack Traverse container 1023 according to container rules update next update prev prev next increment by 1 head decrement by 1 prev next prev 5 next peek index 1 2 3 4 5 prev 4 next prev 3 next prev 2 next 0 push prev 1 next 89
  • 90. Iterators encapsulate container traversals • we have two implementations of a stack Traverse container behavior 1023 according to container rules state update next update prev prev next increment by 1 head decrement by 1 prev next prev 5 next peek index 1 2 3 4 5 prev 4 next prev 3 next prev 2 next 0 push prev 1 next 90
  • 91. Searching Linear vs Binary • If you make no assumptions o iterate (traverse) all elements to find an existing element o iterate (traverse) all elements to realize you don’t have an element a x z b n m l j i b c u • If you assume the container is already order (according to certain rule) o Iterate back/forth skipping some elements to speed up the process 91
  • 92. Searching Linear vs Binary • If you make no assumptions o iterate (traverse) all elements to find an existing element o iterate (traverse) all elements to realize you don’t have an element Looking for u worst case all elements a x z b n m l j i b c u are visited. O(n) • If you assume the container is already order (according to certain rule) o Iterate back/forth skipping some elements to speed up the process 92
  • 93. Searching Linear vs Binary • If you make no assumptions o iterate (traverse) all elements to find an existing element o iterate (traverse) all elements to realize you don’t have an element Looking for u worst case all elements a x z b n m l j i b c u are visited. O(n) • If you assume the container is already order (according to certain rule) o Iterate back/forth skipping some elements to speed up the process 93
  • 94. Searching Linear vs Binary • If you make no assumptions o iterate (traverse) all elements to find an existing element o iterate (traverse) all elements to realize you don’t have an element Looking for u worst case all elements a x z b n m l j i b c u are visited. O(n) • If you assume the container is already order (according to certain rule) o Iterate back/forth skipping some elements to speed up the process a b b c i j l n m u x z 94
  • 95. Searching Linear vs Binary • If you make no assumptions o iterate (traverse) all elements to find an existing element o iterate (traverse) all elements to realize you don’t have an element Looking for u worst case all elements a x z b n m l j i b c u are visited. O(n) • If you assume the container is already order (according to certain rule) o Iterate back/forth skipping some elements to speed up the process a b b c i j l n m u x z Looking for u 95
  • 96. Searching Linear vs Binary • If you make no assumptions o iterate (traverse) all elements to find an existing element o iterate (traverse) all elements to realize you don’t have an element Looking for u worst case all elements a x z b n m l j i b c u are visited. O(n) • If you assume the container is already order (according to certain rule) o Iterate back/forth skipping some elements to speed up the process a b b c i j l n m u x z Looking for u 96
  • 97. Searching Linear vs Binary • If you make no assumptions o iterate (traverse) all elements to find an existing element o iterate (traverse) all elements to realize you don’t have an element Looking for u worst case all elements a x z b n m l j i b c u are visited. O(n) • If you assume the container is already order (according to certain rule) o Iterate back/forth skipping some elements to speed up the process a b b c i j l n m u x z Looking for u 97
  • 98. Searching Linear vs Binary • If you make no assumptions o iterate (traverse) all elements to find an existing element o iterate (traverse) all elements to realize you don’t have an element Looking for u worst case all elements a x z b n m l j i b c u are visited. O(n) • If you assume the container is already order (according to certain rule) o Iterate back/forth skipping some elements to speed up the process worst case there are log(n)+1 a b b c i j l n m u x z element visited. O(log(n)) Looking for u 98
  • 99. So binary search is faster but the assumption is… • The container is already order, so o how do we sort a container o how do insert elements in a sorted container o How do we remove elements in a sorted container • What is more expensive (big-Oh) o A linear search o Order a container and then a binary search o Maintain a container sorted and then a binary search 99
  • 100. Sorting a container Merge sort • Use the divide and conquer approach o Divide the problem into 2 subsets (unless you have a base case) o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 100
  • 101. Sorting a container Merge sort • Use the divide and conquer approach o Divide the problem into 2 subsets (unless you have a base case) o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 45 19 37 91 56 101
  • 102. Sorting a container Merge sort • Use the divide and conquer approach o Divide the problem into 2 subsets (unless you have a base case) o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 45 19 37 91 56 85 24 63 45 102
  • 103. Sorting a container Merge sort • Use the divide and conquer approach o Divide the problem into 2 subsets (unless you have a base case) o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 45 19 37 91 56 85 24 63 45 19 37 91 56 103
  • 104. Sorting a container Merge sort • Use the divide and conquer approach o Divide the problem into 2 subsets (unless you have a base case) o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 45 19 37 91 56 85 24 63 45 19 37 91 56 85 24 104
  • 105. Sorting a container Merge sort • Use the divide and conquer approach o Divide the problem into 2 subsets (unless you have a base case) o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 45 19 37 91 56 85 24 63 45 19 37 91 56 85 24 63 45 105
  • 106. Sorting a container Merge sort • Use the divide and conquer approach o Divide the problem into 2 subsets (unless you have a base case) o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 45 19 37 91 56 85 24 63 45 19 37 91 56 24 85 63 45 106
  • 107. Sorting a container Merge sort • Use the divide and conquer approach o Divide the problem into 2 subsets (unless you have a base case) o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 45 19 37 91 56 85 24 63 45 19 37 91 56 24 85 45 63 107
  • 108. Sorting a container Merge sort • Use the divide and conquer approach o Divide the problem into 2 subsets (unless you have a base case) o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 45 19 37 91 56 24 45 63 85 19 37 91 56 24 85 45 63 108
  • 109. Sorting a container Merge sort • Use the divide and conquer approach o Divide the problem into 2 subsets (unless you have a base case) o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 45 19 37 91 56 Wait, what? 24 45 63 85 19 37 91 56 24 85 45 63 109
  • 110. Sorting a container Merge sort • Use the divide and conquer approach o Divide the problem into 2 subsets (unless you have a base case) o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 45 19 37 91 56 Wait, what? 24 45 63 85 19 37 91 56 24 85 45 63 110
  • 111. Sorting a container Merge sort • Use the divide and conquer approach o Divide the problem into 2 subsets (unless you have a base case) o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 45 19 37 91 56 Wait, what? 24 45 63 85 19 37 91 56 24 85 45 63 24 85 45 63 111
  • 112. Sorting a container Merge sort • Use the divide and conquer approach o Divide the problem into 2 subsets (unless you have a base case) o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 45 19 37 91 56 Wait, what? 24 45 63 85 19 37 91 56 24 85 45 63 take the min 24 85 45 63 112
  • 113. Sorting a container Merge sort • Use the divide and conquer approach o Divide the problem into 2 subsets (unless you have a base case) o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 45 19 37 91 56 Wait, what? 24 45 63 85 19 37 91 56 24 85 45 63 85 85 24 24 45 63 113
  • 114. Sorting a container Merge sort • Use the divide and conquer approach o Divide the problem into 2 subsets (unless you have a base case) o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 45 19 37 91 56 Wait, what? 24 45 63 85 19 37 91 56 24 85 45 63 take the min 85 85 24 24 45 63 114
  • 115. Sorting a container Merge sort • Use the divide and conquer approach o Divide the problem into 2 subsets (unless you have a base case) o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 45 19 37 91 56 Wait, what? 24 45 63 85 19 37 91 56 24 85 45 63 85 85 24 24 45 24 63 63 45 115
  • 116. Sorting a container Merge sort • Use the divide and conquer approach o Divide the problem into 2 subsets (unless you have a base case) o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 45 19 37 91 56 Wait, what? 24 45 63 85 19 37 91 56 24 85 45 63 take the min 85 85 24 24 45 24 63 63 45 116
  • 117. Sorting a container Merge sort • Use the divide and conquer approach o Divide the problem into 2 subsets (unless you have a base case) o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 45 19 37 91 56 24 45 63 85 19 37 91 56 24 85 45 63 117
  • 118. Sorting a container Merge sort • Use the divide and conquer approach o Divide the problem into 2 subsets (unless you have a base case) o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 45 19 37 91 56 DIVIDE 24 45 63 85 19 37 91 56 CONQUER RECUR 24 85 45 63 118
  • 119. Sorting a container Merge sort • What is the complexity of merge sort o Divide the problem into 2 subsets (2 times half the problem) o Recursively solve each subset (keep subdividing the problem) o Conquer: Solve the sub-problem and merge (the merge running time) 85 24 63 45 19 37 91 56 85 24 63 45 19 37 91 56 85 24 63 45 24 85 24 45 45 63 119
  • 120. Sorting a container Merge sort • What is the complexity of merge sort o Divide the problem into 2 subsets (2 times half the problem) o Recursively solve each subset (keep subdividing the problem) o Conquer: Solve the sub-problem and merge (the merge running time) f(n) 85 24 63 45 19 37 91 56 85 24 63 45 19 37 91 56 85 24 63 45 24 85 24 45 45 63 120
  • 121. Sorting a container Merge sort • What is the complexity of merge sort o Divide the problem into 2 subsets (2 times half the problem) o Recursively solve each subset (keep subdividing the problem) o Conquer: Solve the sub-problem and merge (the merge running time) f(n) 85 24 63 45 19 37 91 56 2f(n/2) 85 24 63 45 19 37 91 56 85 24 63 45 24 85 24 45 45 63 121
  • 122. Sorting a container Merge sort • What is the complexity of merge sort o Divide the problem into 2 subsets (2 times half the problem) o Recursively solve each subset (keep subdividing the problem) o Conquer: Solve the sub-problem and merge (the merge running time) f(n) 85 24 63 45 19 37 91 56 2f(n/2) 85 24 63 45 19 37 91 56 4f(n/4) 85 24 63 45 log2(n) 24 85 24 45 45 63 122
  • 123. Sorting a container Merge sort • What is the complexity of merge sort o Divide the problem into 2 subsets (2 times half the problem) o Recursively solve each subset (keep subdividing the problem) o Conquer: Solve the sub-problem and merge (the merge running time) f(n) 85 24 63 45 19 37 91 56 2f(n/2) 85 24 63 45 19 37 91 56 4f(n/4) 85 24 63 45 log2(n) 24 85 x elements 24 45 45 63 y elements 123
  • 124. Sorting a container Merge sort • What is the complexity of merge sort o Divide the problem into 2 subsets (2 times half the problem) o Recursively solve each subset (keep subdividing the problem) o Conquer: Solve the sub-problem and merge (the merge running time) f(n) 85 24 63 45 19 37 91 56 2f(n/2) 85 24 63 45 19 37 91 56 4f(n/4) 85 24 63 45 log2(n) 24 85 x elements O(x+y) 24 45 45 63 y elements 124
  • 125. Sorting a container Merge sort • What is the complexity of merge sort o Divide the problem into 2 subsets (2 times half the problem) o Recursively solve each subset (keep subdividing the problem) o Conquer: Solve the sub-problem and merge (the merge running time) f(n) 85 24 63 45 19 37 91 56 2f(n/2) 85 24 63 45 19 37 91 56 4f(n/4) 85 24 63 45 log2(n) 24 85 O(x+y) 24 45 45 63 125
  • 126. Sorting a container Merge sort O(nlogn) 126
  • 127. Sorting a container Merge sort O(nlogn) 127
  • 128. Sorting a container Merge sort O(nlogn) 128
  • 129. Sorting a container Merge sort O(nlogn) 129
  • 130. Sorting a container Merge sort • Drawback of merge sort algorithm o The merge is not in place take the min Additional memory 85 24 45 63 • The merge could be modified to be in place, but the overhead will slow down the running time. 130
  • 131. Sorting a container Quick sort • Use the divide and conquer approach o Divide the problem into 3 subsets (unless you have a base case) • A (random) pivot x • A subset with numbers lower than x • A subset with numbers greater than x o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 19 37 91 56 45 24 37 19 85 63 91 56 24 37 131
  • 132. Sorting a container Quick sort • Use the divide and conquer approach o Divide the problem into 3 subsets (unless you have a base case) • A (random) pivot x • A subset with numbers lower than x • A subset with numbers greater than x o Recursively solve each subset o Conquer: Solve the sub-problem and merge them into a solution 85 24 63 19 37 91 56 45 24 37 19 85 63 91 56 In place sorting, 24 37 it does not require additional memory 132
  • 133. Sorting a container Quick sort • Complexity o Quick sort (with random pivot) is O(nlogn) • Drawback o No replicated element allowed in the container * The pivot is randomly chosen, * if an element occurs twice, in different divisions * then the merging mechanism won’t work 133
  • 134. Arrays Example 2 Accept 2 integer Arrays: A and B. And find the number of common elements in both assuming no duplicates in each array. o Brute force A, n elements O(nm) B, m elements o Merge-sort modified C, n+m elements Instead of merging compare and increment count when equal O( (n+m)log(n+m) ) 134
  • 135. Arrays Example 2 Accept 2 integer Arrays: A and B. And find the number of common elements in both assuming no duplicates in each array. o Brute force A, n elements O(nm) B, m elements o Merge-sort modified C, n+m elements Instead of merging compare and increment count when equal O( (n+m)log(n+m) ) 135
  • 136. Stacks and Queues Example 3 Write a reverse_Queue method using only stacks and queues in a b c d e f g h out Queue FIFO 136
  • 137. Stacks and Queues Example 3 Write a reverse_Queue method using only stacks and queues in in out in a b c d e f g h a b c d e f g h h g f e d c b a O(n) out out Queue Stack Queue FIFO LIFO FIFO 137
  • 138. Stacks and Queues Example 4 Write a method cut_Queue that adds an element to the head of the queue using only stacks and queues N in a b c d e f g h out Queue FIFO 138
  • 139. Stacks and Queues Example 4 Write a method cut_Queue that adds an element to the head of the queue using only stacks and queues N O(n) in in out in out in h g f e d c b a N a b c d e f g h N a b c d e f g h N a b c d e f g h out out Queue FIFO Stack Stack Queue LIFO LIFO FIFO 139
  • 140. List Example 5 Write a method is_Sorted_Ascedent to check if a single linked list is sorted in non-decreasing order head next next next … next Java pseudo code while ( node.next ) { if (node.next.key < node.key) return false; node = node.next; } return true; 140
  • 141. List Example 6 Write a method compress to remove duplicated elements in a single linked list head next next next … next Java pseudo code while ( node.next ) { if ( node.key == node.next.key) ) { node.next = node.next.next; } else { node = node.next; } } 141