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!

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

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