1/63
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
What is abstraction?
Removing unnecessary detail from a problem to focus only on the important parts ā simplifying complexity to make a problem easier to solve
What is decomposition?
Breaking a complex problem down into smaller more manageable sub-problems ā each sub-problem can then be solved individually
What is algorithmic thinking?
Creating a step by step set of instructions to solve a problem ā thinking logically about the sequence of steps needed
What is a structure diagram?
A diagram that shows the structure of a problem broken into subsections ā shows how subsections link to each other ā used in the design stage before coding
What is pseudocode?
An informal way of describing an algorithm using plain English and programming-like structure ā not tied to any specific language ā used to plan programs before writing real code
What are the flowchart symbols you need to know?
Oval/rounded rectangle ā terminal (start/stop). Rectangle ā process. Parallelogram ā input/output. Diamond ā decision. Rectangle with lines on sides ā sub program. Arrow ā flow of control
What is a trace table?
A table used to track the values of variables at each step of an algorithm ā used to manually follow and check the logic of an algorithm ā helps identify errors
What is a syntax error?
An error that breaks the grammatical rules of the programming language ā prevents the program from being run or translated at all ā eg missing bracket or misspelled keyword
What is a logic error?
An error where the program runs without crashing but produces unexpected or incorrect output ā the code is syntactically correct but the logic is wrong ā eg using < instead of <=
What is nesting?
Placing one selection or iteration construct inside another ā eg an IF statement inside a FOR loop ā or a WHILE loop inside an IF statement
LINEAR SEARCH
What is a linear search?
Checks each element of a list one by one from the start until the target is found or the end of the list is reached ā works on both sorted and unsorted lists
What are the steps of a linear search?
Step 1 ā start at the first element. Step 2 ā check if the current element equals the target. Step 3 ā if yes ā return its position. Step 4 ā if no ā move to the next element. Step 5 ā if end of list reached without finding target ā return not found
What are the advantages of linear search?
Works on unsorted lists ā simple to understand and implement ā no pre-conditions required
What are the disadvantages of linear search?
Slow for large lists ā must check every element in the worst case ā inefficient compared to binary search on sorted data
Apply a linear search to find 15 in the list 8 3 15 22 7
Check 8 ā not 15. Check 3 ā not 15. Check 15 ā found at position 3 (index 2)
Apply a linear search to find 10 in the list 5 12 3 9 6
Check 5 ā no. Check 12 ā no. Check 3 ā no. Check 9 ā no. Check 6 ā no. End of list ā 10 not found
BINARY SEARCH
What is a binary search?
Divides a sorted list in half ā compares the middle element to the target ā searches the left half if target is smaller ā searches the right half if target is larger ā repeats until found or not found
What are the steps of a binary search?
Step 1 ā find the middle element of the list. Step 2 ā if middle equals target ā found. Step 3 ā if target is less than middle ā repeat on left half. Step 4 ā if target is greater than middle ā repeat on right half. Step 5 ā if list is empty ā not found
What is the key pre-condition for binary search?
The list must already be sorted ā binary search will not work correctly on an unsorted list
What are the advantages of binary search?
Much faster than linear search for large sorted lists ā eliminates half the remaining elements each time ā very efficient
What are the disadvantages of binary search?
List must be sorted first ā more complex to implement than linear search ā sorting has its own cost
Apply a binary search to find 23 in the list 5 10 15 20 23 27 30
Middle = 20. 23 > 20 so search right half: 23 27 30. Middle = 27. 23 < 27 so search left: 23. Middle = 23 ā found
Apply a binary search to find 7 in the list 2 4 7 9 11 13 15
Middle = 9. 7 < 9 so search left: 2 4 7. Middle = 4. 7 > 4 so search right: 7. Middle = 7 ā found
Apply a binary search to find 6 in the list 1 3 5 7 9
Middle = 5. 6 > 5 so search right: 7 9. Middle = 7. 6 < 7 so search left: empty ā 6 not found
BUBBLE SORT
What is a bubble sort?
Repeatedly compares adjacent pairs of elements ā swaps them if they are in the wrong order ā larger values bubble to the end ā repeats passes until no swaps occur in a full pass
What are the steps of a bubble sort?
Step 1 ā start at the beginning of the list. Step 2 ā compare the first and second elements ā if first is greater swap them. Step 3 ā move to the next pair and repeat. Step 4 ā continue to the end of the list ā this completes one pass. Step 5 ā repeat passes until a complete pass makes no swaps ā list is sorted
How do you know when bubble sort is finished?
When a complete pass through the list produces no swaps ā the list must be sorted
What are the advantages of bubble sort?
Simple to understand and implement ā can detect an already sorted list quickly ā uses very little additional memory
What are the disadvantages of bubble sort?
Very slow for large lists ā requires many comparisons and swaps ā inefficient compared to merge sort
Sort the list 5 3 8 1 using bubble sort ā show all passes
Pass 1 ā compare 5 and 3 swap ā 3 5 8 1. Compare 5 and 8 no swap. Compare 8 and 1 swap ā 3 5 1 8. Pass 2 ā compare 3 and 5 no swap. Compare 5 and 1 swap ā 3 1 5 8. Compare 5 and 8 no swap. Pass 3 ā compare 3 and 1 swap ā 1 3 5 8. Compare 3 and 5 no swap. Pass 4 ā no swaps ā sorted: 1 3 5 8
Sort the list 4 2 6 1 3 using bubble sort ā show pass 1 only
Compare 4 and 2 ā swap ā 2 4 6 1 3. Compare 4 and 6 ā no swap. Compare 6 and 1 ā swap ā 2 4 1 6 3. Compare 6 and 3 ā swap ā 2 4 1 3 6. End of pass 1
Sort the list 9 5 2 7 using bubble sort ā show all passes
Pass 1 ā 5 9 2 7 ā 5 2 9 7 ā 5 2 7 9. Pass 2 ā 2 5 7 9 ā 2 5 7 9. Pass 3 ā no swaps ā sorted: 2 5 7 9
MERGE SORT
What is a merge sort?
A divide and conquer algorithm ā repeatedly splits the list in half until each section contains only one element ā then merges pairs back together in sorted order ā very efficient for large lists
What are the steps of a merge sort?
Step 1 ā split the list into two halves. Step 2 ā recursively split each half again until you have single elements. Step 3 ā compare the first elements of two lists ā take the smaller one. Step 4 ā repeat until both lists are merged into one sorted list. Step 5 ā continue merging up until the full list is sorted
How does the merging step work in merge sort?
Take two sorted lists ā compare the first element of each ā take the smaller and add it to the new sorted list ā repeat until one list is empty ā add remaining elements ā result is one sorted merged list
What are the advantages of merge sort?
Very efficient for large lists ā consistent performance ā good for sorting linked lists ā stable sort
What are the disadvantages of merge sort?
More complex to understand and implement ā requires additional memory to store split lists during merging
Sort the list 4 2 7 1 using merge sort ā show all steps
Split ā 4 2 and 7 1. Split ā 4 and 2 and 7 and 1. Merge 4 and 2 ā 2 4. Merge 7 and 1 ā 1 7. Merge 2 4 and 1 7 ā compare 2 and 1 take 1 ā compare 2 and 7 take 2 ā compare 4 and 7 take 4 ā take 7. Result: 1 2 4 7
Sort the list 8 3 5 1 using merge sort ā show all steps
Split ā 8 3 and 5 1. Split ā 8 and 3 and 5 and 1. Merge 8 and 3 ā 3 8. Merge 5 and 1 ā 1 5. Merge 3 8 and 1 5 ā compare 3 and 1 take 1 ā compare 3 and 5 take 3 ā compare 8 and 5 take 5 ā take 8. Result: 1 3 5 8
Sort the list 6 4 2 8 3 1 using merge sort ā show splitting stage only
Split ā 6 4 2 and 8 3 1. Split ā 6 4 and 2 and 8 3 and 1. Split ā 6 and 4 and 2 and 8 and 3 and 1. Now merge back in pairs
INSERTION SORT
What is an insertion sort and how does it work?
Insertion sort builds a sorted list one element at a time ā it takes each element from the unsorted part and inserts it into the correct position in the sorted part ā like sorting playing cards in your hand ā you pick up each card and slot it into the right place among the cards already sorted
What are the steps of an insertion sort ā explained clearly?
Step 1 ā the first element is already considered sorted. Step 2 ā take the next element (the key). Step 3 ā compare the key to each element in the sorted portion moving right to left. Step 4 ā shift each sorted element one position to the right if it is greater than the key. Step 5 ā insert the key into the gap created. Step 6 ā repeat steps 2 to 5 for every remaining element
How does insertion sort slot an element into the right place?
It compares the element being inserted (the key) with the sorted elements to its left ā each sorted element that is bigger than the key gets moved one place to the right ā this creates a gap ā the key is then placed in that gap ā the sorted section grows by one each time
What are the advantages of insertion sort?
Simple to implement ā very efficient for small or nearly sorted lists ā builds sorted list in place using very little extra memory ā can sort as data arrives
What are the disadvantages of insertion sort?
Slow for large unsorted lists ā many comparisons and shifts needed in the worst case
Sort the list 5 3 8 1 using insertion sort ā show every step clearly
Start ā sorted section: 5. Unsorted: 3 8 1. Take key = 3. Compare 3 with 5 ā 5 > 3 so shift 5 right. Insert 3 in gap. List now: 3 5 8 1. Take key = 8. Compare 8 with 5 ā 5 < 8 so no shift. Insert 8 here. List now: 3 5 8 1. Take key = 1. Compare 1 with 8 ā shift 8 right. Compare 1 with 5 ā shift 5 right. Compare 1 with 3 ā shift 3 right. Insert 1 at start. Sorted: 1 3 5 8
Sort the list 7 2 9 4 using insertion sort ā show every step clearly
Start ā sorted: 7. Take key = 2. 7 > 2 ā shift 7 right. Insert 2. List: 2 7 9 4. Take key = 9. 7 < 9 ā no shift. Insert 9. List: 2 7 9 4. Take key = 4. 9 > 4 ā shift 9. 7 > 4 ā shift 7. 2 < 4 ā stop. Insert 4. Sorted: 2 4 7 9
Sort the list 6 1 4 2 using insertion sort ā show every step clearly
Start ā sorted: 6. Take key = 1. 6 > 1 ā shift 6. Insert 1. List: 1 6 4 2. Take key = 4. 6 > 4 ā shift 6. 1 < 4 ā stop. Insert 4. List: 1 4 6 2. Take key = 2. 6 > 2 ā shift 6. 4 > 2 ā shift 4. 1 < 2 ā stop. Insert 2. Sorted: 1 2 4 6
Sort the list 3 7 1 5 2 using insertion sort ā show every step
Sorted: 3. Key=7 ā 3<7 no shift ā insert. List: 3 7 1 5 2. Key=1 ā shift 7 ā shift 3 ā insert at start. List: 1 3 7 5 2. Key=5 ā shift 7 ā 3<5 stop ā insert. List: 1 3 5 7 2. Key=2 ā shift 7 ā shift 5 ā shift 3 ā 1<2 stop ā insert. Sorted: 1 2 3 5 7
COMPARISON QUESTIONS
Which sorting algorithm works by comparing and swapping adjacent elements?
Bubble sort
Which sorting algorithm works by dividing the list in half repeatedly then merging?
Merge sort
Which sorting algorithm works by taking each element and inserting it into the correct position in the sorted portion?
Insertion sort
Which searching algorithm requires the list to be sorted first?
Binary search
Which searching algorithm works on any list regardless of order?
Linear search
Which is more efficient for large lists ā bubble sort or merge sort?
Merge sort ā it has O(n log n) complexity compared to bubble sort's O(n²) ā much faster for large data sets
Which sort would be best for a nearly sorted list of 10 items?
Insertion sort ā very efficient for small and nearly sorted lists
Which sort would be best for a list of 1 million items?
Merge sort ā consistent and efficient performance for large data sets
When would you choose linear search over binary search?
When the list is unsorted and sorting it first would take too long ā or when the list is very small making the efficiency difference negligible