ALGORITHM DESIGN & DATA STRUCTURES ALGORITHMS • An algorithm is a set of steps that can be carried out to perform a task. • It can be defined as a set of step by step procedures to be executed in order to solve a problem. Algorithms are not necessarily written in any specific language and can be illustrated using the following: a. Descriptions b. Flowcharts c. Pseudocodes d. Structure diagrams. Advantages of algorithms They are not biased towards any programming language. Easy to convert to a program code or flowchart. Easy to determine logic errors. Has finite steps which lead to a solution. Disadvantages Time consuming to design, i.e. first convert to flowchart, then to program code . Most people find them difficult to learn. Properties of an algorithm 1. Efficiency – a good algorithm should produce results in a reasonable amount of time. 2. Unambiguous – the instructions in an algorithm should be clear and have only one meaning. 3. Multiplicity – an algorithm should be represented in many different ways, e.g. in pseudo code or flowchart. 4. Finiteness – an algorithm should terminate or end or stop after performing a task. 5. An algorithm should have a specified range of input. 6. Feasibility – an algorithm should be feasible with the available resources. 7. Independence – an algorithm should have instructions which are independent of any programming language. Analysis of algorithms Factors to consider when analysing algorithms are: 1. Time complexity – is the amount of time taken by the algorithm to run. 2. Space complexity – is the amount of space/memory taken by the algorithm and this helps us to estimate whether the algorithm requires more or less space. 3. Simplicity – this determines whether the sequence of steps are easy to understand. Simple algorithms lead to simple programs , and simple programs are easy to debug. 4. Range of input – input should be most natural to the corresponding problem. a. Descriptions: These are general statements that are followed in order to complete a specific task. They are not governed by any programming language. An example is as follows: Enter the length of the rectangle. Store length. Enter the width of the rectangle. Store width. Calculate area of rectangle. Store area of rectangle. Display area End program. b. Pseudocodes: These are English-like statements, closer to programming language that indicate steps followed in performing a specific task. They are however independent of any programming language. An example is as follows: Enter centigrade temperature, C If C = 0, then stop. Set F to 32 + (9C/5) Print C and F End Control Structures in Pseudocode A Control Structures is a code portion that specifies flow of control in programs. It basically analyses and chooses in which direction a program flows based on certain parameters or conditions. Control structures can be (1) sequence control structures,(2) conditional control structures or (3) iterative control structures. A number of control structures are used in designing Pseudocodes. These includes: 1. simple sequence 2. selection and 3. iteration (looping/repetition). (1)Simple sequence: This is whereby instructions are executed in the order they appear in a program without jumping any one of them up to the end of the program. Statements are executed one after another in the order they are. It is simple and avoids confusion. Example: Enter first number, A Enter second number, B C=A+B Print C Stop Statement 1 Simple sequence Statement 2 Statement 3 Statement n (2) Selection Structure: This allows one to choose the route to follow in order to accomplish a specific task. Selection is written using the IF ....THEN...ELSE statement or the CASE statement. Question: Using the IF…THEN…ELSE write an algorithm which compares two numbers and shows which one is greater. Selection structure False Statement(s) Condition? True Statement(s) (a) IF… THEN… ELSE STRUCTURE Is a two way selection structure, which allows a computer to choose one path for execution out of two available options. The IF… THEN… ELSE structure can accommodate more than two options through nesting or cascading( placing one if statement inside another). The general structure of the IF…THEN…ELSE statement/structure is as below: IF< condition> THEN statement(s) ELSE statement(s) NB: Statements after the key word THEN are executed only if the condition is TRUE, and those after the ELSE keyword are executed only if the condition is FALSE. (b) CASE STRUCTURE Is a multi-way selection structure, where the computer must choose one option to execute amongst many available options. The general structure/syntax of the CASE statement/structure is as below: CASE 1: <statement(s)> CASE 2: <statement(s)> CASE 3: <statement(s)> …. CASE ELSE <statement(s)> NB: statements after the CASE ELSE are only executed if none of the cases above it are true. Write an algorithm for a program that accepts a mark scored by a learner in computer science and grades it as follows: 70-100: A 60-69: B 50-59: C 45-49: D 40-44: E 0-39: 39 3 Repetition/Iteration/Looping: A control structure that repeatedly executes part of a program or the whole program until a certain condition is satisfied. Iteration is in the following forms: FOR...NEXT loop, REPEAT... UNTIL Loop and the WHILE...ENDWHILE Loop. Iteration structure FTru Condition ? Statement(s) a. For...Next Loop A looping structure that repeatedly executes the loop body for a specified number of times. The general syntax of the For...Next loop is as follows: A group of statements between the looping structures is called the loop body and is the one that is repeatedly executed. The For...Next loop is appropriate when the number of repetitions is known well in advance, e.g. five times. An example of a program that uses the For...Next loop is as follows: b. Repeat...Until Structure This is a looping structure that repeatedly executes the loop body when the condition set is FALSE until it becomes TRUE. The number of repetitions may not be known in advance and the loop body is executed at least once. The general syntax is as follows: Question: Using the repeat until structure, write an algorithm for a program that calculates the average of five numbers entered through the keyboard. c. While ... End while loop A looping structure in which the loop body is repeatedly executed when the condition set is TRUE until it becomes FALSE. It is used when the number of repetitions is not known in advance. The condition set is tested first before execution of the loop body. Therefore the loop body may not be executed at all if the condition set is FALSE from the start. The syntax of the WHILE…END WHILE structure is as follows: question Write an algorithm using the while loop that calculates the average of 10 numbers. Differences between the Repeat...Until and the While…ENDWHILE structures 4 Flowcharts It is a diagram used to give details on how programs and procedures are executed. Flowcharts are drawn using specific symbols, each with its own meaning, as given below: question Write an algorithm to add two numbers and display the sum using a flowchart. Use of the Pre-defined Symbol and the connector This is used when drawing flowcharts of subprograms as given below: d. Structure Diagrams These are diagrams that show relationships between different modules as given below: Interpreting and Testing Programs Dry running (desk checking): the process of manually testing the logic of a program on paper before coding on the computer. Dry running is done to determine the logic of a program (to check if it gives intended results.) Debugging: The process of finding and correcting errors in a program. Bugs are errors in a program. A debugger is a program used in aiding the finding and removal of errors in a program. Programming Errors i. Programming errors are grouped into: Syntax error: this is an error of violating the grammatical rules governing sentence construction in a certain programming language, for example, leaving a semi-colon at the end of each line in Pascal. Syntax errors are detected by the computer. A program cannot run with syntax errors. ii. Logic error (Semantic error): refers to an error in the sequencing of instructions, modules and specifying wrong formulae that will produce undesirable results. For example, instructing the computer to display result before any processing has been done. Logic errors cannot be detected by the computer. The user just finds wrong and unintended results of a process iii. Runtime (execution) error: These are errors that occur during program execution and can be generated when the computer tries to read past an end of file marker or by dividing a number by zero. (iv) Arithmetic error: occurs in an instruction which performs inappropriate arithmetic, e.g. Dividing a number by zero is an arithmetic error. (v) Type Mismatch error A type Mismatch error occurs in a program when a variable has been declared as one data type, but it is later assigned a value that is of an incompatible data type. The following code will produce a ‘Type Mismatch’ error because “Stella” is not an integer: Dim Firstname As integer Firstname= “Stella” Sorting algorithms Is an algorithm that is used to arrange a given list or array of elements into a certain order, i.e. ascending or descending order. There are many sorting algorithms which include: 1. Bubble sort/sinking sort 2. Quick sort 3. Bucket sort 4. Heap sort 5. insertion sort 6. Radix sort 7. Selection sort 8. Merge sort. (i) Bubble sort algorithm Is an algorithm that works by comparing adjacent pairs of elements, and if they are out of order, they are swapped. The process is repeated for all the remaining elements until the whole array is sorted. It is named bubble sort to describe the way larger elements “bubble” to their rightful positions in the sorted list. Bubble sort algorithm 1. Compare the first two elements in the list, if they are out of order, swap them. 2. Compare the second element with its neighbour on the right, if they are out of order, swap them. 3. Compare the third element with its neighbour on the right, if they are out of order, swap them. 4. Repeat this process throughout the list until all the elements have been sorted. - the comparison of elements is called a pass. example Use bubble sort algorithm to arrange the following elements in ascending order: 7 2 8 5 4 (ii) Quick sort Is a highly efficient algorithm and is based on partitioning the array of elements/data into smaller sub arrays. A large array is partitioned into two sub arrays, one of which holds values smaller than the specified value called the pivot and the other which holds values greater than the pivot. The quick sort algorithm is quite efficient for a large data set. It is a divide and conquer algorithm. Steps in quick sort 1. pick the right most element as the pivot. 2. Partition the array using the pivot value. 3. Quick sort the left sub array recursively. 4. Quick sort the right sub array recursively. Searching algorithms These are algorithms that are used to locate a certain item form a list of elements. The element being searched for may be in the list or may not be in the list. A search can be successful or unsuccessful, a successful search is when a search item is found in a list, and an unsuccessful search is when an element is not found after going through the whole list. There are principally many searching algorithms, but we will look at linear search and binary search. Linear search algorithm A linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched. If the item is found, the index where the element is located is returned. If it does not find the element after searching the whole array, then the index -1 is returned, which means element does not exist since -1 is an invalid index. NB: Linear search works on both sorted or unsorted arrays of elements. Steps in linear search (algorithm) 1. check if the search item is equal to the first element in the list. 2. If it is a match, end the search and return the index in which search item is found. 3. If it is not a match, check if the next item in the list matches the search item, if it matches, end search and return the index, otherwise continue until end of list, and if element is not found after searching all the elements, end the search and return -1. Binary search binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the array. Binary search is implemented using following steps... • Step 1 - Read the search element from the user. • Step 2 - Find the middle element in the sorted list. • Step 3 - Compare the search element with the middle element in the sorted list. • Step 4 - If both are matched, then display "Given element is found!!!" and terminate the function. • Step 5 - If both are not matched, then check whether the search element is smaller or larger than the middle element. • Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5 for the left sub list of the middle element. • Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5 for the right sub list of the middle element. • Step 8 - Repeat the same process until we find the search element in the list or until sub list contains only one element. • Step 9 - If that element also doesn't match with the search element, then display "Element is not found in the list!!!" and terminate the search.