SlideShare a Scribd company logo
Simple Sorting and Searching Algorithms
Chapter Two
Outline
โ€ข Searching Algorithms
๏ƒผ Linear Search (Sequential Search)
๏ƒผBinary Search
โ€ข Sorting Algorithms
๏ƒผ Bubble Sort
๏ƒผ Insertion Sort
๏ƒผ Sรฉlection Sort
Searching and sorting
โ€ข Searching and sorting are fundamental operations in computer science,
used to efficiently manage and retrieve data. Searching helps locate
specific elements in a dataset, while sorting arranges data in a structured
order, improving search efficiency and data organization.
Searching
Searching
โ€ข Searching is the process of finding a specific element in an array.
โ€ข Searching is essential when working with large data in arrays, such as
looking up a phone number or accessing a website.
Types of Searching Techniques:
1.Linear Search โ€“ Checks each element one by one.
2.Binary Search โ€“ Efficient for sorted arrays, divides data
in half.
What is Linear Search?
Linear search is a method to find an element in a list by checking each element
one by one.
โ€ขHow it works:
โ€ขStart from the first element.
โ€ขCompare each element with the search key.
โ€ขStop when the key is found or the list ends.
โ€ขKey Points:
โ€ขWorks on unsorted and sorted arrays.
โ€ขSuitable for small datasets.
Steps of Linear Search
1. Start from the first element (A[0]).
2. Compare each element with the search key.
3. If a match is found:
โ€ข Return the index.
1. If no match is found after checking all elements:
โ€ข Conclude that the key is not in the list.
Linear Search
Example of Linear Search
Array: [10, 20, 30, 40, 50]
Search Key: 30
Steps:
1.Compare 10 with 30. โ†’ Not a match.
2.Compare 20 with 30. โ†’ Not a match.
3.Compare 30 with 30. โ†’ Match found at index 2.
Algorithm (Pseudocode)
Input: Array A of size n, Search Key "item"
Output: Index of "item" or -1 if not found
1. Set loc = -1
2. For i = 0 to n-1:
- If A[i] == item:
Set loc = i
Exit loop
3 If loc >= 0:
Display "Item found at index loc"
4. Else:
Display "Item not found"
Linear search
Key Properties
โ€ขTime Complexity:
โ€ขBest case: O(1) (item is found at the first position).
โ€ขWorst case: O(n) (item is at the last position or not present).
โ€ขSpace Complexity:
โ€ขLinear search uses O(1) extra space (no additional memory
needed).
Advantages and Disadvantages
Advantages
1.Simple and easy to implement.
2.Works with unsorted arrays.
3.Useful for small datasets.
Disadvantages
1.Inefficient for large datasets.
2.Requires checking every element in the worst case.
3.Slower than algorithms like Binary Search for sorted arrays.
Binary Search
โ€ข Binary Search: Efficient Searching
โ€ข Finding Elements Quickly in Sorted Lists
What is Binary Search?
โ€ขA highly efficient algorithm for finding a specific element in a sorted list.
โ€ขWorks by repeatedly dividing the search interval in half.
โ€ขSignificantly faster than linear search for large lists.
Cont..
โ€ข Binary Search - How it Works?
1. Find the Middle: Determine the middle element of the sorted list.
2. Compare: Compare the middle element with the target element.
3. Narrow the Search:
โ€ข If the middle element is the target, you're done!
โ€ข If the target is less than the middle, search the left half.
โ€ข If the target is greater than the middle, search the right half.
4. Repeat: Continue dividing and comparing until the target is found or the
search space is empty.
Example
int Binary_Search(int list[], int n, int key) {
int left = 0, right = n - 1;
int mid, index = -1;
while (left <= right) { // Fixed loop condition
mid = (left + right) / 2;
if (list[mid] == key)
return mid; // Return index immediately if found
else if (key < list[mid])
right = mid - 1; // Search left half
else
left = mid + 1; // Search right half
}
return -1; // Not found
}
Cont..
โ€ข Binary Search - Step-by-Step Example
Searching for 23 in [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]:
Cont..
โ€ข Binary Search - Time Complexity
โ€ขTime complexity: O(log n)
โ€ขThis means the search time grows logarithmically with the size of the list.
โ€ขMuch faster than linear search (O(n)) for large lists.
Binary Search vs. Linear Search
โ€ขBinary Search: Fast, requires a sorted list.
โ€ขLinear Search: Slow for large lists, works on unsorted lists.
โ€ขFor 1000 items, binary search takes around 10 comparisons, linear
search an average of 500.
Sorting
What is Sorting?
โ€ข Sorting is the process of arranging items in a specific order
(ascending or descending).
โ€ข It's a fundamental operation in computer science.
โ€ข Today, we'll cover three basic sorting algorithms: Insertion
Sort, Selection Sort, and Bubble Sort.
Insertion Sort
โ€ข Insertion Sort: Sorting Like Playing Cards
โ€ขImagine sorting cards in your hand.
โ€ขYou pick a card and insert it into its correct position within
the already sorted cards.
โ€ขInsertion sort does the same thing with a list of numbers.
Cont..
Insertion Sort - Step-by-Step Example
Insertion Sort Example: [5, 2, 4, 1, 3]
โ€ขStart: [5, 2, 4, 1, 3] (5 is considered sorted)
โ€ขInsert 2: [2, 5, 4, 1, 3] (2 is moved before 5)
โ€ขInsert 4: [2, 4, 5, 1, 3] (4 is inserted between 2 and 5)
โ€ขInsert 1: [1, 2, 4, 5, 3] (1 is moved to the beginning)
โ€ขInsert 3: [1, 2, 3, 4, 5] (3 is inserted between 2 and 4)
Execution example
Implementation
for (int i = 1; i < n; i++) {
int key = list[i];
int j = i - 1;
while (j >= 0 && list[j] > key) {
list[j + 1] = list[j];
j--;
}
list[j + 1] = key;
}
Selection Sort
โ€ข Selection Sort: Finding the Smallest
โ€ขRepeatedly find the smallest element from the unsorted part.
โ€ขPlace it at the beginning of the sorted part.
Cont..
โ€ข Selection Sort - Step-by-Step Example
โ€ขFind 1: [1, 4, 6, 5, 3] (1 is swapped with 6)
โ€ขFind 3: [1, 3, 6, 5, 4] (3 is swapped with 4)
โ€ขFind 4: [1, 3, 4, 5, 6] (4 is swapped with 6)
โ€ขFind 5: [1, 3, 4, 5, 6] (5 is already in place)
Selection Sort Example: [6, 4, 1, 5, 3]
Example Execution
Selection Sort - Implementation
void selection_sort(int list[]) {
int i, j, smallest, temp;
for (i = 0; i < n; i++) {
smallest = i;
for (j = i + 1; j < n; j++) {
if (list[j] < list[smallest])
smallest = j;
} // End of inner loop
temp = list[smallest];
list[smallest] = list[i];
list[i] = temp;
} // End of outer loop
} // End of selection_sort
Bubble Sort
โ€ข Bubble Sort: Comparing and Swapping
โ€ขRepeatedly step through the list.
โ€ขCompare adjacent elements and swap them if they are in the wrong order.
โ€ขLarger elements "bubble" to the end.
Example Execution
Cont..
โ€ข Bubble Sort - Step-by-Step Example
Bubble Sort Example: [5, 1, 4, 2, 8]
โ€ขPass 1: [1, 4, 2, 5, 8]
โ€ขPass 2: [1, 2, 4, 5, 8]
โ€ขPass 3: [1, 2, 4, 5, 8]
Implementation
void bubble_sort(int list[]) {
int i, j, temp;
for (i = 0; i < n; i++) {
for (j = n - 1; j > i; j--) {
if (list[j] < list[j - 1]) {
temp = list[j];
list[j] = list[j - 1];
list[j - 1] = temp;
} // Swap adjacent elements
} // End of inner loop
} // End of outer loop
} // End of bubble_sort
Comparing Sorting Algorithms
โ€ขInsertion Sort: Good for small lists, efficient for nearly sorted lists.
โ€ขSelection Sort: Simple, consistent, but not very efficient.
โ€ขBubble Sort: Very simple, but highly inefficient.
Thanks!

More Related Content

Similar to Chapter 2. data structure and algorithm (20)

data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
Mohammed472103
ย 
Module_1 Linear search search and Bsearch).pptx
Module_1 Linear search search and Bsearch).pptx
vidhyapm2
ย 
searching in data structure.pptx
searching in data structure.pptx
chouguleamruta24
ย 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
ย 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
ย 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdf
MustafaJutt4
ย 
dsa pdf.pdf
dsa pdf.pdf
DewashishDwivedi
ย 
Lecture_Oct26.pptx
Lecture_Oct26.pptx
SylrizcinMarieManzo3
ย 
Rahat &amp; juhith
Rahat &amp; juhith
Rj Juhith
ย 
searching techniques.pptx
searching techniques.pptx
Dr.Shweta
ย 
Data structure Unit - II Searching and Sorting.pptx
Data structure Unit - II Searching and Sorting.pptx
gavanisanjana
ย 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
nikshaikh786
ย 
All Searching and Sorting Techniques in Data Structures
All Searching and Sorting Techniques in Data Structures
sonalishinge2015
ย 
3-Searching and Sortingsdsdsdssssssssss.pdf
3-Searching and Sortingsdsdsdssssssssss.pdf
NGUYNTHNHQUC2
ย 
Algorithm and Programming (Searching)
Algorithm and Programming (Searching)
Adam Mukharil Bachtiar
ย 
Unit 8 searching and hashing
Unit 8 searching and hashing
Dabbal Singh Mahara
ย 
Sorting and hashing concepts
Sorting and hashing concepts
LJ Projects
ย 
Sorting and hashing concepts
Sorting and hashing concepts
LJ Projects
ย 
1.4 Sorting.pptx
1.4 Sorting.pptx
Sujan527908
ย 
sorting and searching.pptx
sorting and searching.pptx
ParagAhir1
ย 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
Mohammed472103
ย 
Module_1 Linear search search and Bsearch).pptx
Module_1 Linear search search and Bsearch).pptx
vidhyapm2
ย 
searching in data structure.pptx
searching in data structure.pptx
chouguleamruta24
ย 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
ย 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdf
MustafaJutt4
ย 
Rahat &amp; juhith
Rahat &amp; juhith
Rj Juhith
ย 
searching techniques.pptx
searching techniques.pptx
Dr.Shweta
ย 
Data structure Unit - II Searching and Sorting.pptx
Data structure Unit - II Searching and Sorting.pptx
gavanisanjana
ย 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
nikshaikh786
ย 
All Searching and Sorting Techniques in Data Structures
All Searching and Sorting Techniques in Data Structures
sonalishinge2015
ย 
3-Searching and Sortingsdsdsdssssssssss.pdf
3-Searching and Sortingsdsdsdssssssssss.pdf
NGUYNTHNHQUC2
ย 
Algorithm and Programming (Searching)
Algorithm and Programming (Searching)
Adam Mukharil Bachtiar
ย 
Unit 8 searching and hashing
Unit 8 searching and hashing
Dabbal Singh Mahara
ย 
Sorting and hashing concepts
Sorting and hashing concepts
LJ Projects
ย 
Sorting and hashing concepts
Sorting and hashing concepts
LJ Projects
ย 
1.4 Sorting.pptx
1.4 Sorting.pptx
Sujan527908
ย 
sorting and searching.pptx
sorting and searching.pptx
ParagAhir1
ย 

More from SolomonEndalu (12)

Research methodology for computer scienceTechnical Report MAS.ppt
Research methodology for computer scienceTechnical Report MAS.ppt
SolomonEndalu
ย 
manual Networking LAB SESSION 1 PPT.pptx
manual Networking LAB SESSION 1 PPT.pptx
SolomonEndalu
ย 
LAB SESSION 3 PPT.pptxNetworking lab manual LAB SESSION 1 PPT.pptx
LAB SESSION 3 PPT.pptxNetworking lab manual LAB SESSION 1 PPT.pptx
SolomonEndalu
ย 
Networking lab manual LAB SESSION 1 PPT.pptx
Networking lab manual LAB SESSION 1 PPT.pptx
SolomonEndalu
ย 
Data Structure and Algorithm Chapter 3.ppsxDSA Chapter 3.ppsx
Data Structure and Algorithm Chapter 3.ppsxDSA Chapter 3.ppsx
SolomonEndalu
ย 
Data Structure and Algorithm Chapter 1.ppsx
Data Structure and Algorithm Chapter 1.ppsx
SolomonEndalu
ย 
chapter_7 _Other Emerging Technologies-new.pptx
chapter_7 _Other Emerging Technologies-new.pptx
SolomonEndalu
ย 
Chapter 5 ARIntroduction to Emerging Technologies
Chapter 5 ARIntroduction to Emerging Technologies
SolomonEndalu
ย 
Emerging Technology Chapter 4 internets of things
Emerging Technology Chapter 4 internets of things
SolomonEndalu
ย 
chapter_1_Introduction to Emerging Technologies
chapter_1_Introduction to Emerging Technologies
SolomonEndalu
ย 
Emerging Technology Chapter 3 Artificial Intelligence
Emerging Technology Chapter 3 Artificial Intelligence
SolomonEndalu
ย 
Emerging Technology Chapter 2 Data Science
Emerging Technology Chapter 2 Data Science
SolomonEndalu
ย 
Research methodology for computer scienceTechnical Report MAS.ppt
Research methodology for computer scienceTechnical Report MAS.ppt
SolomonEndalu
ย 
manual Networking LAB SESSION 1 PPT.pptx
manual Networking LAB SESSION 1 PPT.pptx
SolomonEndalu
ย 
LAB SESSION 3 PPT.pptxNetworking lab manual LAB SESSION 1 PPT.pptx
LAB SESSION 3 PPT.pptxNetworking lab manual LAB SESSION 1 PPT.pptx
SolomonEndalu
ย 
Networking lab manual LAB SESSION 1 PPT.pptx
Networking lab manual LAB SESSION 1 PPT.pptx
SolomonEndalu
ย 
Data Structure and Algorithm Chapter 3.ppsxDSA Chapter 3.ppsx
Data Structure and Algorithm Chapter 3.ppsxDSA Chapter 3.ppsx
SolomonEndalu
ย 
Data Structure and Algorithm Chapter 1.ppsx
Data Structure and Algorithm Chapter 1.ppsx
SolomonEndalu
ย 
chapter_7 _Other Emerging Technologies-new.pptx
chapter_7 _Other Emerging Technologies-new.pptx
SolomonEndalu
ย 
Chapter 5 ARIntroduction to Emerging Technologies
Chapter 5 ARIntroduction to Emerging Technologies
SolomonEndalu
ย 
Emerging Technology Chapter 4 internets of things
Emerging Technology Chapter 4 internets of things
SolomonEndalu
ย 
chapter_1_Introduction to Emerging Technologies
chapter_1_Introduction to Emerging Technologies
SolomonEndalu
ย 
Emerging Technology Chapter 3 Artificial Intelligence
Emerging Technology Chapter 3 Artificial Intelligence
SolomonEndalu
ย 
Emerging Technology Chapter 2 Data Science
Emerging Technology Chapter 2 Data Science
SolomonEndalu
ย 
Ad

Recently uploaded (20)

Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
ย 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
ย 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
ย 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
ย 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
ย 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
ย 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
ย 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
ย 
Kubernetes Security Act Now Before Itโ€™s Too Late
Kubernetes Security Act Now Before Itโ€™s Too Late
Michael Furman
ย 
โ€œAddressing Evolving AI Model Challenges Through Memory and Storage,โ€ a Prese...
โ€œAddressing Evolving AI Model Challenges Through Memory and Storage,โ€ a Prese...
Edge AI and Vision Alliance
ย 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
ย 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
ย 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
ย 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
ย 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
ย 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
ย 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
ย 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
ย 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
ย 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
ย 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
ย 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
ย 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
ย 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
ย 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
ย 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
ย 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
ย 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
ย 
Kubernetes Security Act Now Before Itโ€™s Too Late
Kubernetes Security Act Now Before Itโ€™s Too Late
Michael Furman
ย 
โ€œAddressing Evolving AI Model Challenges Through Memory and Storage,โ€ a Prese...
โ€œAddressing Evolving AI Model Challenges Through Memory and Storage,โ€ a Prese...
Edge AI and Vision Alliance
ย 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
ย 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
ย 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
ย 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
ย 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
ย 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
ย 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
ย 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
ย 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
ย 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
ย 
Ad

Chapter 2. data structure and algorithm

  • 1. Simple Sorting and Searching Algorithms Chapter Two
  • 2. Outline โ€ข Searching Algorithms ๏ƒผ Linear Search (Sequential Search) ๏ƒผBinary Search โ€ข Sorting Algorithms ๏ƒผ Bubble Sort ๏ƒผ Insertion Sort ๏ƒผ Sรฉlection Sort
  • 3. Searching and sorting โ€ข Searching and sorting are fundamental operations in computer science, used to efficiently manage and retrieve data. Searching helps locate specific elements in a dataset, while sorting arranges data in a structured order, improving search efficiency and data organization.
  • 5. Searching โ€ข Searching is the process of finding a specific element in an array. โ€ข Searching is essential when working with large data in arrays, such as looking up a phone number or accessing a website. Types of Searching Techniques: 1.Linear Search โ€“ Checks each element one by one. 2.Binary Search โ€“ Efficient for sorted arrays, divides data in half.
  • 6. What is Linear Search? Linear search is a method to find an element in a list by checking each element one by one. โ€ขHow it works: โ€ขStart from the first element. โ€ขCompare each element with the search key. โ€ขStop when the key is found or the list ends. โ€ขKey Points: โ€ขWorks on unsorted and sorted arrays. โ€ขSuitable for small datasets.
  • 7. Steps of Linear Search 1. Start from the first element (A[0]). 2. Compare each element with the search key. 3. If a match is found: โ€ข Return the index. 1. If no match is found after checking all elements: โ€ข Conclude that the key is not in the list.
  • 9. Example of Linear Search Array: [10, 20, 30, 40, 50] Search Key: 30 Steps: 1.Compare 10 with 30. โ†’ Not a match. 2.Compare 20 with 30. โ†’ Not a match. 3.Compare 30 with 30. โ†’ Match found at index 2.
  • 10. Algorithm (Pseudocode) Input: Array A of size n, Search Key "item" Output: Index of "item" or -1 if not found 1. Set loc = -1 2. For i = 0 to n-1: - If A[i] == item: Set loc = i Exit loop 3 If loc >= 0: Display "Item found at index loc" 4. Else: Display "Item not found"
  • 12. Key Properties โ€ขTime Complexity: โ€ขBest case: O(1) (item is found at the first position). โ€ขWorst case: O(n) (item is at the last position or not present). โ€ขSpace Complexity: โ€ขLinear search uses O(1) extra space (no additional memory needed).
  • 13. Advantages and Disadvantages Advantages 1.Simple and easy to implement. 2.Works with unsorted arrays. 3.Useful for small datasets. Disadvantages 1.Inefficient for large datasets. 2.Requires checking every element in the worst case. 3.Slower than algorithms like Binary Search for sorted arrays.
  • 14. Binary Search โ€ข Binary Search: Efficient Searching โ€ข Finding Elements Quickly in Sorted Lists What is Binary Search? โ€ขA highly efficient algorithm for finding a specific element in a sorted list. โ€ขWorks by repeatedly dividing the search interval in half. โ€ขSignificantly faster than linear search for large lists.
  • 15. Cont.. โ€ข Binary Search - How it Works? 1. Find the Middle: Determine the middle element of the sorted list. 2. Compare: Compare the middle element with the target element. 3. Narrow the Search: โ€ข If the middle element is the target, you're done! โ€ข If the target is less than the middle, search the left half. โ€ข If the target is greater than the middle, search the right half. 4. Repeat: Continue dividing and comparing until the target is found or the search space is empty.
  • 16. Example int Binary_Search(int list[], int n, int key) { int left = 0, right = n - 1; int mid, index = -1; while (left <= right) { // Fixed loop condition mid = (left + right) / 2; if (list[mid] == key) return mid; // Return index immediately if found else if (key < list[mid]) right = mid - 1; // Search left half else left = mid + 1; // Search right half } return -1; // Not found }
  • 17. Cont.. โ€ข Binary Search - Step-by-Step Example Searching for 23 in [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]:
  • 18. Cont.. โ€ข Binary Search - Time Complexity โ€ขTime complexity: O(log n) โ€ขThis means the search time grows logarithmically with the size of the list. โ€ขMuch faster than linear search (O(n)) for large lists.
  • 19. Binary Search vs. Linear Search โ€ขBinary Search: Fast, requires a sorted list. โ€ขLinear Search: Slow for large lists, works on unsorted lists. โ€ขFor 1000 items, binary search takes around 10 comparisons, linear search an average of 500.
  • 21. What is Sorting? โ€ข Sorting is the process of arranging items in a specific order (ascending or descending). โ€ข It's a fundamental operation in computer science. โ€ข Today, we'll cover three basic sorting algorithms: Insertion Sort, Selection Sort, and Bubble Sort.
  • 22. Insertion Sort โ€ข Insertion Sort: Sorting Like Playing Cards โ€ขImagine sorting cards in your hand. โ€ขYou pick a card and insert it into its correct position within the already sorted cards. โ€ขInsertion sort does the same thing with a list of numbers.
  • 23. Cont.. Insertion Sort - Step-by-Step Example Insertion Sort Example: [5, 2, 4, 1, 3] โ€ขStart: [5, 2, 4, 1, 3] (5 is considered sorted) โ€ขInsert 2: [2, 5, 4, 1, 3] (2 is moved before 5) โ€ขInsert 4: [2, 4, 5, 1, 3] (4 is inserted between 2 and 5) โ€ขInsert 1: [1, 2, 4, 5, 3] (1 is moved to the beginning) โ€ขInsert 3: [1, 2, 3, 4, 5] (3 is inserted between 2 and 4)
  • 25. Implementation for (int i = 1; i < n; i++) { int key = list[i]; int j = i - 1; while (j >= 0 && list[j] > key) { list[j + 1] = list[j]; j--; } list[j + 1] = key; }
  • 26. Selection Sort โ€ข Selection Sort: Finding the Smallest โ€ขRepeatedly find the smallest element from the unsorted part. โ€ขPlace it at the beginning of the sorted part.
  • 27. Cont.. โ€ข Selection Sort - Step-by-Step Example โ€ขFind 1: [1, 4, 6, 5, 3] (1 is swapped with 6) โ€ขFind 3: [1, 3, 6, 5, 4] (3 is swapped with 4) โ€ขFind 4: [1, 3, 4, 5, 6] (4 is swapped with 6) โ€ขFind 5: [1, 3, 4, 5, 6] (5 is already in place) Selection Sort Example: [6, 4, 1, 5, 3]
  • 29. Selection Sort - Implementation void selection_sort(int list[]) { int i, j, smallest, temp; for (i = 0; i < n; i++) { smallest = i; for (j = i + 1; j < n; j++) { if (list[j] < list[smallest]) smallest = j; } // End of inner loop temp = list[smallest]; list[smallest] = list[i]; list[i] = temp; } // End of outer loop } // End of selection_sort
  • 30. Bubble Sort โ€ข Bubble Sort: Comparing and Swapping โ€ขRepeatedly step through the list. โ€ขCompare adjacent elements and swap them if they are in the wrong order. โ€ขLarger elements "bubble" to the end.
  • 32. Cont.. โ€ข Bubble Sort - Step-by-Step Example Bubble Sort Example: [5, 1, 4, 2, 8] โ€ขPass 1: [1, 4, 2, 5, 8] โ€ขPass 2: [1, 2, 4, 5, 8] โ€ขPass 3: [1, 2, 4, 5, 8]
  • 33. Implementation void bubble_sort(int list[]) { int i, j, temp; for (i = 0; i < n; i++) { for (j = n - 1; j > i; j--) { if (list[j] < list[j - 1]) { temp = list[j]; list[j] = list[j - 1]; list[j - 1] = temp; } // Swap adjacent elements } // End of inner loop } // End of outer loop } // End of bubble_sort
  • 34. Comparing Sorting Algorithms โ€ขInsertion Sort: Good for small lists, efficient for nearly sorted lists. โ€ขSelection Sort: Simple, consistent, but not very efficient. โ€ขBubble Sort: Very simple, but highly inefficient.