Coventry University Logo
4061CEM - Programming and Algorithms 1

Searching and Sorting

Dr Ian Cornelius

Hello

  • Learning Objectives
    1. Understand the concept of searching and sorting in Python
    2. Demonstrate the ability to use sorting algorithms

Introduction to Searching Algorithms

  • Searching algorithms are a series of instructions to retrieve information stored in a data structure
  • Split into two categories:
    1. Sequential
    2. Interval
  • There are many searching algorithms
    • i.e. linear search, binary search, interpolation search and exponential search

Linear Search Demonstration

  • Demonstration of the Linear Search algorithm
    • Refer to the pre-recorded video for a demonstration

Binary Search Demonstration

  • Demonstration of the Binary Search algorithm
    • Refer to the pre-recorded video for a demonstration

Introduction to Sorting

  • Sorting algorithms are made up of a series of instructions
    • they accept a list as an input and outputs a sorted list
  • There are many examples of sorting algorithms:
    • i.e. selection sort, bubble sort, insertion sort, merge sort etc.
  • In this lecture we shall look at two sorting algorithms: bubble and insertion sort

Bubble Sort

  • A simple algorithm that works by swapping adjacent elements if they are in the incorrect order

Algorithm Instructions

  1. Compare each adjacent element in the list
  2. Swap the two elements if necessary
  3. Repeat the process for all elements in the list until the list is sorted

Bubble Sort Demonstration

  • Demonstration of the Bubble Sort algorithm
    • Refer to the pre-recorded video for a demonstration

Insertion Sort

  • A sorting algorithm that works in a similar method to how we would sort playing cards in our hands

Algorithm Instructions

  1. Compare the first element (\(n\)) of the list to the next one (\(n+1\))
  2. If \(n\) is less than \(n+1\), add \(n\) to the sorted sub-list, move onto the next element
  3. Compare the current element to all the elements in the sub-list
  4. Shift all elements in the sorted sub-list if it is greater than the current element
  5. Insert the current element into the sorted sub-list
  6. Repeat this process until the list is sorted

Insertion Sort Demonstration

  • Demonstration of the Insertion Sort algorithm
    • Refer to the pre-recorded video for a demonstration

Goodbye

  • Questions?
    • Post them in the Community Page on Aula
  • Contact Details: