SlideShare ist ein Scribd-Unternehmen logo
1 von 31
Lecture -3
      on
Data structures
    Array
Array
Data structures are classified as either linear or nonlinear.
A data structure is said to be linear if its elements form a sequence or a linear
list.
There are two basic ways of representing such linear structures in memory.
One way is to have the linear relationship between the elements represented by
means of sequential memory locations. These linear structures are called
arrays.
The other way is to have the linear relationship between the elements
represented by means of pointers or links. These linear structures are called
linked lists.
Nonlinear structures are trees and graphs.
Linear Arrays
A linear array is a list of finite number n of homogeneous data elements such that :
a) The elements of the array are referenced respectively by an index set consisting
   of n consecutive numbers.
b) The elements of the array are stored respectively in successive memory
   locations.
The number n of elements is called the length or size of the array.

Three numbers define an array : lower bound, upper bound, size.
a. The lower bound is the smallest subscript you can use in the array (usually 0)
b. The upper bound is the largest subscript you can use in the array
c. The size / length of the array refers to the number of elements in the array , It
    can be computed as upper bound - lower bound + 1

Let, Array name is A then the elements of A is : a1,a2….. an
Or by the bracket notation A[1], A[2], A[3],…………., A[n]
The number k in A[k] is called a subscript and A[k] is called a subscripted variable.
Linear Arrays
Example :
A linear array DATA consisting of the name of six elements

             DATA
                              DATA[1] = 247
    1   247
                              DATA[2] = 56
    2   56
    3                         DATA[3] = 429
        429
    4   135                   DATA[4] = 135
    5   87                    DATA[5] = 87
    6
        156                   DATA[6] = 156
Linear Arrays
Example :
An automobile company uses an array AUTO to record the number of auto mobile
sold each year from 1932 through 1984.
AUTO[k] = Number of auto mobiles sold in the year K
LB = 1932
UB = 1984
Length = UB – LB+1 = 1984 – 1930+1 =55
Representation of linear array in memory
Let LA be a linear array in the memory of the computer. The memory of the
computer is a sequence of addressed locations.
           LA
1000
1001                       The computer does not need to keep track of the
1002                       address of every element of LA, but needs to keep
1003                       track only of the first element of LA, denoted by
1004
                                            Base(LA)
1005
                           Called the base address of LA. Using this address
                           Base(LA), the computer calculates the address of
                           any element of LA by the following formula :
                           LOC(LA[k]) = Base(LA) + w(K – lower bound)
                           Where w is the number of words per memory cell for
Fig : Computer memory      the array LA
Representation of linear array in memory
200
201                         Example :
                            An automobile company uses an array AUTO to record
202
                            the number of auto mobile sold each year from 1932
203              AUTO[1932]
                            through 1984. Suppose AUTO appears in memory as
204                         pictured in fig A . That is Base(AUTO) = 200, and w = 4
205                         words per memory cell for AUTO. Then,
206                         LOC(AUTO[1932]) = 200, LOC(AUTO[1933]) =204
207              AUTO[1933] LOC(AUTO[1934]) = 208
                            the address of the array element for the year K = 1965
208                         can be obtained by using :
209                         LOC(AUTO[1965]) = Base(AUTO) + w(1965 – lower
210              AUTO[1934] bound)
211                         =200+4(1965-1932)=332
212




      Fig : A
Traversing linear arrays
 Print the contents of each element of DATA or Count the number of
elements of DATA with a given property. This can be accomplished by
traversing DATA, That is, by accessing and processing (visiting) each
element of DATA exactly once.
      Algorithm 2.3: Given DATA is a linear array with lower bound LB and
upper bound UB . This algorithm traverses DATA applying an operation
PROCESS to each element of DATA.


              1.   Set K : = LB.
              2.   Repeat steps 3 and 4 while K<=UB:
              3.   Apply PROCESS to DATA[k]
              4.   Set K : = K+1.
              5.   Exit.
Traversing linear arrays
  Example :
  An automobile company uses an array AUTO to record the number of auto
  mobile sold each year from 1932 through 1984.
  a) Find the number NUM of years during which more than 300 automobiles
  were sold.
  b) Print each year and the number of automobiles sold in that year



1. Set NUM : = 0.
2. Repeat for K = 1932 to 1984:                1. Repeat for K = 1932 to 1984:
if AUTO[K]> 300, then : set NUM : = NUM+1      Write : K, AUTO[K]
3. Exit.                                       2. Exit.
Inserting and Deleting
Inserting refers to the operation of adding another element to the Array
Deleting refers to the operation of removing one element from the Array
Inserting an element somewhere in the middle of the array require that each
subsequent element be moved downward to new locations to accommodate the
new element and keep the order of the other elements.
Deleting an element somewhere in the middle of the array require that each
subsequent element be moved one location upward in order to “fill up” the
array. Fig shows Milon Inserted, Sumona deleted.

                                 STUDENT
       STUDENT                                                  STUDENT
                            1   Dalia Rahaman
  1   Dalia Rahaman
                                Sumona                     1   Dalia Rahaman
      Sumona                2
  2                             Milon                      2   Milon
      Mubtasim Fuad         3                                  Mubtasim Fuad
  3                             Mubtasim Fuad              3
      Anamul Haque          4                                  Anamul Haque
  4                             Anamul Haque
                                                           4
                            5
  5                                                        5
                            6
  6                                                        6
Insertion

INSERTING AN ELEMENT INTO AN ARRAY:
Insert (LA, N, K, ITEM)
Here LA is linear array with N elements and K is a positive integer such that
K<=N.This algorithm inserts an element ITEM into the Kth position in LA.
ALGORITHM
Step 1.       [Initialize counter] Set J:=N
Step 2.       Repeat Steps 3 and 4] while J>=K
Step 3.       [Move Jth element downward] Set LA [J+1]: =LA [J]
Step 4.       [Decrease counter] Set J:=J-1
[End of step 2 loop]
Step 5        [Insert element] Set LA [K]: =ITEM
Step 6.       [Reset N] Set N:=N+1
Step 7.       Exit
Deletion
DELETING AN ELEMENT FROM A LINEAR ARRAY
Delete (LA, N, K, ITEM)
ALGORITHM
Step 1.       Set ITEM: = LA [K]
Step 2.       Repeat for J=K to N-1
[Move J+1st element upward] Set LA [J]: =LA [J+1]
[End of loop]
Step 3        [Reset the number N of elements in LA] Set N:=N-1
Step 4.       Exit
Bubble sort
Bubble sort is one of the easiest sort algorithms. It is called bubble sort because
it will 'bubble' values in your list to the top.

 Algorithm Bubble_Sort (DATA, N):

 1.  Repeat steps 2 and 3 for K = 1 to N-1.
 2.  Set PTR: =1.[Initializes pass pointer PTR]
 3.  Repeat while PTR<=N-K: [Executes pass]
 a)  If DATA[PTR]>DATA[PTR+1],then:
         TEMP := DATA[PTR], DATA[PTR] :=
     DATA[PTR+1],DATA[PTR+1] := temp            [End of if structure]
 b) Set PTR: =PTR+1
                   [End of inner loop]
                   [End of step 1 Outer loop]
 4. Exit
Sorting : Bubble sort

• Sorting takes an unordered collection and makes
  it an ordered one.

     1     2       3     4       5      6

    77    42      35     12     101      5



    1      2      3       4      5      6
    5     12      35     42     77     101
"Bubbling Up" the Largest Element

•   Traverse a collection of elements
     – Move from the front to the end
     – “Bubble” the largest value to the end using pair-wise
       comparisons and swapping




         1        2       3       4         5         6

        77      42       35       12       101        5
"Bubbling Up" the Largest Element

•   Traverse a collection of elements
     – Move from the front to the end
     – “Bubble” the largest value to the end using pair-wise
       comparisons and swapping




         1        2       3       4         5         6
        42Swap77                  12       101
        77   42          35                           5
"Bubbling Up" the Largest Element

•   Traverse a collection of elements
     – Move from the front to the end
     – “Bubble” the largest value to the end using pair-wise
       comparisons and swapping




         1        2       3       4         5         6

        42      77 Swap77
                 35    35         12       101        5
"Bubbling Up" the Largest Element

•   Traverse a collection of elements
     – Move from the front to the end
     – “Bubble” the largest value to the end using pair-wise
       comparisons and swapping




         1        2       3       4         5         6
                          12Swap12
                                77         101
        42      35       77                           5
"Bubbling Up" the Largest Element

•   Traverse a collection of elements
     – Move from the front to the end
     – “Bubble” the largest value to the end using pair-wise
       comparisons and swapping




         1        2       3       4         5         6

        42      35       12       77       101        5

                               No need to swap
"Bubbling Up" the Largest Element

•   Traverse a collection of elements
     – Move from the front to the end
     – “Bubble” the largest value to the end using pair-wise
       comparisons and swapping




         1        2       3       4         5         6

        42      35       12       77        5 Swap101
                                           101     5
"Bubbling Up" the Largest Element

•   Traverse a collection of elements
     – Move from the front to the end
     – “Bubble” the largest value to the end using pair-wise
       comparisons and swapping




         1        2       3       4         5         6

        42      35       12       77        5        101

              Largest value correctly placed
Putting It All Together
Items of Interest


• Notice that only the largest value is correctly
  placed
• All other values are still out of order
• So we need to repeat this process


     1       2      3      4        5       6

    42     35      12      77       5      101

         Largest value correctly placed
Repeat “Bubble Up” How Many Times?

• If we have N elements…

• And if each time we bubble an element, we
  place it in its correct location…

• Then we repeat the “bubble up” process N – 1
  times.

• This guarantees we’ll correctly
  place all N elements.
“Bubbling” All the Elements


       1     2      3     4       5       6
      42    35     12     77      5      101
       1     2      3     4       5       6
      35    12     42     5      77      101
       1     2      3     4       5       6
N-1




      12     35     5     42     77      101
       1     2      3     4       5       6
      12     5      35    42     77      101
      1      2      3     4       5       6
      5      12     35    42     77      101
Reducing the Number of Comparisons

 1    2     3    4      5      6
77   42    35    12    101    5
 1    2     3    4      5      6
42   35    12    77     5     101
 1    2     3    4      5      6
35   12    42    5     77     101
 1   2     3     4      5      6
12   35    5     42    77     101
 1   2     3     4      5      6
12   5     35    42    77     101
Summary

• “Bubble Up” algorithm will move largest value to
  its correct location (to the right)
• Repeat “Bubble Up” until all elements are
  correctly placed:
   – Maximum of N-1 times
   – Can finish early if no swapping occurs
• We reduce the number of elements we compare
  each time one is correctly placed
Complexity of the bubble sort algorithm

The time for a sorting algorithm is measured in terms of the number of
comparisons. The number f(n) of comparisons in the bubble sort is easily
computed. Specifically there are n -1 comparisons during first pass, which places
the largest element in the last position, there are n -2 comparisons in the second
step, which places the second largest element in the next – to - last position, and
so on. Thus
f(n) = (n-1)+(n-2)+. . . +2+1 =n(n-1)/2=n2/2+O(n)
In other words, The time required to execute bubble sort algorithm is proportional to
n2, where n is the number of input items.
Selection Sort

•   SelectionSort(A,N)

    for i:=1 to N-1 do

       for j:=i+1 to N-1 do

        if A[i] > A[j] then
                  temp:=A[i]
                  A[i] := A[j]
                  A[j] := temp
Insertion Sort:

•   Insertionsort(A,N)

     for j:=2 to N
        key:=A[j]
        i:=j-1

       while i>0 and A[i] > key do
                 A[i]:=A[i+1]
                 i--
       A[i+1]:=key

Weitere ähnliche Inhalte

Was ist angesagt?

STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTUREArchie Jamwal
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structurestudent
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queueRojan Pariyar
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arraysNeeru Mittal
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applicationssomendra kumar
 
Evaluation of prefix expression with example
Evaluation of prefix expression with exampleEvaluation of prefix expression with example
Evaluation of prefix expression with exampleGADAPURAMSAINIKHIL
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESSowmya Jyothi
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structuresDurgaDeviCbit
 
Fixed point and floating-point numbers
Fixed point and  floating-point numbersFixed point and  floating-point numbers
Fixed point and floating-point numbersMOHAN MOHAN
 
Octal to binary encoder
Octal to binary encoderOctal to binary encoder
Octal to binary encoderAjay844
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeAbhishek L.R
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data StructureZidny Nafan
 

Was ist angesagt? (20)

Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structure
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
 
design of accumlator
design of accumlatordesign of accumlator
design of accumlator
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Singly & Circular Linked list
Singly & Circular Linked listSingly & Circular Linked list
Singly & Circular Linked list
 
Evaluation of prefix expression with example
Evaluation of prefix expression with exampleEvaluation of prefix expression with example
Evaluation of prefix expression with example
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
stack & queue
stack & queuestack & queue
stack & queue
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
 
Fixed point and floating-point numbers
Fixed point and  floating-point numbersFixed point and  floating-point numbers
Fixed point and floating-point numbers
 
2D Array
2D Array 2D Array
2D Array
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Octal to binary encoder
Octal to binary encoderOctal to binary encoder
Octal to binary encoder
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 

Ähnlich wie Data structure lecture 3

BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHISowmya Jyothi
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arraysmaamir farooq
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data StructureMandeep Singh
 
data structure and algorithm Array.pptx btech 2nd year
data structure and algorithm  Array.pptx btech 2nd yeardata structure and algorithm  Array.pptx btech 2nd year
data structure and algorithm Array.pptx btech 2nd yearpalhimanshi999
 
PPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structuresPPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structuresmidtushar
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsAakash deep Singhal
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7Kumar
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptxssuserd602fd
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updatedvrgokila
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptxchin463670
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueRai University
 

Ähnlich wie Data structure lecture 3 (20)

CSE225_LEC3 (1).pptx
CSE225_LEC3 (1).pptxCSE225_LEC3 (1).pptx
CSE225_LEC3 (1).pptx
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data Structure
 
data structure and algorithm Array.pptx btech 2nd year
data structure and algorithm  Array.pptx btech 2nd yeardata structure and algorithm  Array.pptx btech 2nd year
data structure and algorithm Array.pptx btech 2nd year
 
PPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structuresPPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structures
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 
Arrays in C.pptx
Arrays in C.pptxArrays in C.pptx
Arrays in C.pptx
 
Bcsl 033 solve assignment
Bcsl 033 solve assignmentBcsl 033 solve assignment
Bcsl 033 solve assignment
 
sorting.pptx
sorting.pptxsorting.pptx
sorting.pptx
 
Teacher Lecture
Teacher LectureTeacher Lecture
Teacher Lecture
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
Queue
QueueQueue
Queue
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
2.DS Array
2.DS Array2.DS Array
2.DS Array
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 

Mehr von Kumar

Graphics devices
Graphics devicesGraphics devices
Graphics devicesKumar
 
Fill area algorithms
Fill area algorithmsFill area algorithms
Fill area algorithmsKumar
 
region-filling
region-fillingregion-filling
region-fillingKumar
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivationKumar
 
Bresenham circles and polygons derication
Bresenham circles and polygons dericationBresenham circles and polygons derication
Bresenham circles and polygons dericationKumar
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xsltKumar
 
Extracting data from xml
Extracting data from xmlExtracting data from xml
Extracting data from xmlKumar
 
Xml basics
Xml basicsXml basics
Xml basicsKumar
 
XML Schema
XML SchemaXML Schema
XML SchemaKumar
 
Publishing xml
Publishing xmlPublishing xml
Publishing xmlKumar
 
Applying xml
Applying xmlApplying xml
Applying xmlKumar
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XMLKumar
 
How to deploy a j2ee application
How to deploy a j2ee applicationHow to deploy a j2ee application
How to deploy a j2ee applicationKumar
 
JNDI, JMS, JPA, XML
JNDI, JMS, JPA, XMLJNDI, JMS, JPA, XML
JNDI, JMS, JPA, XMLKumar
 
EJB Fundmentals
EJB FundmentalsEJB Fundmentals
EJB FundmentalsKumar
 
JSP and struts programming
JSP and struts programmingJSP and struts programming
JSP and struts programmingKumar
 
java servlet and servlet programming
java servlet and servlet programmingjava servlet and servlet programming
java servlet and servlet programmingKumar
 
Introduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversIntroduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversKumar
 
Introduction to J2EE
Introduction to J2EEIntroduction to J2EE
Introduction to J2EEKumar
 

Mehr von Kumar (20)

Graphics devices
Graphics devicesGraphics devices
Graphics devices
 
Fill area algorithms
Fill area algorithmsFill area algorithms
Fill area algorithms
 
region-filling
region-fillingregion-filling
region-filling
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivation
 
Bresenham circles and polygons derication
Bresenham circles and polygons dericationBresenham circles and polygons derication
Bresenham circles and polygons derication
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xslt
 
Extracting data from xml
Extracting data from xmlExtracting data from xml
Extracting data from xml
 
Xml basics
Xml basicsXml basics
Xml basics
 
XML Schema
XML SchemaXML Schema
XML Schema
 
Publishing xml
Publishing xmlPublishing xml
Publishing xml
 
DTD
DTDDTD
DTD
 
Applying xml
Applying xmlApplying xml
Applying xml
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
How to deploy a j2ee application
How to deploy a j2ee applicationHow to deploy a j2ee application
How to deploy a j2ee application
 
JNDI, JMS, JPA, XML
JNDI, JMS, JPA, XMLJNDI, JMS, JPA, XML
JNDI, JMS, JPA, XML
 
EJB Fundmentals
EJB FundmentalsEJB Fundmentals
EJB Fundmentals
 
JSP and struts programming
JSP and struts programmingJSP and struts programming
JSP and struts programming
 
java servlet and servlet programming
java servlet and servlet programmingjava servlet and servlet programming
java servlet and servlet programming
 
Introduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversIntroduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC Drivers
 
Introduction to J2EE
Introduction to J2EEIntroduction to J2EE
Introduction to J2EE
 

Kürzlich hochgeladen

"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 

Kürzlich hochgeladen (20)

"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 

Data structure lecture 3

  • 1. Lecture -3 on Data structures Array
  • 2. Array Data structures are classified as either linear or nonlinear. A data structure is said to be linear if its elements form a sequence or a linear list. There are two basic ways of representing such linear structures in memory. One way is to have the linear relationship between the elements represented by means of sequential memory locations. These linear structures are called arrays. The other way is to have the linear relationship between the elements represented by means of pointers or links. These linear structures are called linked lists. Nonlinear structures are trees and graphs.
  • 3. Linear Arrays A linear array is a list of finite number n of homogeneous data elements such that : a) The elements of the array are referenced respectively by an index set consisting of n consecutive numbers. b) The elements of the array are stored respectively in successive memory locations. The number n of elements is called the length or size of the array. Three numbers define an array : lower bound, upper bound, size. a. The lower bound is the smallest subscript you can use in the array (usually 0) b. The upper bound is the largest subscript you can use in the array c. The size / length of the array refers to the number of elements in the array , It can be computed as upper bound - lower bound + 1 Let, Array name is A then the elements of A is : a1,a2….. an Or by the bracket notation A[1], A[2], A[3],…………., A[n] The number k in A[k] is called a subscript and A[k] is called a subscripted variable.
  • 4. Linear Arrays Example : A linear array DATA consisting of the name of six elements DATA DATA[1] = 247 1 247 DATA[2] = 56 2 56 3 DATA[3] = 429 429 4 135 DATA[4] = 135 5 87 DATA[5] = 87 6 156 DATA[6] = 156
  • 5. Linear Arrays Example : An automobile company uses an array AUTO to record the number of auto mobile sold each year from 1932 through 1984. AUTO[k] = Number of auto mobiles sold in the year K LB = 1932 UB = 1984 Length = UB – LB+1 = 1984 – 1930+1 =55
  • 6. Representation of linear array in memory Let LA be a linear array in the memory of the computer. The memory of the computer is a sequence of addressed locations. LA 1000 1001 The computer does not need to keep track of the 1002 address of every element of LA, but needs to keep 1003 track only of the first element of LA, denoted by 1004 Base(LA) 1005 Called the base address of LA. Using this address Base(LA), the computer calculates the address of any element of LA by the following formula : LOC(LA[k]) = Base(LA) + w(K – lower bound) Where w is the number of words per memory cell for Fig : Computer memory the array LA
  • 7. Representation of linear array in memory 200 201 Example : An automobile company uses an array AUTO to record 202 the number of auto mobile sold each year from 1932 203 AUTO[1932] through 1984. Suppose AUTO appears in memory as 204 pictured in fig A . That is Base(AUTO) = 200, and w = 4 205 words per memory cell for AUTO. Then, 206 LOC(AUTO[1932]) = 200, LOC(AUTO[1933]) =204 207 AUTO[1933] LOC(AUTO[1934]) = 208 the address of the array element for the year K = 1965 208 can be obtained by using : 209 LOC(AUTO[1965]) = Base(AUTO) + w(1965 – lower 210 AUTO[1934] bound) 211 =200+4(1965-1932)=332 212 Fig : A
  • 8. Traversing linear arrays Print the contents of each element of DATA or Count the number of elements of DATA with a given property. This can be accomplished by traversing DATA, That is, by accessing and processing (visiting) each element of DATA exactly once. Algorithm 2.3: Given DATA is a linear array with lower bound LB and upper bound UB . This algorithm traverses DATA applying an operation PROCESS to each element of DATA. 1. Set K : = LB. 2. Repeat steps 3 and 4 while K<=UB: 3. Apply PROCESS to DATA[k] 4. Set K : = K+1. 5. Exit.
  • 9. Traversing linear arrays Example : An automobile company uses an array AUTO to record the number of auto mobile sold each year from 1932 through 1984. a) Find the number NUM of years during which more than 300 automobiles were sold. b) Print each year and the number of automobiles sold in that year 1. Set NUM : = 0. 2. Repeat for K = 1932 to 1984: 1. Repeat for K = 1932 to 1984: if AUTO[K]> 300, then : set NUM : = NUM+1 Write : K, AUTO[K] 3. Exit. 2. Exit.
  • 10. Inserting and Deleting Inserting refers to the operation of adding another element to the Array Deleting refers to the operation of removing one element from the Array Inserting an element somewhere in the middle of the array require that each subsequent element be moved downward to new locations to accommodate the new element and keep the order of the other elements. Deleting an element somewhere in the middle of the array require that each subsequent element be moved one location upward in order to “fill up” the array. Fig shows Milon Inserted, Sumona deleted. STUDENT STUDENT STUDENT 1 Dalia Rahaman 1 Dalia Rahaman Sumona 1 Dalia Rahaman Sumona 2 2 Milon 2 Milon Mubtasim Fuad 3 Mubtasim Fuad 3 Mubtasim Fuad 3 Anamul Haque 4 Anamul Haque 4 Anamul Haque 4 5 5 5 6 6 6
  • 11. Insertion INSERTING AN ELEMENT INTO AN ARRAY: Insert (LA, N, K, ITEM) Here LA is linear array with N elements and K is a positive integer such that K<=N.This algorithm inserts an element ITEM into the Kth position in LA. ALGORITHM Step 1. [Initialize counter] Set J:=N Step 2. Repeat Steps 3 and 4] while J>=K Step 3. [Move Jth element downward] Set LA [J+1]: =LA [J] Step 4. [Decrease counter] Set J:=J-1 [End of step 2 loop] Step 5 [Insert element] Set LA [K]: =ITEM Step 6. [Reset N] Set N:=N+1 Step 7. Exit
  • 12. Deletion DELETING AN ELEMENT FROM A LINEAR ARRAY Delete (LA, N, K, ITEM) ALGORITHM Step 1. Set ITEM: = LA [K] Step 2. Repeat for J=K to N-1 [Move J+1st element upward] Set LA [J]: =LA [J+1] [End of loop] Step 3 [Reset the number N of elements in LA] Set N:=N-1 Step 4. Exit
  • 13.
  • 14. Bubble sort Bubble sort is one of the easiest sort algorithms. It is called bubble sort because it will 'bubble' values in your list to the top. Algorithm Bubble_Sort (DATA, N): 1. Repeat steps 2 and 3 for K = 1 to N-1. 2. Set PTR: =1.[Initializes pass pointer PTR] 3. Repeat while PTR<=N-K: [Executes pass] a) If DATA[PTR]>DATA[PTR+1],then: TEMP := DATA[PTR], DATA[PTR] := DATA[PTR+1],DATA[PTR+1] := temp [End of if structure] b) Set PTR: =PTR+1 [End of inner loop] [End of step 1 Outer loop] 4. Exit
  • 15. Sorting : Bubble sort • Sorting takes an unordered collection and makes it an ordered one. 1 2 3 4 5 6 77 42 35 12 101 5 1 2 3 4 5 6 5 12 35 42 77 101
  • 16. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 77 42 35 12 101 5
  • 17. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 42Swap77 12 101 77 42 35 5
  • 18. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 42 77 Swap77 35 35 12 101 5
  • 19. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 12Swap12 77 101 42 35 77 5
  • 20. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 42 35 12 77 101 5 No need to swap
  • 21. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 42 35 12 77 5 Swap101 101 5
  • 22. "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 42 35 12 77 5 101 Largest value correctly placed
  • 23. Putting It All Together
  • 24. Items of Interest • Notice that only the largest value is correctly placed • All other values are still out of order • So we need to repeat this process 1 2 3 4 5 6 42 35 12 77 5 101 Largest value correctly placed
  • 25. Repeat “Bubble Up” How Many Times? • If we have N elements… • And if each time we bubble an element, we place it in its correct location… • Then we repeat the “bubble up” process N – 1 times. • This guarantees we’ll correctly place all N elements.
  • 26. “Bubbling” All the Elements 1 2 3 4 5 6 42 35 12 77 5 101 1 2 3 4 5 6 35 12 42 5 77 101 1 2 3 4 5 6 N-1 12 35 5 42 77 101 1 2 3 4 5 6 12 5 35 42 77 101 1 2 3 4 5 6 5 12 35 42 77 101
  • 27. Reducing the Number of Comparisons 1 2 3 4 5 6 77 42 35 12 101 5 1 2 3 4 5 6 42 35 12 77 5 101 1 2 3 4 5 6 35 12 42 5 77 101 1 2 3 4 5 6 12 35 5 42 77 101 1 2 3 4 5 6 12 5 35 42 77 101
  • 28. Summary • “Bubble Up” algorithm will move largest value to its correct location (to the right) • Repeat “Bubble Up” until all elements are correctly placed: – Maximum of N-1 times – Can finish early if no swapping occurs • We reduce the number of elements we compare each time one is correctly placed
  • 29. Complexity of the bubble sort algorithm The time for a sorting algorithm is measured in terms of the number of comparisons. The number f(n) of comparisons in the bubble sort is easily computed. Specifically there are n -1 comparisons during first pass, which places the largest element in the last position, there are n -2 comparisons in the second step, which places the second largest element in the next – to - last position, and so on. Thus f(n) = (n-1)+(n-2)+. . . +2+1 =n(n-1)/2=n2/2+O(n) In other words, The time required to execute bubble sort algorithm is proportional to n2, where n is the number of input items.
  • 30. Selection Sort • SelectionSort(A,N) for i:=1 to N-1 do for j:=i+1 to N-1 do if A[i] > A[j] then temp:=A[i] A[i] := A[j] A[j] := temp
  • 31. Insertion Sort: • Insertionsort(A,N) for j:=2 to N key:=A[j] i:=j-1 while i>0 and A[i] > key do A[i]:=A[i+1] i-- A[i+1]:=key