Computer Science LearnITWithMrC ⛯ Year 7 Year 8 Year 9 GCSE
ICT
Responsive image

LO 1 - Computational Thinking

abstraction

Algorithms

Searching and sorting

We are going to look at the idea of divide and conquer problem solving in the context of search algorithms. We will also introduce the idea of efficiency analysis as a way of comparing algorithms.

Searching - Divide and conquer

Searching for things by checking each place one by one seems an obvious thing to do - it is a simple algorithm. However, it is very slow. Divide and conquer gives much faster algorithms. It is the approach to solving a problem where you split the problem in half in a way which leaves you with a simpler version of the same problem - you are still looking for the same thing, just on a problem half the size. Do that over and over and you get the answer incredibly quickly.

Binary search is one search algorithm that uses such a divide and conquer approach. It assumes your data is sorted. Rather than checking each piece of data in turn, you go to the middle element and use that as a signpost. You check whether the thing you are looking for is bigger or smaller that it and then only search the bottom or top half of the data as appropriate. Because the data is in order you can tell which side it must be. You keep doing that until you only have one piece of data left and check if it is the thing you are after.

Binary Search

In computer science, binary search, also known as half-interval search or logarithmic search, is a search algorithm that finds the position of a target value within a sorted array. It compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful.

decomposition

Binary search runs in at worst logarithmic time, making O(log n) comparisons, where n is the number of elements in the array and log is the binary logarithm.


Sorting

Almost any list that comes out of a computer is sorted into some sort of order, and there are many more sorted lists inside computers that the user doesn’t see. Many clever algorithms have been devised for putting values into order efficiently.

The sorting algorithms you will learn share two basic operations in common. These operations are the comparison operation and the swap operation. We will look at each one in more detail before we examine our sorting algorithms.

  1. Copy the contents of cell A to temp
  2. Copy the contents of cell B to cell A
  3. Copy the contents of temp to cell B

Quick sort

break

Exercise

Design a quick sort algorithm use the four steps of computational thinking to solve the problem

Implement your desgin using python clearly comment the code you have produced

Extension Task

Come up with your own algorithm for sorting a list of numbers into the correct order



  • Learning Objectives

  • I can identify and describe problems and processes.
  • I can break down problems and processes into distinct steps.
  • I can describe problems and processes as a set of structured steps.
  • I can communicate the key features of problems and processes to others.