SlideShare a Scribd company logo
1 of 16
COMPUTER
PROGRAMMING
Lecture 06
Prepared By Mr. V. S. Patil Dept (CSE)/AEC
Unit 1 Problem Solving
Syllabus
Prepared By Mr. V. S. Patil Dept (CSE)/AEC
2
1
Understanding Linear Search
Understanding Binary Search
Objectives
Linear Search: A Simple Search
•A search traverses the collection until
–The desired element is found
–Or the collection is exhausted
•If the collection is ordered, I might not have to look at all
elements
–I can stop looking when I know the element cannot be in
the collection.
Algorithm of Linear search
(Linear Search) LINEAR(DATA, N, ITEM, LOC)
Here DATA is an linear array with N element and ITEM is a given
item of information .This algorithm find the location LOC of
ITEM in the DATA or Sets LOC:=0. IF Search is unsuccessful.
1. [Insert ITEM at the end of DATA].
Set DATA[N+1]:=ITEM
2. [Initialize counter] set LOC:=1
1
2
Continue..
3. [Search for item].
Repeat while DATA[LOC]=!ITEM
Set LOC:=LOC+1
[End of loop]
4 [Successful?] if LOC=N+1 then:
Set LOC:=0.
Exit
3
4
5
Linear Search
7 12 5 22 13 32DAT
A
1 2 3 4 5 6
DAT
A
7 12 5 22 13 32
1 2 3 4 5 6
7 12 5 22 13 32
1 2 3 4 5 6
7 12 5 22 13 32
1 2 3 4 5 6
ITEM= 13
7 12 5 22 13 32
1 2 3 4 5 6
Item Found at
LOC=5
DAT
A
DAT
A
DAT
A
Video of linear search
• View video
Binary Search
•BINARY(DATA, N, ITEM, Lo, Hi, Mid)
Here DATA is an array with N element and ITEM is a given
item of information .This algorithm find the location ITEM in
the DATA.
1.Initially Lo=0 and Hi= N+1
2.Find Mid=(Lo+Hi)/2
3.If(ITEM<Mid)
4.Hi=Mid-1
5.Repeat step 2
Continue..
6. If(ITEM>Mid)
7. Lo=Lo+Mid
8. Repeat step 2
9. if(mid==ITEM)
10.ITEM found at Mid
11. Else ITEM not in list
12.Stop
Binary Search
• (Binary search. Given value and sorted array a[ ], find
index i
such that a[i] = value, or report that no such index exists.
• Invariant. Algorithm maintains a[lo] ≤ value ≤ a[hi].
• Ex. Binary search for 33.
Binary Search
Binary Search
Binary Search
ITEM= 33 Found at 4th
position
Binary search video
Question Bank
1. What is mean by searching?
• Explain linear search with suitable example
1. With the help of binary search algorithm find 7 in given
list.
12 45 34 67 98 42 23 7 87

More Related Content

Viewers also liked

COMPUTER PROGRAMMING UNIT 1 Lecture 5
COMPUTER PROGRAMMING UNIT 1 Lecture 5COMPUTER PROGRAMMING UNIT 1 Lecture 5
COMPUTER PROGRAMMING UNIT 1 Lecture 5Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4Vishal Patil
 
Database system by VISHAL PATIL
Database system by VISHAL PATILDatabase system by VISHAL PATIL
Database system by VISHAL PATILVishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1Vishal Patil
 
Important Questions of fourier series with theoretical study Engg. Mathem...
Important Questions  of  fourier series with theoretical study   Engg. Mathem...Important Questions  of  fourier series with theoretical study   Engg. Mathem...
Important Questions of fourier series with theoretical study Engg. Mathem...Mohammad Imran
 
COMPUTER PROGRAMMING UNIT 1 Lecture 3
COMPUTER PROGRAMMING UNIT 1 Lecture 3COMPUTER PROGRAMMING UNIT 1 Lecture 3
COMPUTER PROGRAMMING UNIT 1 Lecture 3Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 2
COMPUTER PROGRAMMING UNIT 1 Lecture 2COMPUTER PROGRAMMING UNIT 1 Lecture 2
COMPUTER PROGRAMMING UNIT 1 Lecture 2Vishal Patil
 

Viewers also liked (9)

COMPUTER PROGRAMMING UNIT 1 Lecture 5
COMPUTER PROGRAMMING UNIT 1 Lecture 5COMPUTER PROGRAMMING UNIT 1 Lecture 5
COMPUTER PROGRAMMING UNIT 1 Lecture 5
 
COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4
 
Database system by VISHAL PATIL
Database system by VISHAL PATILDatabase system by VISHAL PATIL
Database system by VISHAL PATIL
 
COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1
 
Important Questions of fourier series with theoretical study Engg. Mathem...
Important Questions  of  fourier series with theoretical study   Engg. Mathem...Important Questions  of  fourier series with theoretical study   Engg. Mathem...
Important Questions of fourier series with theoretical study Engg. Mathem...
 
COMPUTER PROGRAMMING UNIT 1 Lecture 3
COMPUTER PROGRAMMING UNIT 1 Lecture 3COMPUTER PROGRAMMING UNIT 1 Lecture 3
COMPUTER PROGRAMMING UNIT 1 Lecture 3
 
COMPUTER PROGRAMMING UNIT 1 Lecture 2
COMPUTER PROGRAMMING UNIT 1 Lecture 2COMPUTER PROGRAMMING UNIT 1 Lecture 2
COMPUTER PROGRAMMING UNIT 1 Lecture 2
 
Computer Programming- Lecture 5
Computer Programming- Lecture 5 Computer Programming- Lecture 5
Computer Programming- Lecture 5
 
Sucess
SucessSucess
Sucess
 

Similar to COMPUTER PROGRAMMING UNIT 1 Lecture 6

Similar to COMPUTER PROGRAMMING UNIT 1 Lecture 6 (20)

search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
 
seaching internal 2 ppt.pptx
seaching internal 2 ppt.pptxseaching internal 2 ppt.pptx
seaching internal 2 ppt.pptx
 
Ocw chp6 2searchbinary
Ocw chp6 2searchbinaryOcw chp6 2searchbinary
Ocw chp6 2searchbinary
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
Linear and binary search
Linear and binary searchLinear and binary search
Linear and binary search
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
 
Data structure
Data structureData structure
Data structure
 
Algorithm and Programming (Searching)
Algorithm and Programming (Searching)Algorithm and Programming (Searching)
Algorithm and Programming (Searching)
 
Array operations
Array operationsArray operations
Array operations
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
dsa pdf.pdf
dsa pdf.pdfdsa pdf.pdf
dsa pdf.pdf
 
Array 2
Array 2Array 2
Array 2
 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdf
 
8.binry search tree
8.binry search tree8.binry search tree
8.binry search tree
 
Updated Lab3.docx
Updated Lab3.docxUpdated Lab3.docx
Updated Lab3.docx
 
Sorting
SortingSorting
Sorting
 
ds 2Arrays.ppt
ds 2Arrays.pptds 2Arrays.ppt
ds 2Arrays.ppt
 
Searching.ppt
Searching.pptSearching.ppt
Searching.ppt
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
 
Searching
SearchingSearching
Searching
 

Recently uploaded

Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating SystemRashmi Bhat
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...Amil Baba Dawood bangali
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Industrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptIndustrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptNarmatha D
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxRomil Mishra
 

Recently uploaded (20)

Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating System
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Industrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptIndustrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.ppt
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptx
 

COMPUTER PROGRAMMING UNIT 1 Lecture 6

  • 1. COMPUTER PROGRAMMING Lecture 06 Prepared By Mr. V. S. Patil Dept (CSE)/AEC
  • 2. Unit 1 Problem Solving Syllabus Prepared By Mr. V. S. Patil Dept (CSE)/AEC
  • 4. Linear Search: A Simple Search •A search traverses the collection until –The desired element is found –Or the collection is exhausted •If the collection is ordered, I might not have to look at all elements –I can stop looking when I know the element cannot be in the collection.
  • 5. Algorithm of Linear search (Linear Search) LINEAR(DATA, N, ITEM, LOC) Here DATA is an linear array with N element and ITEM is a given item of information .This algorithm find the location LOC of ITEM in the DATA or Sets LOC:=0. IF Search is unsuccessful. 1. [Insert ITEM at the end of DATA]. Set DATA[N+1]:=ITEM 2. [Initialize counter] set LOC:=1 1 2
  • 6. Continue.. 3. [Search for item]. Repeat while DATA[LOC]=!ITEM Set LOC:=LOC+1 [End of loop] 4 [Successful?] if LOC=N+1 then: Set LOC:=0. Exit 3 4 5
  • 7. Linear Search 7 12 5 22 13 32DAT A 1 2 3 4 5 6 DAT A 7 12 5 22 13 32 1 2 3 4 5 6 7 12 5 22 13 32 1 2 3 4 5 6 7 12 5 22 13 32 1 2 3 4 5 6 ITEM= 13 7 12 5 22 13 32 1 2 3 4 5 6 Item Found at LOC=5 DAT A DAT A DAT A
  • 8. Video of linear search • View video
  • 9. Binary Search •BINARY(DATA, N, ITEM, Lo, Hi, Mid) Here DATA is an array with N element and ITEM is a given item of information .This algorithm find the location ITEM in the DATA. 1.Initially Lo=0 and Hi= N+1 2.Find Mid=(Lo+Hi)/2 3.If(ITEM<Mid) 4.Hi=Mid-1 5.Repeat step 2
  • 10. Continue.. 6. If(ITEM>Mid) 7. Lo=Lo+Mid 8. Repeat step 2 9. if(mid==ITEM) 10.ITEM found at Mid 11. Else ITEM not in list 12.Stop
  • 11. Binary Search • (Binary search. Given value and sorted array a[ ], find index i such that a[i] = value, or report that no such index exists. • Invariant. Algorithm maintains a[lo] ≤ value ≤ a[hi]. • Ex. Binary search for 33.
  • 14. Binary Search ITEM= 33 Found at 4th position
  • 16. Question Bank 1. What is mean by searching? • Explain linear search with suitable example 1. With the help of binary search algorithm find 7 in given list. 12 45 34 67 98 42 23 7 87