CIE 9618 A Level Computer Science: Abstract Data Types, Searching, and Sorting Notes 1. Searching Algorithms **Linear Search**: Sequentially checks each element. Advantages: Simple to implement. Disadvantages: Inefficient for large datasets. **Binary Search**: Divides sorted list in half repeatedly. Precondition: List must be sorted. Performance: O(log n) time complexity. 2. Sorting Algorithms **Bubble Sort**: Repeatedly steps through the list, compares adjacent elements, and swaps them if in the wrong order. Performance: O(n^2). Slow on large lists. **Insertion Sort**: Builds the sorted array one element at a time. Performance: O(n^2) worst case, O(n) best case (when list is already sorted). 3. Abstract Data Types (ADTs) ADTs are data structures defined by their behavior (operations) not implementation. Common ADTs: - Stack (LIFO) - Queue (FIFO) - Linked List - Dictionary (Key-Value store) - Binary Tree - Graph ADTs can be implemented using arrays, linked lists, or even other ADTs. 4. Algorithm Performance Performance Criteria: - Time Complexity: How long it takes (Big O notation) - Space Complexity: How much memory it uses **Example Comparisons**: - Linear vs Binary Search (O(n) vs O(log n)) - Bubble vs Insertion Sort (both O(n^2)) 5. Algorithms **Linear Search Pseudocode**: FOR i ← 0 TO length(array)-1 IF array[i] = target THEN RETURN i ENDIF NEXT RETURN -1 **Binary Search Pseudocode**: left ← 0 right ← length(array) - 1 WHILE left <= right mid ← (left + right) DIV 2 IF array[mid] = target THEN RETURN mid ELSE IF array[mid] < target THEN left ← mid + 1 ELSE right ← mid - 1 ENDIF ENDWHILE RETURN -1 **Insertion Sort**: FOR i ← 1 TO length(array)-1 key ← array[i] j←i-1 WHILE j >= 0 AND array[j] > key array[j+1] ← array[j] j←j-1 ENDWHILE array[j+1] ← key NEXT **Bubble Sort**: FOR i ← 0 TO length(array)-1 FOR j ← 0 TO length(array)-i-2 IF array[j] > array[j+1] THEN SWAP array[j] and array[j+1] ENDIF NEXT NEXT 6. Algorithms for Data Structures **Search in Linked List**: node ← head WHILE node ≠ NULL IF node.data = target THEN RETURN node ENDIF node ← node.next ENDWHILE RETURN NULL **Search in Binary Tree (Recursive)**: IF root = NULL THEN RETURN NULL IF root.data = target THEN RETURN root IF target < root.data THEN RETURN Search(root.left, target) ELSE RETURN Search(root.right, target) **Insert into Stack**: PUSH(stack, item) ← stack[top+1] ← item; top ← top+1 **Insert into Queue**: ENQUEUE(queue, item) ← queue[rear+1] ← item; rear ← rear+1 **Insert into Linked List**: new_node.next ← head; head ← new_node **Insert into Binary Tree**: Standard binary insert based on BST logic **Delete from Stack**: POP(stack) ← top ← top - 1 **Delete from Queue**: DEQUEUE(queue) ← front ← front + 1 **Delete from Linked List**: Traverse to target node and adjust pointers 7. Graphs as ADTs Graphs: Set of nodes (vertices) connected by edges. Can be directed or undirected. Can be weighted or unweighted. Use cases: Social networks, routing algorithms, dependency tracking. Representations: Adjacency list (space efficient), Adjacency matrix (fast access). Graphs are ADTs because their implementation is abstracted from their usage. 8. Big O Complexity Examples - Linear Search: O(n) - Binary Search: O(log n) - Bubble Sort / Insertion Sort: O(n^2) - Stack operations: O(1) - Queue operations: O(1) - Binary Search Tree (balanced): O(log n) for insert/search/delete - Graph traversal (BFS/DFS): O(V + E) where V=vertices, E=edges Computational thinking is a set of fundamental skills used to produce a solution to a problem1. Skills such as abstraction, decomposition, and algorithmic thinking are used to study a problem and design a solution that can be implemented1. Algorithms are solutions to problems expressed as a sequence of defined steps2. Abstract Data Types (ADTs) are collections of data and a set of operations on those data3.... Searching Methods To find an item stored in an array, the array needs to be searched7. • Linear search: Each element of an array is checked in order, from the lower bound to the upper bound, until the item is found or the upper bound is reached7.... This method works for a list in which the items can be stored in any order9. The pseudocode for a linear search algorithm on a 1D array myList involves iterating through the array, comparing each element with the item being searched for10. The algorithm terminates when the item is found or the upper bound is reached11. The identifier table for the linear search algorithm includes item, myList, upperBound, lowerBound, index, and found9.... • Binary search: A binary search is more efficient if a list is already sorted13. The value of the middle item in the list is first tested13. The half of the list that does not contain the required item is discarded13. Then, the next item to be tested is the middle item of the half of the list that was kept13. This is repeated until the required item is found or there is nothing left to test13. The pseudocode for a binary search algorithm on a sorted 1D array myList involves calculating the middle index and adjusting the upperBound or lowerBound based on the comparison with the item14. The loop repeats until the item is found or the bounds cross each other (indicating the item is not found)14. • Performance: As the size of a list increases, the average time taken to retrieve an item using a linear search increases correspondingly9. A binary search usually takes far fewer comparisons than a linear search to find an item in a list15. For example, in a list of 1024 elements, a binary search might take a maximum of 16 comparisons, while a linear search could take up to 102415. Sorting Methods Lists can be more useful if the items are sorted in a meaningful order12. • Bubble sort: Each element of an array is compared with the next element and swapped if the elements are in the wrong order, starting from the lower bound12. This comparison is repeated with one less element in the list until there is only one element left or no swaps are made12. The pseudocode for a bubble sort algorithm on a 1D array myList uses nested loops and a swap flag to indicate if any swaps occurred during a pass16. • Insertion sort: Data in a list is sorted by placing each item in turn in the correct position in a sorted list17. An insertion sort works well for incremental sorting or partially sorted lists17.... The pseudocode for an insertion sort algorithm on a 1D array myList involves iterating through the list, taking each element as a key, and inserting it into the correct position within the already sorted part of the list by shifting other elements19. • Performance: The performance of a sorting routine may depend on the initial order of the data and the number of data items20. An insertion sort performs better on partially sorted lists because it moves the element being checked to approximately the right place18. The bubble sort only swaps elements with their neighbours18. As the number of elements in a list increases, the time taken to sort the list increases, and the performance of the bubble sort deteriorates faster than the insertion sort18. Abstract Data Types (ADTs) An Abstract Data Type (ADT) is a collection of data and a set of operations on those data3.... Examples of ADTs include stacks, queues, linked lists, binary trees, graphs, and dictionaries3.... You should be able to describe the key features of a stack, queue, and linked list and justify their use for a given situation3. Essential operations when using an ADT include finding an item, adding a new item, and deleting an item6. • Stack: A list containing several items operating on the Last In, First Out (LIFO) principle4.... Items are always added at the end25. Stack operations are handled using statements like PUSH (add an item) and POP (remove an item)4.... Stacks are used in memory management, expression evaluation, and backtracking in recursion28. • Queue: A list containing several items operating on the First In, First Out (FIFO) principle4.... Items are always added at the end25. Queue operations include adding items (Enqueue) and removing items (Dequeue)29. Queues are used in managing files sent to a printer, keyboard buffers, and scheduling28. • Linked List: A list containing several items in which each item points to the next item in the list5.... Linked lists make use of pointers to manage operations25. Items can be removed from any position30. Linked lists can be implemented using two 1D arrays (one for items, one for pointers) and a set of pointers30. Algorithms can be written to find, insert, and delete items in a linked list20.... • Binary Tree: A hierarchical data structure in which each parent node can have a maximum of two child nodes33.... Each item is stored at a node35. Nodes can have left and right pointers36. Binary trees are used in syntax analysis, compression algorithms, and 3D video games33. Algorithms can be written to find or insert an item in a binary tree20.... • Graph: A non-linear data structure consisting of nodes and edges8.... Nodes or vertices are the fundamental units where edges converge40. Graphs can be used to aid Artificial Intelligence (AI)41. Examples of algorithms that use graphs include Dijkstra's algorithm and the A* algorithm, which find the shortest path between nodes40.... Graphs are examples of ADTs8.... You should be able to describe the key features of a graph and justify its use for a given situation, though you will not be required to write code for a graph structure21. • Dictionary: An ADT consisting of pairs of keys and values, where the key is used to find the value8.... Each key can only appear once, and keys are unordered43. The same value may appear more than once43. ADT Implementation ADTs are collections of data and operations3.... It is possible for ADTs to be implemented from other ADTs21.... ADTs can also be implemented from appropriate built-in types or other ADTs21. For example, a stack, queue, or linked list can be implemented using arrays3.... A dictionary can be implemented using a linked list as part of its definition43. In pseudocode, a dictionary type myDictionary could be declared using a linkedList type that contains an item (STRING) and pointer (INTEGER), along with an array for value (STRING)44. Algorithm Comparison Different algorithms which perform the same task can be compared by using criteria such as the time taken to complete the task and memory used21.... Big O notation is a mathematical notation used to describe the performance or complexity of an algorithm in relation to the time taken or the memory used8.... It describes the worst-case scenario45. • Big O order of time complexity: Describes how the time to perform a task grows as the number of data items (N) increases45.... ◦ O(1): Time is constant regardless of N (e.g., deciding if a number is even or odd)47. ◦ O(N): Time grows linearly with N (e.g., a linear search)47. ◦ O(N^2): Time grows linearly with the square of N (e.g., bubble sort, insertion sort)45.... ◦ O(Log N): Time grows linearly as N grows exponentially (e.g., binary search)45. • Big O order of space complexity: Describes how the memory (space) used grows as N increases48.... ◦ O(1): Space is constant regardless of N (e.g., any algorithm that just uses variables)48. ◦ O(N): Space grows linearly with N (e.g., any algorithm that uses arrays)49. keep_pinSave to note copy_all thumb_up thumb_down Create audio overview on Show understanding of linear and binary searching methods Show understanding of insertion sort and bubble sort methods Show understanding of and use Abstract Data Types (ADT) Show how it is possible for ADTs to be implemented from another ADT Show understanding that different algorithms which perform the same task can be compared by using criteria (e.g. time taken to complete the task and memory used) Back to contents page Notes and guidance Write an algorithm to implement a linear search Write an algorithm to implement a binary search The conditions necessary for the use of a binary search How the performance of a binary search varies according to the number of data items Write an algorithm to implement an insertion sort Write an algorithm to implement a bubble sort Performance of a sorting routine may depend on the initial order of the data and the number of data items Write algorithms to find an item in each of the following: linked list, binary tree Write algorithms to insert an item into each of the following: stack, queue, linked list, binary tree Write algorithms to delete an item from each of the following: stack, queue, linked list Show understanding that a graph is an example of an ADT. Describe the key features of a graph and justify its use for a given situation. Candidates will not be required to write code for a graph structure Describe the following ADTs and demonstrate how they can be implemented from appropriate built in types or other ADTs: stack, queue, linked list, dictionary, binary tree Including use of Big O notation to specify time and space complexity Okay, here is an audio overview covering the concepts of searching, sorting, Abstract Data Types, their implementation, algorithm comparison, and related algorithms, drawing upon the provided sources. (Begin Audio Overview) Welcome to this overview of fundamental computer science concepts: searching, sorting, Abstract Data Types, and algorithm analysis. We'll start with searching, the process of finding a specific item within a collection of data. The sources discuss two main methods: 1. Linear Search: In a linear search, each element of an array is checked in order, from the beginning to the end, until the desired item is found or the end of the array is reached1.... This method works on lists where items can be in any order1. The pseudocode involves iterating through the array, comparing each element to the item being sought1.... 2. Binary Search: A binary search is significantly more efficient, but it requires the list to be already sorted5. It works by checking the value of the middle item in the list5. If the middle item isn't the one needed, the half of the list that cannot contain the item is discarded5. This process is repeated on the remaining half until the item is found or there are no items left to check5. The pseudocode for a binary search involves calculating a middle index and adjusting the search boundaries (upper and lower bounds)6. Comparing performance, as the size of a list increases, the time taken for a linear search increases proportionally1. A binary search generally takes far fewer comparisons1. For a list of 1024 elements, a binary search might take a maximum of around 10-16 comparisons (since log₂1024 is 10, plus a few for implementation), whereas a linear search could take up to 1024 comparisons in the worst case1. The performance of a binary search varies logarithmically according to the number of data items7.... Next, sorting methods, used to arrange data in a meaningful order10. The sources cover two methods: 1. Bubble Sort: This method compares each element of an array with the next element10.... If they are in the wrong order, they are swapped10.... This comparison and swapping process is repeated through the list10. Each pass places the next largest (or smallest) unsorted element into its correct position10. This continues until a pass is completed with no swaps or only one element remains unsorted10.... Pseudocode for bubble sort uses nested loops and a flag to track swaps13.... 2. Insertion Sort: Data is sorted by taking each item in turn and placing it into its correct position within the portion of the list that is already sorted15.... It works well for lists that are incrementally sorted or partially sorted15.... Pseudocode for insertion sort involves an outer loop to pick the element to insert and an inner process (often a loop) to shift existing elements and find the correct place for the selected element18. The performance of a sorting routine depends on the initial order of the data and the number of data items8.... The insertion sort performs better on partially sorted lists compared to bubble sort20. As the number of elements increases, the time taken to sort also increases, and bubble sort's performance degrades faster than insertion sort20. Now, let's discuss Abstract Data Types, or ADTs21.... An ADT is defined as a collection of data and a set of operations on that data21.... Examples of ADTs mentioned include stacks, queues, linked lists, binary trees, graphs, and dictionaries2.... Essential operations for using an ADT are finding, adding (inserting), and deleting items23. Let's look at some specific ADTs and their implementation: • Stack: A stack is a list that operates on the Last In, First Out (LIFO) principle21.... Items are added and removed from the same end22. Operations include PUSH (add an item) and POP (remove an item)22. Stacks can be implemented using arrays21.... Algorithms are needed to insert into a stack and delete from a stack19.... • Queue: A queue is a list that operates on the First In, First Out (FIFO) principle3.... Items are added at one end (the 'rear') and removed from the other end (the 'front')22. Operations include Enqueue (add an item) and Dequeue (remove an item)22. Queues can also be implemented using arrays, often as a circular queue to manage space efficiently21.... Algorithms are needed to insert into a queue and delete from a queue19.... • Linked List: A linked list is a list where each item, or node, contains both data and a pointer to the next item in the list3.... This structure allows items to be removed from any position22. Linked lists can be implemented using two 1D arrays: one for the data items and another for the pointers31.... A set of pointers, like a startPointer and a heapStartPointer (for managing available space), are used31.... Algorithms are needed to find an item19..., insert an item19..., and delete an item from a linked list19.... • Binary Tree: A binary tree is a hierarchical data structure where each node, or parent, can have a maximum of two child nodes (a left child and a right child)12.... Each node stores an item34. Binary trees are often implemented using pointers to the left and right children35. Algorithms are needed to find an item in a binary tree19... and insert an item into a binary tree19.... Implementing binary trees in languages like Python, VB.NET, or Java often requires using objects and recursion38.... • Dictionary: A dictionary is an ADT consisting of key-value pairs2.... A key is used to find its corresponding value2.... Keys must be unique, but values can be duplicated2.... Keys in a dictionary are unordered2.... ADTs can be implemented from appropriate built-in types or other ADTs42. For instance, stacks, queues, and linked lists can be implemented using arrays21.... A dictionary can be declared using a linked list as part of its definition41.... Finally, let's look at comparing different algorithms that perform the same task. They can be compared using criteria such as the time taken to complete the task and the memory used42. Big O notation is a mathematical notation used to specify the performance or complexity of an algorithm2.... It describes the worst-case scenario for either time taken (time complexity) or memory used (space complexity) as the number of data items (N) increases44.... • Big O Time Complexity: Describes how the execution time scales with N9.... ◦ O(1): Constant time, independent of N (e.g., checking if a number is even or odd)9. ◦ O(N): Linear time, grows directly with N (e.g., linear search)9. ◦ O(N^2): Quadratic time, grows with the square of N (e.g., bubble sort, insertion sort)9.... ◦ O(Log N): Logarithmic time, grows slowly as N increases exponentially (e.g., binary search)45. ◦ O(2^N): Exponential time, grows very rapidly with N (e.g., calculating Fibonacci numbers recursively)45.... • Big O Space Complexity: Describes how the memory used scales with N46.... ◦ O(1): Constant space, independent of N (e.g., using a fixed number of variables)46. ◦ O(N): Linear space, grows directly with N (e.g., using arrays to store input data)48. Lastly, graphs are also examples of ADTs42. A graph is a non-linear data structure made up of nodes (or vertices) and edges connecting them2.... Nodes are the fundamental units where edges meet50. Graphs can be used to aid Artificial Intelligence (AI)51.... Their use is justified in situations requiring pathfinding or representing complex relationships, such as in shortest path algorithms like Dijkstra's or the A* algorithm50.... Candidates are not required to write code for graph structures42. This concludes the overview of these key data structures, algorithms, and their analysis methods based on the provided source material.
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )