SlideShare a Scribd company logo
1 of 12
Lecture 4
     on
Data Structure

    Array
Searching : Linear search
Searching refers to the operation of finding the location LOC of ITEM in DATA, or printing
some message that ITEM does not appear there.
DATA is a linear array with n elements. The most intuitive way to search for a given ITEM in
DATA is to compare ITEM with each element of DATA one by one. That is first we test
whether DATA[1 ]=ITEM, and then we test whether DATA[2 ]=ITEM, and so on. This
method, which traverses DATA sequentially to locate ITEM, is called linear search
or sequential search.

  Algorithm 4.5 : A linear array DATA with N elements and a specific ITEM
  of information are given. This algorithm finds the location LOC of ITEM in
  the array DATA or sets LOC = 0.
              1. Set DATA[N+1]:=ITEM.
              2. Set LOC:=1.
              3. Repeat while DATA[LOC]! =ITEM :
                    Set LOC := LOC +1.
                   [End of loop]
              4. If LOC = N+1, then :
                  Write : ITEM is not in the array DATA.
                  Else :
                  Write : LOC is the location of ITEM.
                        5.Exit.
Complexity of Linear search
•   Measured by the number f(n) of comparisons required to find ITEM in
    the DATA array. Two important case:
    – Average case:
        • Suppose pk is the probability that ITEM appears in DATA[k], and
        • q is the probability that ITEM does not appears in DATA.
             – Then p1+ p2+ p3+ p4+ … pn+ q = 1 (Total probability)
        • Average number of comparisons can be calculated by-
             – f(n) = 1. p1 + 2. p1+ 3. p1+ ……………….+ n . pn+ (n+1). q
             – Let, q is very small q0 and item appears in equal probability then pi= 1/n
                                1          1        1
                     f (n) = 1 ⋅ + 2 ⋅ + ⋅ ⋅ ⋅ + n ⋅ + (n + 1) ⋅ 0
                                n          n        n
                                                1 n(n + 1) 1 n + 1
                     = (1 + 2 + 3 + ⋅ ⋅ ⋅ + n) ⋅ =        ⋅ =
                                                n    2     n       2

    – Worse case: when the search occurs through the entire array, DATA. i.e.
      When the ITEM does not appar in the array DATA
        • It requires f(n)= n+1
        • In this case, the running time is proportional to n
Binary Search Algorithm
BINARY(DATA, LB, UB, ITEM, LOC)

1. Set BEG=LB; END=UB; and MID=INT((BEG+END)/2).
2. Repeat step 3 and 4 while BEG ≤ END and DATA[MID] ≠ ITEM
3. If ITEM < DATA[MID] then
       Set END= MID - 1
    Else:
        Set BEG= MID +1
   [end of if structure]
4. Set MID= INT((BEG+END)/2)
   [End of step 2 loop]
5. If ITEM = DATA[MID] then
       Set LOC= MID
    Else:
        Set LOC= NULL
   [end of if structure]
6. Exit.
Binary Search example (Seek for 123)
Binary Search - Complexity

• Often not interested in best case.
• Worst case:
  –   Loop executes until BEG <= END
  –   Size halved in each iteration
  –   N, N/2, N/4, …N/2K…. 1
  –   How many steps ?
Binary Search - Complexity

• Worst case:
  – N/2K = 1 i.e. 2K = N
  – Which gives K=log2N steps, which is O(log2(N))
  – This is considered very fast when compared to linear
Binary Search - Complexity

• Average case:
  – 1st iteration: 1 possible value
  – 2nd iteration: 2 possible values (left or right half)
  – 3rd iteration: 4 possible values (left of left, right of left, right of right,
    right of left)
  – ith iteration: 2i-1 possible values
Binary Search - Complexity

• Average Case:
  1 + 2 + 2 + 3 + 3 + 3 + 3 + … (upto log N steps)

     1 element can be found with 1 comparison
     2 elements                          2
     4 elements                          3
     Above Sum = sum over all possibilities
      = Σi=0 to log N (i*2i-1) = O (log N)
Multidimensional arrays
Two dimensional, three dimensional arrays and ….. Where elements are
referenced respectively by two, three and ……subscripts.
Two – dimensional Arrays
A Two – dimensional Arrays m x n array A is a collection of m . n data
elements such that each element is specified by a pair of integers (such as
J, K), called subscripts.
The element of A with first subscript J and second subscript K will be
denoted by A[J, K]
                           Columns

                       1         2         3
              1       A[1, 1]   A[1, 2]   A[1, 3]
   Rows       2
                      A[2, 1]   A[2, 2] A[2, 3]
              3       A[3, 1]   A[3, 2] A[3, 3]


              Fig: Two dimensional 3 x 3 array A
Matrix Multiplication
Algorithm 4.7: MATMUL(A, B, C, M, P, N)
Let A be an MXP matrix array, and let B be a PXN matrix array. This algorithm
stores the product of A and B in an MXN matrix array.

          1.   Repeat steps 2 to 4 for I =1 to M:
          2.   Repeat steps 3 to 4 for J =1 to N:
          3.   Set C[I, J]:=0
          4.   Repeat for K =1 to P:
          5.   C[I, J]:= C[I, J]:+A[I, K]*B[K, J]
          6.   Exit

          See solved problem 4.12.
Solved Problem

4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 4.10,4.12

Supplementary problems:
4.25

More Related Content

What's hot

Top schools in gudgao
Top schools in gudgaoTop schools in gudgao
Top schools in gudgaoEdhole.com
 
Search algorithms master
Search algorithms masterSearch algorithms master
Search algorithms masterHossam Hassan
 
Row major and column major in 2 d
Row major and column major in 2 dRow major and column major in 2 d
Row major and column major in 2 dnikhilarora2211
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structurekalyanineve
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structuresMohd Arif
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms ArraysManishPrajapati78
 
Application of hashing in better alg design tanmay
Application of hashing in better alg design tanmayApplication of hashing in better alg design tanmay
Application of hashing in better alg design tanmayTanmay 'Unsinkable'
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsAakash deep Singhal
 
5.5 back track
5.5 back track5.5 back track
5.5 back trackKrish_ver2
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsAakash deep Singhal
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data StructureMandeep Singh
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDev Chauhan
 
2 1 Bzca5e
2 1 Bzca5e2 1 Bzca5e
2 1 Bzca5esilvia
 

What's hot (20)

arrays
arraysarrays
arrays
 
Top schools in gudgao
Top schools in gudgaoTop schools in gudgao
Top schools in gudgao
 
Search algorithms master
Search algorithms masterSearch algorithms master
Search algorithms master
 
Row major and column major in 2 d
Row major and column major in 2 dRow major and column major in 2 d
Row major and column major in 2 d
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structure
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structures
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
 
Application of hashing in better alg design tanmay
Application of hashing in better alg design tanmayApplication of hashing in better alg design tanmay
Application of hashing in better alg design tanmay
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithms
 
5.5 back track
5.5 back track5.5 back track
5.5 back track
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithms
 
Data structures
Data structuresData structures
Data structures
 
Section3 Prologppt
Section3 PrologpptSection3 Prologppt
Section3 Prologppt
 
Arrays
ArraysArrays
Arrays
 
Data structure ppt
Data structure pptData structure ppt
Data structure ppt
 
Big o notation
Big o notationBig o notation
Big o notation
 
Set Theory QA 3
Set Theory QA 3Set Theory QA 3
Set Theory QA 3
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data Structure
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
 
2 1 Bzca5e
2 1 Bzca5e2 1 Bzca5e
2 1 Bzca5e
 

Viewers also liked

data structure, stack, stack data structure
data structure, stack, stack data structuredata structure, stack, stack data structure
data structure, stack, stack data structurepcnmtutorials
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structurestudent
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arorakulachihansraj
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structureeShikshak
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application Tech_MX
 
Fundamentals of Array Antenna
Fundamentals of Array AntennaFundamentals of Array Antenna
Fundamentals of Array AntennaYong Heui Cho
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueFarhanum Aziera
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data StructureZidny Nafan
 

Viewers also liked (11)

Stack Operation In Data Structure
Stack Operation In Data Structure Stack Operation In Data Structure
Stack Operation In Data Structure
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
 
data structure, stack, stack data structure
data structure, stack, stack data structuredata structure, stack, stack data structure
data structure, stack, stack data structure
 
Data Structure (Stack)
Data Structure (Stack)Data Structure (Stack)
Data Structure (Stack)
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structure
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
Fundamentals of Array Antenna
Fundamentals of Array AntennaFundamentals of Array Antenna
Fundamentals of Array Antenna
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 

Similar to Data structure lecture 4

Fundamental computing algorithms
Fundamental computing algorithmsFundamental computing algorithms
Fundamental computing algorithmsGanesh Solanke
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1smruti sarangi
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfShiwani Gupta
 
lecture 10
lecture 10lecture 10
lecture 10sajinsc
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024RUHULAMINHAZARIKA
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsDrishti Bhalla
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptxchouguleamruta24
 
Array 2
Array 2Array 2
Array 2Abbott
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptxssuserd602fd
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptxSajalFayyaz
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03Krish_ver2
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03Krish_ver2
 

Similar to Data structure lecture 4 (20)

searching.pdf
searching.pdfsearching.pdf
searching.pdf
 
Fundamental computing algorithms
Fundamental computing algorithmsFundamental computing algorithms
Fundamental computing algorithms
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
 
Sorting
SortingSorting
Sorting
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
 
lecture 10
lecture 10lecture 10
lecture 10
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
1D Array
1D Array1D Array
1D Array
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
Array 2
Array 2Array 2
Array 2
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
Tower of Hanoi.ppt
Tower of Hanoi.pptTower of Hanoi.ppt
Tower of Hanoi.ppt
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptx
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 

More from 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
 

More from 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
 

Recently uploaded

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
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
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
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
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
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 

Recently uploaded (20)

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
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
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
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
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
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 

Data structure lecture 4

  • 1. Lecture 4 on Data Structure Array
  • 2. Searching : Linear search Searching refers to the operation of finding the location LOC of ITEM in DATA, or printing some message that ITEM does not appear there. DATA is a linear array with n elements. The most intuitive way to search for a given ITEM in DATA is to compare ITEM with each element of DATA one by one. That is first we test whether DATA[1 ]=ITEM, and then we test whether DATA[2 ]=ITEM, and so on. This method, which traverses DATA sequentially to locate ITEM, is called linear search or sequential search. Algorithm 4.5 : A linear array DATA with N elements and a specific ITEM of information are given. This algorithm finds the location LOC of ITEM in the array DATA or sets LOC = 0. 1. Set DATA[N+1]:=ITEM. 2. Set LOC:=1. 3. Repeat while DATA[LOC]! =ITEM : Set LOC := LOC +1. [End of loop] 4. If LOC = N+1, then : Write : ITEM is not in the array DATA. Else : Write : LOC is the location of ITEM. 5.Exit.
  • 3. Complexity of Linear search • Measured by the number f(n) of comparisons required to find ITEM in the DATA array. Two important case: – Average case: • Suppose pk is the probability that ITEM appears in DATA[k], and • q is the probability that ITEM does not appears in DATA. – Then p1+ p2+ p3+ p4+ … pn+ q = 1 (Total probability) • Average number of comparisons can be calculated by- – f(n) = 1. p1 + 2. p1+ 3. p1+ ……………….+ n . pn+ (n+1). q – Let, q is very small q0 and item appears in equal probability then pi= 1/n 1 1 1 f (n) = 1 ⋅ + 2 ⋅ + ⋅ ⋅ ⋅ + n ⋅ + (n + 1) ⋅ 0 n n n 1 n(n + 1) 1 n + 1 = (1 + 2 + 3 + ⋅ ⋅ ⋅ + n) ⋅ = ⋅ = n 2 n 2 – Worse case: when the search occurs through the entire array, DATA. i.e. When the ITEM does not appar in the array DATA • It requires f(n)= n+1 • In this case, the running time is proportional to n
  • 4. Binary Search Algorithm BINARY(DATA, LB, UB, ITEM, LOC) 1. Set BEG=LB; END=UB; and MID=INT((BEG+END)/2). 2. Repeat step 3 and 4 while BEG ≤ END and DATA[MID] ≠ ITEM 3. If ITEM < DATA[MID] then Set END= MID - 1 Else: Set BEG= MID +1 [end of if structure] 4. Set MID= INT((BEG+END)/2) [End of step 2 loop] 5. If ITEM = DATA[MID] then Set LOC= MID Else: Set LOC= NULL [end of if structure] 6. Exit.
  • 5. Binary Search example (Seek for 123)
  • 6. Binary Search - Complexity • Often not interested in best case. • Worst case: – Loop executes until BEG <= END – Size halved in each iteration – N, N/2, N/4, …N/2K…. 1 – How many steps ?
  • 7. Binary Search - Complexity • Worst case: – N/2K = 1 i.e. 2K = N – Which gives K=log2N steps, which is O(log2(N)) – This is considered very fast when compared to linear
  • 8. Binary Search - Complexity • Average case: – 1st iteration: 1 possible value – 2nd iteration: 2 possible values (left or right half) – 3rd iteration: 4 possible values (left of left, right of left, right of right, right of left) – ith iteration: 2i-1 possible values
  • 9. Binary Search - Complexity • Average Case: 1 + 2 + 2 + 3 + 3 + 3 + 3 + … (upto log N steps)  1 element can be found with 1 comparison  2 elements  2  4 elements  3  Above Sum = sum over all possibilities = Σi=0 to log N (i*2i-1) = O (log N)
  • 10. Multidimensional arrays Two dimensional, three dimensional arrays and ….. Where elements are referenced respectively by two, three and ……subscripts. Two – dimensional Arrays A Two – dimensional Arrays m x n array A is a collection of m . n data elements such that each element is specified by a pair of integers (such as J, K), called subscripts. The element of A with first subscript J and second subscript K will be denoted by A[J, K] Columns 1 2 3 1 A[1, 1] A[1, 2] A[1, 3] Rows 2 A[2, 1] A[2, 2] A[2, 3] 3 A[3, 1] A[3, 2] A[3, 3] Fig: Two dimensional 3 x 3 array A
  • 11. Matrix Multiplication Algorithm 4.7: MATMUL(A, B, C, M, P, N) Let A be an MXP matrix array, and let B be a PXN matrix array. This algorithm stores the product of A and B in an MXN matrix array. 1. Repeat steps 2 to 4 for I =1 to M: 2. Repeat steps 3 to 4 for J =1 to N: 3. Set C[I, J]:=0 4. Repeat for K =1 to P: 5. C[I, J]:= C[I, J]:+A[I, K]*B[K, J] 6. Exit See solved problem 4.12.
  • 12. Solved Problem 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 4.10,4.12 Supplementary problems: 4.25