Binary search - Searching and sorting algorithms - AQA - GCSE Computer Science Revision - AQA - BBC Bitesize (2024)

Binary search

Another example of a computer searching is binary search. This is a more complex algorithm than and requires all items to be in order.

With each that is run, half of the is removed from the search. To do this, the algorithm uses the of items in the list - an index is the number given to the position of an item in a list.

To determine the next item to be checked in a list, the midpoint has to be identified. This is found by adding the lowest index to the highest index and dividing by two, then rounding either up or down to the nearest if needed. Whether we round up or down does not matter, as long as within a particular search the same method is applied. For example, if the highest index in a list is 8 and the lowest is 0, the midpoint can be found using the following sum:

8/2 = 4

The algorithm runs as follows:

  1. Start by setting the counter to the middle position in the list.
  2. If the value held there is a match, the search ends.
  3. If the value at the midpoint is less than the value to be found, the list is divided in half, the lower half of the list is ignored and the search keeps to the upper half of the list.
  4. Otherwise, if the value at the midpoint is greater than the value to be found, the upper half of the list is ignored and the search keeps to the lower half of the list.
  5. The search moves to the midpoint of the remaining items. Steps 2 through 4 continue until a match is made or there are no more items to be found.

Binary search example

This algorithm could be used to search the following list for the number 7:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

In a , this would look like:

Search termStartEndMidFoundList
71105False[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
6108False[6, 7, 8, 9, 10]
676False[6, 7]
777True[7]
Search term7
Start1
End10
Mid5
FoundFalse
List[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Search term
Start6
End10
Mid8
FoundFalse
List[6, 7, 8, 9, 10]
Search term
Start6
End7
Mid6
FoundFalse
List[6, 7]
Search term
Start7
End7
Mid7
FoundTrue
List[7]

In , this would look like:

Find <-- 7Found <-- FalseStart <-- 0End <-- length(list)WHILE Found = False AND Start <= End Mid <-- (Start + End) DIV 2 IF list[Mid] = Find THEN OUTPUT 'Found at' + Mid Found <-- True ELSE IF List[Mid] > Find THEN End <-- Mid - 1 ELSE Start <-- Mid + 1 ENDIF ENDIFENDWHILEIF Found = False THEN OUTPUT 'Not found'ENDIF
Binary search - Searching and sorting algorithms - AQA - GCSE Computer Science Revision - AQA - BBC Bitesize (2024)

FAQs

What is a binary search in GCSE computer science? ›

A binary search is an efficient method of searching an ordered list. A binary search works like this: Start by setting the counter to the middle position in the list. If the value held there is a match, the search ends. If the value at the midpoint is less than the value to be found, the list is divided in half.

What is an algorithm BBC bitesize GCSE? ›

Algorithms are step-by-step plans for solving problems. Algorithms can be designed using pseudo-code, flowcharts, written descriptions and program code. There are also some standard algorithms for searching and sorting. Part of Computer SciencePrinciples of computer science. Save to My Bitesize.

Do you round up or down in binary search? ›

In the case of a decimal number, we round down to find the actual number of guesses. Therefore, for a 1000-element array, binary search would require at most 10 guesses. For the Tycho-2 star catalog with 2,539,913 stars, the closest lower power of 2 is ‍ (which is 2,097,152), so we would need at most 22 guesses.

Is binary search tough? ›

Conceptually, binary search is a very easy to understand algorithm. However, implementing it is very difficult, especially when you try to implement many variants of it.

What is a sorting algorithm GCSE? ›

A sort algorithm is a set of instructions to arrange a dataset into a particular order.

What is the difference between binary and linear search? ›

Difference Between Linear and Binary Search: Linear vs Binary Search. Linear Search sequentially checks each element in the list until it finds a match or exhausts the list. Binary Search continuously divides the sorted list, comparing the middle element with the target value.

Does binary search need to be sorted? ›

Binary search is faster than linear search except for small arrays. However, the array must be sorted first to be able to apply binary search.

How to write an algorithm in GCSE computer science? ›

There are various ways to describe algorithms, from natural language (which can imprecise), through flowcharts (which are often used to plan out the sequence in which events must take place in an algorithm) to pseudocode which is a much more formal version of natural language, designed to imitate a programming language ...

What is an algorithm in computer science aqa? ›

Algorithms are step-by-step plans for solving problems. They can be designed using pseudo-code and flowcharts. Part of Computer ScienceComputational thinking and problem solving.

Is algorithm a math or science? ›

An algorithm, especially in mathematics, is a step-by-step procedure that can be used to solve computations or other mathematical problems. So, an algorithm can be thought of as a set of directions for solving mathematical computations and problems.

What is binary search in GCSE? ›

An alternative to a linear search, the binary search starts in the middle of a list and removes half of the items from the list until the item you are looking for is found. It is usually more efficient than a linear search.

What is the rule of binary search? ›

Binary search is an efficient algorithm for finding an item from a sorted list of items. It works by repeatedly dividing in half the portion of the list that could contain the item, until you've narrowed down the possible locations to just one.

What is binary search in computer science? ›

Binary search is an efficient algorithm for finding an item from a sorted list of items. It works by repeatedly dividing in half the portion of the list that could contain the item, until you've narrowed down the possible locations to just one.

What is binary GCSE computer science? ›

Binary is a base 2 number system. It is the number system used by computers. Each number position can only contain one of two digits, either a 1 or a 0.

What is binary search explain with? ›

Binary search works on the divide and conquer approach, i.e. the list from which the search is to be done is divided into two halves, and then the searched element is compared with the middle element in the array. If the element is found, then the index of the middle element is returned.

What is a searching algorithm in GCSE? ›

A search algorithm is a set of instructions for finding a specific item of data within a data set. Computer systems can store and process billions of pieces of data so it is vital that computers can find the information they need efficiently.

Top Articles
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 5864

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.