SlideShare a Scribd company logo
Government Polytechnic Lucknow
INFORMATION
TECHNOLOGY 2ND YEAR
Presented by : Krishnanand Mishra, Sumit Shukla , Ankur Kumar , Utkarsh Tiwari
Data structure using C
Searching
Linear
search
Binary
search
Searching
Searching in Array
Searching in array
means to find
whether a
particular value is
present in array or
not.
If the desired value is
present in the array,
then searching is said to
be successful. And the
searching process gives
the location of that
value in the array.
If the desired
value is not
present in the
array, then
searching is said
to be
unsuccessful.
There are two
popular methods
for searching the
array elements.
Linear
search
Binary
search
Linear Search
It is a very basic and simple search algorithm.
In linear search we search an element in given array by traversing the array from the
starting till the desired element is found.
Linear search is mostly used to search an unordered list of elements.
like : A[ ] = { 10,8,4,7,6,2,5,1,9};
Algorithm
Step 1 - Read the search element from the user.
Step 2 - Compare the search element with the first element in the list.
Step 3 - If both are matched, then display "given element is found!!!" And terminate the function
Step 4 - If both are not matched, then compare search element with the next element in the list.
Step 5 - Repeat steps 3 and 4 until search element is compared with last element in the list.
Step 6 - If last element in the list also doesn't match, then display "element is not found!!!" And terminate the
Example
Code
Int linear_search ( a[ ], LB, UB, item ) // LB= Lower bound, UB= upper bound,
{
int k;
for (k = LB ; k <= UB; K++)
{
if ( a [k] == item )
{
return k ;
}
}
return LB -1;
}
Binary search
Binary search is the search technique that works efficiently on sorted lists.
To search an element into some list using the binary search technique, we must ensure that
the list is sorted.
Binary search follows the divide and conquer approach in which the list is divided into two
halves, and the item is compared with the middle element of the list.
If the match is found then, the location of the middle element is returned. Otherwise, we
search into either of the halves depending upon the result produced through the match.
Algorithm
Step 1 - Start searching data from middle of the list.
Step 2 – If it is a match, return the index of the item, and exit.
Step 3 – If it is not a match, probe position.
Step 4 – Divide the list and find the new middle.
Step 5 – If data is greater than middle, search in higher sub-list.
Step 6 – If data is smaller than middle, search in lower sub-list.
Step 7 – Repeat until match
Example
.
Code
Int binary_ search ( A[ ], LB, UB, item ) // LB= Lower bound, UB= Upper bound,
{ int low=LB, high=UB;
int mid=(low + high) / 2;
while( (A[mid]!= item) && ( low<=high ) )
{ if( item<A[mid] )
high = mid -1 ;
else
low = mid +1 ;
mid = (low + high ) / 2;
}
if( A[ mid ]= item )
return min ;
else
return LB -1 ;
}
Linear search vs Binary search
Linear search Binary search
1. Data can be in any order. 1. Data should be in sorted order.
2. Multi dimensional array also can 2. Only single dimensional array is
be used. used.
3. Time complexity O(n). 3. Time complexity O log (n).
4. Not an efficient method to be used 4. Efficient for large inputs also.
if there is a large item.
Visualization
,
Thank you

More Related Content

DOCX
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPT
Searching in c language
PDF
searching
PPTX
Searching and Sorting Algorithms in Data Structures
PPTX
Searching in Data Structure
PPTX
Linear and binary search
PPTX
Dsa – data structure and algorithms searching
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Searching in c language
searching
Searching and Sorting Algorithms in Data Structures
Searching in Data Structure
Linear and binary search
Dsa – data structure and algorithms searching

Similar to Presentation on Searching and Sorting in C language.pptx (20)

PDF
Searching and Sorting Techniques in Data Structure
PDF
linear search and binary search
PPTX
unit II_2_i.pptx
PPTX
Sequential & binary, linear search
PPTX
Introduction to Searching Algorithm for beginners
PPTX
data_structure_Chapter two_computer.pptx
PPTX
Rahat &amp; juhith
PDF
programming in C
PPTX
placement preparation for Array Searching.pptx
PDF
Binary search
PPTX
Searching & Algorithms IN DATA STRUCTURES
PPTX
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
DOCX
MODULE 5-Searching and-sorting
PPT
Searching
PPTX
linear-search-and-binary-search-170418023907.pptx
PPTX
Searching and Sorting Unit II Part I.pptx
PPTX
unit 2 First.pptx Searching - Linear and Binary Search
PPTX
Searching techniques in Data Structure And Algorithm
PPTX
Linear search-and-binary-search
PPTX
Linear and Binary search .pptx
Searching and Sorting Techniques in Data Structure
linear search and binary search
unit II_2_i.pptx
Sequential & binary, linear search
Introduction to Searching Algorithm for beginners
data_structure_Chapter two_computer.pptx
Rahat &amp; juhith
programming in C
placement preparation for Array Searching.pptx
Binary search
Searching & Algorithms IN DATA STRUCTURES
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
MODULE 5-Searching and-sorting
Searching
linear-search-and-binary-search-170418023907.pptx
Searching and Sorting Unit II Part I.pptx
unit 2 First.pptx Searching - Linear and Binary Search
Searching techniques in Data Structure And Algorithm
Linear search-and-binary-search
Linear and Binary search .pptx
Ad

Recently uploaded (20)

PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Lesson notes of climatology university.
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Pharma ospi slides which help in ospi learning
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
01-Introduction-to-Information-Management.pdf
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Lesson notes of climatology university.
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Pharma ospi slides which help in ospi learning
GDM (1) (1).pptx small presentation for students
Anesthesia in Laparoscopic Surgery in India
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Abdominal Access Techniques with Prof. Dr. R K Mishra
Final Presentation General Medicine 03-08-2024.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
O7-L3 Supply Chain Operations - ICLT Program
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Microbial diseases, their pathogenesis and prophylaxis
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
01-Introduction-to-Information-Management.pdf
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Ad

Presentation on Searching and Sorting in C language.pptx

  • 1. Government Polytechnic Lucknow INFORMATION TECHNOLOGY 2ND YEAR Presented by : Krishnanand Mishra, Sumit Shukla , Ankur Kumar , Utkarsh Tiwari
  • 2. Data structure using C Searching Linear search Binary search
  • 4. Searching in Array Searching in array means to find whether a particular value is present in array or not. If the desired value is present in the array, then searching is said to be successful. And the searching process gives the location of that value in the array. If the desired value is not present in the array, then searching is said to be unsuccessful. There are two popular methods for searching the array elements. Linear search Binary search
  • 5. Linear Search It is a very basic and simple search algorithm. In linear search we search an element in given array by traversing the array from the starting till the desired element is found. Linear search is mostly used to search an unordered list of elements. like : A[ ] = { 10,8,4,7,6,2,5,1,9};
  • 6. Algorithm Step 1 - Read the search element from the user. Step 2 - Compare the search element with the first element in the list. Step 3 - If both are matched, then display "given element is found!!!" And terminate the function Step 4 - If both are not matched, then compare search element with the next element in the list. Step 5 - Repeat steps 3 and 4 until search element is compared with last element in the list. Step 6 - If last element in the list also doesn't match, then display "element is not found!!!" And terminate the
  • 8. Code Int linear_search ( a[ ], LB, UB, item ) // LB= Lower bound, UB= upper bound, { int k; for (k = LB ; k <= UB; K++) { if ( a [k] == item ) { return k ; } } return LB -1; }
  • 9. Binary search Binary search is the search technique that works efficiently on sorted lists. To search an element into some list using the binary search technique, we must ensure that the list is sorted. Binary search follows the divide and conquer approach in which the list is divided into two halves, and the item is compared with the middle element of the list. If the match is found then, the location of the middle element is returned. Otherwise, we search into either of the halves depending upon the result produced through the match.
  • 10. Algorithm Step 1 - Start searching data from middle of the list. Step 2 – If it is a match, return the index of the item, and exit. Step 3 – If it is not a match, probe position. Step 4 – Divide the list and find the new middle. Step 5 – If data is greater than middle, search in higher sub-list. Step 6 – If data is smaller than middle, search in lower sub-list. Step 7 – Repeat until match
  • 12. Code Int binary_ search ( A[ ], LB, UB, item ) // LB= Lower bound, UB= Upper bound, { int low=LB, high=UB; int mid=(low + high) / 2; while( (A[mid]!= item) && ( low<=high ) ) { if( item<A[mid] ) high = mid -1 ; else low = mid +1 ; mid = (low + high ) / 2; } if( A[ mid ]= item ) return min ; else return LB -1 ; }
  • 13. Linear search vs Binary search Linear search Binary search 1. Data can be in any order. 1. Data should be in sorted order. 2. Multi dimensional array also can 2. Only single dimensional array is be used. used. 3. Time complexity O(n). 3. Time complexity O log (n). 4. Not an efficient method to be used 4. Efficient for large inputs also. if there is a large item.