SlideShare a Scribd company logo
5
Most read
6
Most read
9
Most read
Searching & Sorting
Algorithms
Content
1. Linear Search
2. Binary Search
3. Selection Sort
4. Bubble Sort
5. Quick Sort
6. Merge Sort
Linear Search
In linear search, we look at each item in the list in turn, quitting once we find an item that
matches the search term or once we’ve reached the end of the list. Our “return value” is the
index at which the search term was found, or some indicator that the search term was not
found in the list.
Algorithm for linear search
for (each item in list)
{
compare search term to current item
if match,
save index of matching item
break
}
return index of matching item, or -1 if item not found
Performance of linear search
• Best case O(1): The best case occurs when the
search term is in the first slot in the array. The number
of comparisons in this case is 1.
• Worst case O(N) : The worst case occurs when the
search term is in the last slot in the array, or is not in
the array. The number of comparisons in this case is
equal to the size of the array. If our array has N items,
then it takes N comparisons in the worst case.
• Average case O(N/2): On average, the search term
will be somewhere in the middle of the array. The
number of comparisons in this case is approximately
N/2.
Binary Search
Binary search algorithm works on sorted arrays and uses divide and conquer technique. The
binary search begins by comparing the middle element of the array with the target value. If
the target value matches the middle element, its position in the array is returned. If the target
value is less than or greater than the middle element, the search continues in the lower or
upper half of the array, respectively, eliminating the other half from consideration.
Algorithm for binary search
set first = 1, last = N, mid = N/2
while (item not found and first < last)
{
compare search term to item at mid
if match , save index
break
else if search term is less than item at mid,
set last = mid-1
else
set first = mid+1
set mid = (first+last)/2
}
return index of matching item, or -1 if item not found
Binary Search (cont.)
Performance of linear search
• Best case O(1) : The best case occurs when the search term is in the middle slot in the array. The
number of comparisons in this case is 1O(1).
• Worst or Average Case O(log N) : The worst/average case occurs when the search term is in the
other that middle slot in the array, or is not in the array. The number of comparisons in this case is
equal to the size of the array. If our array has N items, then it takes N comparisons in the worst case.
Selection Sort
The idea behind selection sort is that we put a list in order by placing each item in
turn. In other words, we put the smallest item at the start of the list, then the next
smallest item at the second position in the list, and so on until the list is in order.
Algorithm for selection sort
for i=0 to N-2
{
set smallest = i
for j=i+1 to N-1
{
compare list[smallest] to list[j]
if list[smallest] == list[j]
smallest = j
}
swap list[i] and list[smallest]
}
O(N) in swaps and O(N^2) in comparisons
Bubble Sort
Bubble sort is similar to selection sort, on each pass through the algorithm, we place at
least one item in its proper location. The differences between bubble sort and selection
sort lie in how many times data is swapped and when the algorithm terminates.
Algorithm for bubble sort
for i=N-1 to 2
{
set swap flag to false
for j=1 to i
{
if list[j-1] > list[j]
swap list[j-1] and list[j]
set swap flag to true
}
if swap flag is false,
break.
}
O(N^2) in swaps and O(N^2) in comparisons
Quick Sort
Quicksort is a divide and conquer algorithm which relies on a partition operation: to
partition an array an element called a pivot is selected. All elements smaller than the pivot
are moved before it and all greater elements are moved after it. This can be done
efficiently in linear time and in-place. The lesser and greater sub-lists are then recursively
sorted.
O(N^2) in swaps and O(N^2) in
comparisons
Algorithm Steps for Quicksort
1. Pick an arbitrary element of the array
(the pivot).
2. Divide the array into two sub arrays,
those that are smaller and those that are
greater (the partition phase).
3. Recursively sort the sub arrays.
4. Put the pivot in the middle, between the
two sorted sub arrays to obtain the final
sorted array.
Merge Sort
Merge sort divides the array into two halves, sort each of those halves and then merges
them back together. For each half, it uses the same algorithm to divide and merge smaller
halves back. The smallest halves will just have one element each which is already sorted.
O(N log N) in swaps and O(N log N) in
comparisons
Algorithm Steps for Quicksort
1. divides the list into two smaller sub-lists
of approximately equal size
2. Recursively repeat this procedure till
only one element is left in the sub-list.
3. After this, various sorted sub-lists are
merged to form sorted parent list.
4. It will continue recursively until the
original sorted list created.
Summary
1. Selection sort’s advantage is that the number of swaps is O(N), since we perform at most
one data swap per pass through the algorithm. Its disadvantage is that it does not stop
early if the list is sorted; it looks at every element in the list in turn.
2. Bubble sort’s advantage is that it stops once it determines that the list is sorted. Its
disadvantage is that it is O(N^2) in both swaps and comparisons. For this reason, bubble
sort is actually the least efficient sorting method.
3. Quicksort is the fastest sort: it is O(N log N) in both the number of comparisons and the
number of swaps. The disadvantages of quicksort are that the algorithm is a bit tricky to
understand, and if we continually select poor pivots then the algorithm can approach
O(N^2) in the worst case.
4. Merge sort is as fast as quicksort: it is O(N log N) in both swaps and comparisons. The
disadvantage of merge sort is that it requires more copying of data from temporary lists
back to the “full” list, which slows down the algorithm a bit.
Questions?

More Related Content

What's hot (20)

PDF
linear search and binary search
Zia Ush Shamszaman
 
PPTX
Insertion sort
almaqboli
 
PDF
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
PPT
Binary Search
kunj desai
 
PPTX
Insertion sort
Monalisa Patel
 
PPTX
Linear and Binary search
Nisha Soms
 
PPT
Data Structures with C Linked List
Reazul Islam
 
PDF
Quick Sort , Merge Sort , Heap Sort
Mohammed Hussein
 
PPTX
Linked list
MahammadAdil
 
PPT
Stack
srihariyenduri
 
PPTX
Searching techniques in Data Structure And Algorithm
03446940736
 
PPTX
Searching
Ashim Lamichhane
 
PDF
Array data structure
maamir farooq
 
PPTX
Sparse matrix and its representation data structure
Vardhil Patel
 
PPTX
Priority Queue in Data Structure
Meghaj Mallick
 
PPTX
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Albin562191
 
PPT
Sorting Techniques
Rafay Farooq
 
PPSX
Data structure stack&queue basics
Selvin Josy Bai Somu
 
PPTX
Searching Techniques and Analysis
AkashBorse2
 
PPTX
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
linear search and binary search
Zia Ush Shamszaman
 
Insertion sort
almaqboli
 
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Binary Search
kunj desai
 
Insertion sort
Monalisa Patel
 
Linear and Binary search
Nisha Soms
 
Data Structures with C Linked List
Reazul Islam
 
Quick Sort , Merge Sort , Heap Sort
Mohammed Hussein
 
Linked list
MahammadAdil
 
Searching techniques in Data Structure And Algorithm
03446940736
 
Searching
Ashim Lamichhane
 
Array data structure
maamir farooq
 
Sparse matrix and its representation data structure
Vardhil Patel
 
Priority Queue in Data Structure
Meghaj Mallick
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Albin562191
 
Sorting Techniques
Rafay Farooq
 
Data structure stack&queue basics
Selvin Josy Bai Somu
 
Searching Techniques and Analysis
AkashBorse2
 
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 

Similar to Searching & Sorting Algorithms (20)

PPTX
Chapter 2. data structure and algorithm
SolomonEndalu
 
PPT
Ch05 Black Jack
leminhvuong
 
PPTX
All Searching and Sorting Techniques in Data Structures
sonalishinge2015
 
PPTX
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
PPSX
Data Structure and Algorithm Chapter 2.ppsx Chapter 2.ppsx
SolomonEndalu
 
PDF
Unit 6 dsa SEARCHING AND SORTING
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PPTX
Sorting Algorithms to arrange data in particular format
itsusamazahid
 
PPTX
Sorting.pptx
LuciaMakwasha1
 
PPTX
sorting and searching.pptx
ParagAhir1
 
PPTX
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PradipTadme
 
PPT
(Data Structure) Chapter11 searching & sorting
Fadhil Ismail
 
PPTX
searching in data structure.pptx
chouguleamruta24
 
PPTX
DFC30233_CHAPTER 6 (1).pptxxxxxxxxxxxxxxxxxxxxxxxx
rajinevitable05
 
PPTX
Searching,sorting
LavanyaJ28
 
PPTX
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
PPTX
Data Structures_ Sorting & Searching
ThenmozhiK5
 
PPTX
LLL3 Searching & Sorting algorithms.pptx
plabonrahmanme
 
PPTX
Searching_Sorting.pptx
21BD1A058RSahithi
 
PDF
Sorting algorithms bubble sort to merge sort.pdf
AyeshaMazhar21
 
PPTX
Searching and Sorting Algorithms in Data Structures
poongothai11
 
Chapter 2. data structure and algorithm
SolomonEndalu
 
Ch05 Black Jack
leminhvuong
 
All Searching and Sorting Techniques in Data Structures
sonalishinge2015
 
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
Data Structure and Algorithm Chapter 2.ppsx Chapter 2.ppsx
SolomonEndalu
 
Sorting Algorithms to arrange data in particular format
itsusamazahid
 
Sorting.pptx
LuciaMakwasha1
 
sorting and searching.pptx
ParagAhir1
 
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PradipTadme
 
(Data Structure) Chapter11 searching & sorting
Fadhil Ismail
 
searching in data structure.pptx
chouguleamruta24
 
DFC30233_CHAPTER 6 (1).pptxxxxxxxxxxxxxxxxxxxxxxxx
rajinevitable05
 
Searching,sorting
LavanyaJ28
 
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Data Structures_ Sorting & Searching
ThenmozhiK5
 
LLL3 Searching & Sorting algorithms.pptx
plabonrahmanme
 
Searching_Sorting.pptx
21BD1A058RSahithi
 
Sorting algorithms bubble sort to merge sort.pdf
AyeshaMazhar21
 
Searching and Sorting Algorithms in Data Structures
poongothai11
 
Ad

More from Rahul Jamwal (6)

PPTX
Process & Mutlithreading
Rahul Jamwal
 
PPTX
Data Structures
Rahul Jamwal
 
PPTX
C++ Memory Management
Rahul Jamwal
 
PPTX
Virtual Memory Management
Rahul Jamwal
 
PPTX
C++ shared libraries and loading
Rahul Jamwal
 
PPTX
C++ compilation process
Rahul Jamwal
 
Process & Mutlithreading
Rahul Jamwal
 
Data Structures
Rahul Jamwal
 
C++ Memory Management
Rahul Jamwal
 
Virtual Memory Management
Rahul Jamwal
 
C++ shared libraries and loading
Rahul Jamwal
 
C++ compilation process
Rahul Jamwal
 
Ad

Recently uploaded (20)

PPTX
Automatic_Iperf_Log_Result_Excel_visual_v2.pptx
Chen-Chih Lee
 
PDF
IObit Uninstaller Pro 14.3.1.8 Crack for Windows Latest
utfefguu
 
PPTX
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
 
PDF
Laboratory Workflows Digitalized and live in 90 days with Scifeon´s SAPPA P...
info969686
 
PDF
Continouous failure - Why do we make our lives hard?
Papp Krisztián
 
PPTX
For my supp to finally picking supp that work
necas19388
 
PDF
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
 
PPTX
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
 
PDF
Automated Test Case Repair Using Language Models
Lionel Briand
 
PDF
Dealing with JSON in the relational world
Andres Almiray
 
PDF
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
 
PPTX
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
 
PPTX
EO4EU Ocean Monitoring: Maritime Weather Routing Optimsation Use Case
EO4EU
 
PDF
WholeClear Split vCard Software for Split large vCard file
markwillsonmw004
 
PDF
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
 
PDF
How DeepSeek Beats ChatGPT: Cost Comparison and Key Differences
sumitpurohit810
 
PDF
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
 
PDF
Code Once; Run Everywhere - A Beginner’s Journey with React Native
Hasitha Walpola
 
PDF
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
 
Automatic_Iperf_Log_Result_Excel_visual_v2.pptx
Chen-Chih Lee
 
IObit Uninstaller Pro 14.3.1.8 Crack for Windows Latest
utfefguu
 
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
 
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
 
Laboratory Workflows Digitalized and live in 90 days with Scifeon´s SAPPA P...
info969686
 
Continouous failure - Why do we make our lives hard?
Papp Krisztián
 
For my supp to finally picking supp that work
necas19388
 
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
 
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
 
Automated Test Case Repair Using Language Models
Lionel Briand
 
Dealing with JSON in the relational world
Andres Almiray
 
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
 
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
 
EO4EU Ocean Monitoring: Maritime Weather Routing Optimsation Use Case
EO4EU
 
WholeClear Split vCard Software for Split large vCard file
markwillsonmw004
 
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
 
How DeepSeek Beats ChatGPT: Cost Comparison and Key Differences
sumitpurohit810
 
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
 
Code Once; Run Everywhere - A Beginner’s Journey with React Native
Hasitha Walpola
 
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
 

Searching & Sorting Algorithms

  • 2. Content 1. Linear Search 2. Binary Search 3. Selection Sort 4. Bubble Sort 5. Quick Sort 6. Merge Sort
  • 3. Linear Search In linear search, we look at each item in the list in turn, quitting once we find an item that matches the search term or once we’ve reached the end of the list. Our “return value” is the index at which the search term was found, or some indicator that the search term was not found in the list. Algorithm for linear search for (each item in list) { compare search term to current item if match, save index of matching item break } return index of matching item, or -1 if item not found Performance of linear search • Best case O(1): The best case occurs when the search term is in the first slot in the array. The number of comparisons in this case is 1. • Worst case O(N) : The worst case occurs when the search term is in the last slot in the array, or is not in the array. The number of comparisons in this case is equal to the size of the array. If our array has N items, then it takes N comparisons in the worst case. • Average case O(N/2): On average, the search term will be somewhere in the middle of the array. The number of comparisons in this case is approximately N/2.
  • 4. Binary Search Binary search algorithm works on sorted arrays and uses divide and conquer technique. The binary search begins by comparing the middle element of the array with the target value. If the target value matches the middle element, its position in the array is returned. If the target value is less than or greater than the middle element, the search continues in the lower or upper half of the array, respectively, eliminating the other half from consideration. Algorithm for binary search set first = 1, last = N, mid = N/2 while (item not found and first < last) { compare search term to item at mid if match , save index break else if search term is less than item at mid, set last = mid-1 else set first = mid+1 set mid = (first+last)/2 } return index of matching item, or -1 if item not found
  • 5. Binary Search (cont.) Performance of linear search • Best case O(1) : The best case occurs when the search term is in the middle slot in the array. The number of comparisons in this case is 1O(1). • Worst or Average Case O(log N) : The worst/average case occurs when the search term is in the other that middle slot in the array, or is not in the array. The number of comparisons in this case is equal to the size of the array. If our array has N items, then it takes N comparisons in the worst case.
  • 6. Selection Sort The idea behind selection sort is that we put a list in order by placing each item in turn. In other words, we put the smallest item at the start of the list, then the next smallest item at the second position in the list, and so on until the list is in order. Algorithm for selection sort for i=0 to N-2 { set smallest = i for j=i+1 to N-1 { compare list[smallest] to list[j] if list[smallest] == list[j] smallest = j } swap list[i] and list[smallest] } O(N) in swaps and O(N^2) in comparisons
  • 7. Bubble Sort Bubble sort is similar to selection sort, on each pass through the algorithm, we place at least one item in its proper location. The differences between bubble sort and selection sort lie in how many times data is swapped and when the algorithm terminates. Algorithm for bubble sort for i=N-1 to 2 { set swap flag to false for j=1 to i { if list[j-1] > list[j] swap list[j-1] and list[j] set swap flag to true } if swap flag is false, break. } O(N^2) in swaps and O(N^2) in comparisons
  • 8. Quick Sort Quicksort is a divide and conquer algorithm which relies on a partition operation: to partition an array an element called a pivot is selected. All elements smaller than the pivot are moved before it and all greater elements are moved after it. This can be done efficiently in linear time and in-place. The lesser and greater sub-lists are then recursively sorted. O(N^2) in swaps and O(N^2) in comparisons Algorithm Steps for Quicksort 1. Pick an arbitrary element of the array (the pivot). 2. Divide the array into two sub arrays, those that are smaller and those that are greater (the partition phase). 3. Recursively sort the sub arrays. 4. Put the pivot in the middle, between the two sorted sub arrays to obtain the final sorted array.
  • 9. Merge Sort Merge sort divides the array into two halves, sort each of those halves and then merges them back together. For each half, it uses the same algorithm to divide and merge smaller halves back. The smallest halves will just have one element each which is already sorted. O(N log N) in swaps and O(N log N) in comparisons Algorithm Steps for Quicksort 1. divides the list into two smaller sub-lists of approximately equal size 2. Recursively repeat this procedure till only one element is left in the sub-list. 3. After this, various sorted sub-lists are merged to form sorted parent list. 4. It will continue recursively until the original sorted list created.
  • 10. Summary 1. Selection sort’s advantage is that the number of swaps is O(N), since we perform at most one data swap per pass through the algorithm. Its disadvantage is that it does not stop early if the list is sorted; it looks at every element in the list in turn. 2. Bubble sort’s advantage is that it stops once it determines that the list is sorted. Its disadvantage is that it is O(N^2) in both swaps and comparisons. For this reason, bubble sort is actually the least efficient sorting method. 3. Quicksort is the fastest sort: it is O(N log N) in both the number of comparisons and the number of swaps. The disadvantages of quicksort are that the algorithm is a bit tricky to understand, and if we continually select poor pivots then the algorithm can approach O(N^2) in the worst case. 4. Merge sort is as fast as quicksort: it is O(N log N) in both swaps and comparisons. The disadvantage of merge sort is that it requires more copying of data from temporary lists back to the “full” list, which slows down the algorithm a bit.