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)

PPTX
Searching, Sorting and Hashing Techniques
Selvaraj Seerangan
 
PPTX
PPT.pptx Searching and Sorting Techniques
Vaibhav Parjane
 
PPTX
LLL3 Searching & Sorting algorithms.pptx
plabonrahmanme
 
PPTX
Searching and Sorting Algorithms in Data Structures
poongothai11
 
PPT
search_sort Search sortSearch sortSearch sortSearch sort
Shanmuganathan C
 
PPTX
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
PPTX
Data Structures_ Sorting & Searching
ThenmozhiK5
 
PPT
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
Kanupriya731200
 
PPTX
Chapter-2.pptx
selemonGamo
 
PPTX
9.Sorting & Searching
Mandeep Singh
 
DOCX
MODULE 5-Searching and-sorting
nikshaikh786
 
PDF
Chapter Two.pdf
abay golla
 
PPTX
Searching and sorting
PoojithaBollikonda
 
PDF
Chapter 14 Searching and Sorting
MuhammadBakri13
 
PDF
Quick sort,bubble sort,heap sort and merge sort
abhinavkumar77723
 
PPTX
Searching,sorting
LavanyaJ28
 
ODP
Data operatons & searching and sorting algorithms
Anushdika Jeganathan
 
PDF
Searching and sorting by B kirron Reddi
B.Kirron Reddi
 
PPTX
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
PDF
Chapter 1 - Introduction to Searching and Sorting Algorithms - Student.pdf
mylinhbangus
 
Searching, Sorting and Hashing Techniques
Selvaraj Seerangan
 
PPT.pptx Searching and Sorting Techniques
Vaibhav Parjane
 
LLL3 Searching & Sorting algorithms.pptx
plabonrahmanme
 
Searching and Sorting Algorithms in Data Structures
poongothai11
 
search_sort Search sortSearch sortSearch sortSearch sort
Shanmuganathan C
 
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Data Structures_ Sorting & Searching
ThenmozhiK5
 
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
Kanupriya731200
 
Chapter-2.pptx
selemonGamo
 
9.Sorting & Searching
Mandeep Singh
 
MODULE 5-Searching and-sorting
nikshaikh786
 
Chapter Two.pdf
abay golla
 
Searching and sorting
PoojithaBollikonda
 
Chapter 14 Searching and Sorting
MuhammadBakri13
 
Quick sort,bubble sort,heap sort and merge sort
abhinavkumar77723
 
Searching,sorting
LavanyaJ28
 
Data operatons & searching and sorting algorithms
Anushdika Jeganathan
 
Searching and sorting by B kirron Reddi
B.Kirron Reddi
 
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
Chapter 1 - Introduction to Searching and Sorting Algorithms - Student.pdf
mylinhbangus
 

More from SolomonEndalu (12)

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

Recently uploaded (20)

PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
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.