Back to 课程

Computer Science GCES OCR

0% Complete
0/0 Steps
  1. Cpu Architecture Performance And Embedded Systems Ocr
    5 主题
  2. Primary And Secondary Storage Ocr
    6 主题
  3. Data Storage And Compression Ocr
    12 主题
  4. Networks And Topologies Ocr
    6 主题
  5. Wired And Wireless Networks Protocols And Layers Ocr
    6 主题
  6. Identifying And Preventing Threats To Computer Systems And Networks Ocr
    2 主题
  7. Operating Systems And Utility Software Ocr
    2 主题
  8. Ethical Legal Cultural And Environmental Impact Ocr
    2 主题
  9. Computational Thinking Searching And Sorting Algorithms Ocr
    3 主题
  10. Designing Creating And Refining Algorithms Ocr
    5 主题
  11. Programming Fundamentals And Data Types Ocr
    5 主题
  12. Additional Programming Techniques Ocr
    7 主题
  13. Defensive Design And Testing Ocr
    6 主题
  14. Boolean Logic Diagrams Ocr
    2 主题
  15. Programming Languages And Integrated Development Environments Ides Ocr
    3 主题
  16. The Exam Papers Ocr
    2 主题
  17. Structuring Your Responses Ocr
    3 主题
课 Progress
0% Complete

Exam code:J277

What is a sorting algorithm?

  • In GCSE Computer Science, sorting algorithms are precise step-by-step instructions that a computer can follow to sort data in massive datasets efficiently

  • Three common sorting algorithms are:

    • Bubble sort

    • Merge sort

    • Insertion sort

Bubble Sort

What is a bubble sort?

  • A bubble sort is a simple sorting algorithm that starts at the beginning of a dataset and checks values in ‘pairs’ and swaps them if they are not in the correct order

  • One full run of comparisons from beginning to end is called a ‘pass‘, a bubble sort may require multiple ‘passes‘ to sort the dataset

  • The algorithm is finished when there are no more swaps to make

How do you perform a bubble sort?

Step

Instructions

1

Compare the first two values in the dataset

2

IF they are in the wrong order…

  • Swap them

3

Compare the next two values

4

REPEAT step 2 & 3 until you reach the end of the dataset (pass 1)

5

IF you have made any swaps…

  • REPEAT from the start (pass 2,3,4…)

6

ELSE you have not made any swaps…

  • STOP! the list is in the correct order

Example

  • Perform a bubble sort on the following dataset

Instructions for sorting dataset using bubble sort. Steps involve comparing and swapping values in a sequence: 5, 2, 4, 1, 6, 3 until the correct order is achieved.
Table explains sorting algorithm steps. After pass 2, sequence is 2, 1, 4, 3, 5, 6. After pass 3, it’s 1, 2, 3, 4, 5, 6. No swaps in pass 4, final list correct.

Examiner Tips and Tricks

In the exam you do not have to show every swap that takes place in a bubble sort. You can show the outcome of a bubble sort at the end of each pass. If you have the outcome of each pass correct then a bubble sort has been implemented correctly and all marks will be given!

Worked Example

A program uses a file to store a list of words.

A sample of this data is shown

Milk

Eggs

Bananas

Cheese

Potatoes

Grapes

Show the stages of a bubble sort when applied to data shown [2]

How to answer this question

  • We need to sort the values in to alphabetical order from A-Z

  • You CAN use the first letter of each word to simplify the process

Answer

  • E, B, C, M, G, P (pass 1)

  • B, C, E, G, M, P (pass 2)

A bubble sort in python

# unsorted dataset
nums=[66, 7, 69, 50, 42, 80, 71, 321, 67, 8, 39] # count the length of the dataset
numlength = len(nums) # sets a flag to initiate the loop
swaps = True while swaps == True : swaps = False # loop through the dataset for y in range(numlength-1) : # if the first number is bigger than the second number if nums[y] > nums[y+1] : # swap the numbers using a temporary variable temp = nums[y] nums[y] = nums[y+1] nums[y+1] = temp # sets the flag to true swaps = True # prints the sorted list
print (nums)

Merge Sort

What is a merge sort?

  • A merge sort is a sorting algorithm that uses the ‘divide and conquer‘ strategy of dividing a dataset into smaller sub-datasets and merging them back together in the correct order

How do you perform a merge sort?

Step

Instruction

1

Divide the dataset into individual datasets by repeatedly splitting the dataset in half (DIVIDE)

2

Merge pairs of sub-datasets together by comparing the first value in each dataset (CONQUER)

3

REPEAT step 2 until all sub-datasets are merged together into one dataset

Example

  • Perform a merge sort on the following dataset

Flowchart showing the merge sort algorithm with a dataset of 7, 4, 1, 2, 6, 3, 8, and 5. It splits, merges, and reorders them into 1, 2, 3, 4, 5, 6, 7, and 8.

Examiner Tips and Tricks

In the exam, the divide stage could already be done for you and you would only need to demonstrate the conquer stage!

Insertion Sort

What is an insertion sort?

  • The insertion sort sorts one item at a time by placing it in the correct position of an unsorted list. This process repeats until all items are in the correct position

  • Values in the dataset can move as many places as they need

How do you perform an insertion sort?

Step

Instruction

1

Take the first value in the dataset, this is now the sorted list

2

Look at the next value in the dataset and compare it to the first value

IF it is smaller

  • insert it into the correct position to the left

3

ELSE

  • Keep in the same position

4

REPEAT steps 2 & 3 until all values in the dataset have been compared, the list should now be in order

Example

  • Perform an insertion sort on the following dataset

<img alt=”Table explaining sorting steps for dataset [54, 27, 17, 9, 40, 12]. Steps show comparisons, insertions to sorted list, and final order [9, 12, 17, 27, 40, 54].” class=”ContentBlock_figure__vJw2q” data-nimg=”1″ decoding=”async” height=”1334″ loading=”lazy” sizes=”(max-width: 320px) 320w, (max-width: 640px) 640w, (max-width: 960px) 960w, (max-width: 1280px) 1280w, 1920w” src=”https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=3840/https://cdn.savemyexams.com/uploads/2024/07/25239_standard-sorting-algorithm-insertion-sort—table-1.jpg” srcset=”https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=16/https://cdn.savemyexams.com/uploads/2024/07/25239_standard-sorting-algorithm-insertion-sort—table-1.jpg 16w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=32/https://cdn.savemyexams.com/uploads/2024/07/25239_standard-sorting-algorithm-insertion-sort—table-1.jpg 32w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=48/https://cdn.savemyexams.com/uploads/2024/07/25239_standard-sorting-algorithm-insertion-sort—table-1.jpg 48w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=64/https://cdn.savemyexams.com/uploads/2024/07/25239_standard-sorting-algorithm-insertion-sort—table-1.jpg 64w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=96/https://cdn.savemyexams.com/uploads/2024/07/25239_standard-sorting-algorithm-insertion-sort—table-1.jpg 96w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=128/https://cdn.savemyexams.com/uploads/2024/07/25239_standard-sorting-algorithm-insertion-sort—table-1.jpg 128w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=256/https://cdn.savemyexams.com/uploads/2024/07/25239_standard-sorting-algorithm-insertion-sort—table-1.jpg 256w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=384/https://cdn.savemyexams.com/uploads/2024/07/25239_standard-sorting-algorithm-insertion-sort—table-1.jpg 384w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=640/https://cdn.savemyexams.com/uploads/2024/07/25239_standard-sorting-algorithm-insertion-sort—table-1.jpg 640w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=750/https://cdn.savemyexams.com/uploads/2024/07/25239_standard-sorting-algorithm-insertion-sort—table-1.jpg 750w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=828/https://cdn.savemyexams.com/uploads/2024/07/25239_standard-sorting-algorithm-insertion-sort—table-1.jpg 828w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=1080/https://cdn.savemyexams.com/uploads/2024/07/25239_standard-sorting-algorithm-insertion-sort—table-1.jpg 1080w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=1200/https://cdn.savemyexams.com/uploads/2024/07/25239_standard-sorting-algorithm-insertion-sort—table-

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注