SlideShare a Scribd company logo
By Mr Solomon E.
Simple Sorting and Searching Algorithms
Chapter Two
By Mr Solomon E.
Outline
• Searching Algorithms
 Linear Search (Sequential Search)
Binary Search
• Sorting Algorithms
 Bubble Sort
 Insertion Sort
 Sélection Sort
By Mr Solomon E.
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.
By
Mr
Solomon
E.
Searching
By Mr Solomon E.
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.
By Mr Solomon E.
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.
By Mr Solomon E.
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.
By Mr Solomon E.
Linear Search
By Mr Solomon E.
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.
By Mr Solomon E.
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"
By Mr Solomon E.
Linear search
By Mr Solomon E.
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).
By Mr Solomon E.
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.
By Mr Solomon E.
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.
By Mr Solomon E.
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.
By Mr Solomon E.
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
}
By Mr Solomon E.
Cont..
• Binary Search - Step-by-Step Example
Searching for 23 in [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]:
By Mr Solomon E.
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.
By Mr Solomon E.
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.
By
Mr
Solomon
E.
Sorting
By Mr Solomon E.
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.
By Mr Solomon E.
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.
By Mr Solomon E.
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)
By Mr Solomon E.
Execution example
By Mr Solomon E.
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;
}
By Mr Solomon E.
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.
By Mr Solomon E.
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]
By Mr Solomon E.
Example Execution
By Mr Solomon E.
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
By Mr Solomon E.
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.
By Mr Solomon E.
Example Execution
By Mr Solomon E.
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]
By Mr Solomon E.
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
By Mr Solomon E.
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.
By Mr Solomon E.
Thanks!

More Related Content

PPTX
Chapter 2. data structure and algorithm
PPTX
Searching Algorithms - Foundations of Algorithms
PPTX
Chapter 2 Sorting and Searching .pptx.soft
PPTX
Searching techniques
PPTX
sorting-160810203705.pptx
PPT
Sorting
PPT
search_sort.ppt
PPTX
searching in data structure.pptx
Chapter 2. data structure and algorithm
Searching Algorithms - Foundations of Algorithms
Chapter 2 Sorting and Searching .pptx.soft
Searching techniques
sorting-160810203705.pptx
Sorting
search_sort.ppt
searching in data structure.pptx

Similar to Data Structure and Algorithm Chapter 2.ppsx Chapter 2.ppsx (20)

PPTX
Data Structures Unit 2 FINAL presentation.pptx
PPTX
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
PPT
Lecture_4 (Sorting Algorithms) before mids - Copy.ppt
PDF
Ocw chp6 2searchbinary
PPTX
Chapter #3 (Searchinmmmg ALgorithm).pptx
PDF
dsa pdf.pdf
PPTX
sorting and searching.pptx
PPTX
Unit vii sorting
PPTX
Unit 7 sorting
PPTX
sorting-160810203705.pptx
PDF
DSA Lec 5+6(Search+Sort) (1).pdf
PPTX
Sorting and hashing concepts
PPTX
Sorting and hashing concepts
PPTX
Searching linear &amp; binary search
PPTX
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PPTX
1.4 Sorting.pptx
PPTX
Rahat &amp; juhith
PDF
Activity Selection Problem (Greedy Algorithm) with Step-by-Step Explanation
PPTX
Lecture_Oct26.pptx
PPTX
Searching and Sorting algorithms and working
Data Structures Unit 2 FINAL presentation.pptx
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
Lecture_4 (Sorting Algorithms) before mids - Copy.ppt
Ocw chp6 2searchbinary
Chapter #3 (Searchinmmmg ALgorithm).pptx
dsa pdf.pdf
sorting and searching.pptx
Unit vii sorting
Unit 7 sorting
sorting-160810203705.pptx
DSA Lec 5+6(Search+Sort) (1).pdf
Sorting and hashing concepts
Sorting and hashing concepts
Searching linear &amp; binary search
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
1.4 Sorting.pptx
Rahat &amp; juhith
Activity Selection Problem (Greedy Algorithm) with Step-by-Step Explanation
Lecture_Oct26.pptx
Searching and Sorting algorithms and working
Ad

More from SolomonEndalu (12)

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

Recently uploaded (20)

PPT
Teaching material agriculture food technology
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
Teaching material agriculture food technology
NewMind AI Weekly Chronicles - August'25 Week I
Dropbox Q2 2025 Financial Results & Investor Presentation
Mobile App Security Testing_ A Comprehensive Guide.pdf
Spectral efficient network and resource selection model in 5G networks
NewMind AI Monthly Chronicles - July 2025
Per capita expenditure prediction using model stacking based on satellite ima...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
The AUB Centre for AI in Media Proposal.docx
Advanced Soft Computing BINUS July 2025.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Review of recent advances in non-invasive hemoglobin estimation
Advanced methodologies resolving dimensionality complications for autism neur...
Unlocking AI with Model Context Protocol (MCP)
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
20250228 LYD VKU AI Blended-Learning.pptx
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....

Data Structure and Algorithm Chapter 2.ppsx Chapter 2.ppsx

  • 1. By Mr Solomon E. Simple Sorting and Searching Algorithms Chapter Two
  • 2. By Mr Solomon E. Outline • Searching Algorithms  Linear Search (Sequential Search) Binary Search • Sorting Algorithms  Bubble Sort  Insertion Sort  Sélection Sort
  • 3. By Mr Solomon E. 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. By Mr Solomon E. 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. By Mr Solomon E. 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. By Mr Solomon E. 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.
  • 8. By Mr Solomon E. Linear Search
  • 9. By Mr Solomon E. 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. By Mr Solomon E. 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"
  • 11. By Mr Solomon E. Linear search
  • 12. By Mr Solomon E. 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. By Mr Solomon E. 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. By Mr Solomon E. 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. By Mr Solomon E. 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. By Mr Solomon E. 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. By Mr Solomon E. Cont.. • Binary Search - Step-by-Step Example Searching for 23 in [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]:
  • 18. By Mr Solomon E. 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. By Mr Solomon E. 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. By Mr Solomon E. 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. By Mr Solomon E. 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. By Mr Solomon E. 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)
  • 24. By Mr Solomon E. Execution example
  • 25. By Mr Solomon E. 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. By Mr Solomon E. 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. By Mr Solomon E. 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]
  • 28. By Mr Solomon E. Example Execution
  • 29. By Mr Solomon E. 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. By Mr Solomon E. 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.
  • 31. By Mr Solomon E. Example Execution
  • 32. By Mr Solomon E. 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. By Mr Solomon E. 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. By Mr Solomon E. 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.
  • 35. By Mr Solomon E. Thanks!