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
SolomonEndalu
 
PPTX
Searching Algorithms - Foundations of Algorithms
ssusere05275
 
PPTX
Chapter 2 Sorting and Searching .pptx.soft
kuruabeje7
 
PPTX
Searching techniques
ER Punit Jain
 
PPTX
sorting-160810203705.pptx
AnSHiKa187943
 
PPT
Sorting
Samsil Arefin
 
PPT
search_sort.ppt
SwatiHans10
 
PPTX
searching in data structure.pptx
chouguleamruta24
 
Chapter 2. data structure and algorithm
SolomonEndalu
 
Searching Algorithms - Foundations of Algorithms
ssusere05275
 
Chapter 2 Sorting and Searching .pptx.soft
kuruabeje7
 
Searching techniques
ER Punit Jain
 
sorting-160810203705.pptx
AnSHiKa187943
 
Sorting
Samsil Arefin
 
search_sort.ppt
SwatiHans10
 
searching in data structure.pptx
chouguleamruta24
 

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

PPTX
Data Structures Unit 2 FINAL presentation.pptx
dilipd20
 
PPTX
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
tahliildhoore54
 
PDF
Ocw chp6 2searchbinary
Prashant Rai
 
PPTX
Chapter #3 (Searchinmmmg ALgorithm).pptx
hekmatyarzahir44
 
PDF
dsa pdf.pdf
DewashishDwivedi
 
PPTX
sorting and searching.pptx
ParagAhir1
 
PPTX
Unit vii sorting
Tribhuvan University
 
PPTX
Unit 7 sorting
Dabbal Singh Mahara
 
PPTX
sorting-160810203705.pptx
VarchasvaTiwari2
 
PDF
DSA Lec 5+6(Search+Sort) (1).pdf
MustafaJutt4
 
PPTX
Sorting and hashing concepts
LJ Projects
 
PPTX
Sorting and hashing concepts
LJ Projects
 
PPTX
Searching linear &amp; binary search
nikunjandy
 
PPTX
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PradipTadme
 
PPTX
1.4 Sorting.pptx
Sujan527908
 
PPTX
Rahat &amp; juhith
Rj Juhith
 
PDF
Activity Selection Problem (Greedy Algorithm) with Step-by-Step Explanation
suryatejaalt2
 
PPTX
Lecture_Oct26.pptx
SylrizcinMarieManzo3
 
PPTX
Searching and Sorting algorithms and working
RitikaLohiya2
 
PPTX
Chapter2.1 .pptx
RojaPogul1
 
Data Structures Unit 2 FINAL presentation.pptx
dilipd20
 
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
tahliildhoore54
 
Ocw chp6 2searchbinary
Prashant Rai
 
Chapter #3 (Searchinmmmg ALgorithm).pptx
hekmatyarzahir44
 
dsa pdf.pdf
DewashishDwivedi
 
sorting and searching.pptx
ParagAhir1
 
Unit vii sorting
Tribhuvan University
 
Unit 7 sorting
Dabbal Singh Mahara
 
sorting-160810203705.pptx
VarchasvaTiwari2
 
DSA Lec 5+6(Search+Sort) (1).pdf
MustafaJutt4
 
Sorting and hashing concepts
LJ Projects
 
Sorting and hashing concepts
LJ Projects
 
Searching linear &amp; binary search
nikunjandy
 
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PradipTadme
 
1.4 Sorting.pptx
Sujan527908
 
Rahat &amp; juhith
Rj Juhith
 
Activity Selection Problem (Greedy Algorithm) with Step-by-Step Explanation
suryatejaalt2
 
Lecture_Oct26.pptx
SylrizcinMarieManzo3
 
Searching and Sorting algorithms and working
RitikaLohiya2
 
Chapter2.1 .pptx
RojaPogul1
 
Ad

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
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 

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!