Back to 课程

Computer Science GCES AQA

0% Complete
0/0 Steps
  1. Representing Algorithms Aqa
    4 主题
  2. Efficiency Of Algorithms Aqa
    1 主题
  3. Searching Algorithms Aqa
    3 主题
  4. Sorting Algorithms Aqa
    3 主题
  5. Data Types Aqa
    1 主题
  6. Programming Concepts Aqa
    5 主题
  7. Arithmetic Relational And Boolean Operations Aqa
    1 主题
  8. Data Structures Aqa
    3 主题
  9. String Manipulation Aqa
    1 主题
  10. Random Number Generation Aqa
    1 主题
  11. Structured Programming Aqa
    2 主题
  12. Robust And Secure Programming Aqa
    4 主题
  13. Number Bases Aqa
    2 主题
  14. Converting Between Number Bases Aqa
    3 主题
  15. Units Of Information Aqa
    9 主题
  16. Hardware And Software Aqa
    4 主题
  17. Boolean Logic Aqa
    3 主题
  18. Programming Languages And Translators Aqa
    2 主题
  19. Cpu Architecture Performance And Embedded Systems Aqa
    4 主题
  20. Memory Aqa
    2 主题
  21. Secondary Storage Aqa
    3 主题
  22. Fundamentals Of Computer Networks Aqa
    8 主题
  23. Fundamentals Of Cyber Security Aqa
    1 主题
  24. Methods Of Preventing Cyber Security Threats Aqa
    1 主题
  25. Relational Databases Aqa
    2 主题
  26. Ethical Legal And Environmental Impacts Aqa
    2 主题
课 Progress
0% Complete

Exam code:8525

What is a sorting algorithm?

  • Sorting algorithms are precise step-by-step instructions that a computer can follow to efficiently sort data in massive datasets

  • Two common sorting algorithms are:

    • Bubble sort

    • Merge sort

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?

Table detailing three steps for dataset processing: 1. Divide dataset repeatedly in half. 2. Merge pairs of sub-datasets by comparing first values. 3. Repeat step 2 until one dataset.

Example

  • Perform a merge sort on the following dataset

Table showing the merge sort algorithm. The dataset [7, 4, 1, 2, 6, 3, 8, 5] is repeatedly divided, then merged back together by comparing values, resulting in a sorted array.

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!

A Merge Sort in Python

# List of numbers to perform merge sort on

numbers = [7, 4, 1, 2, 6, 3, 8, 5]

# Merge function to merge two sorted lists

def merge(left, right):

merged = []

left_index, right_index = 0, 0

# Merge the two sorted lists

while left_index < len(left) and right_index < len(right):

if left[left_index] < right[right_index]:

merged.append(left[left_index])

left_index += 1

else:

merged.append(right[right_index])

right_index += 1

# Append remaining elements from left or right sublist if there are any remaining elements in the left sublist

while left_index < len(left):

merged.append(left[left_index])

left_index += 1

# If there are any remaining elements in the right sublist

while right_index < len(right):

merged.append(right[right_index])

right_index += 1


return merged

# Merge sort implementation without using a separate function

def merge_sort(arr):

# Checks to see if the list has 1 or 0 elements, it's already sorted

if len(arr) <= 1:

return arr

# Split the list into two halves

mid = len(arr) // 2

left_half = merge_sort(arr[:mid]) # Split and recursively sort left half

right_half = merge_sort(arr[mid:]) # Split and recursively sort right half

# Merge the sorted halves

return merge(left_half, right_half)

# Perform merge sort

sorted_numbers = merge_sort(numbers)

print("Sorted numbers:", sorted_numbers)

Responses

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