ALGORITHMS TO STUDY FOR THE MIDTERM LINEAR SEARCH ALGORITHM i = 0; while((key != data[i]) && (i < n)) i++; BINARY SEARCH ALGORITHM Note: the array must be sorted first before using the binary search first = 0; last = n-1; found = false; while (first <= last) && (!found) { mid = (first + last)/2; if (key == data[mid]) found = true; if (key < data[mid]) last = mid-1; if (key > data[mid]) first = mid+1; } Q1: Complete the table below by writing the number of times the algorithm will be executed. Linear Search Binary Search Best case 1 1 Average case n/2 log2n/2 Worst case n log2n Note: n is the size of the array Q2: When the size of the array is small, which algorithm is better to use (linear or binary)? Why? Answer: For small array size, linear search algorithm is better because in linear algorithm, there s no need to sort the array and it will take few times of execution since it is small. Q3: When the size of the array is big, which algorithm is better to use (linear or binary)? Why? Answer: For large array, binary Search algorithm is better to use because it will take few times of execution compare to linear search. 1 SORTING ALGORITHMS BUBBLE SORT ALGORITHM INSERTION SORT ALGORITHM for (j = 0; j<n-1; j++) for(i=1; i<N; i++) { temp = array[i]; j = i-1; for (i=0; i<(n-1)-j ; i++) if (array[i] > array[i+1]) while (temp < array[j] && j>=0) { array[j+1] = array[j]; j = j-1; } { temp = array[i]; array[i] = array[i+1]; array[i+1] = temp; } array[j+1] = Temp; } Given the array below, answer the following questions: 4 10 1 8 5 Q1: If we are going t sort the array above using Bubble sort algorithm, how many comparisons will take place? Answer: ___4+3+2+1 = 10____ How many swap will happen? Answer: ____5__ note that swapping contains 3 operations, hence, the total number of operations is 5*3 = 15 Q2: If we are going to sort the array above using Insertion sort algorithm, how many comparisons will take place? Answer: _1+2+3+4 = 10_ How many times this statement (array[j+1] = array[j];) will be executed number of insertions? Answer: _____5___ Q3: Based on your answer in Q1 & Q2, which do you think is faster, Bubble algorithm or Insertion Sort algorithm? Answer: ____________ 2 FIBONACCI NUMBERS Below are two different C program of fibonacci numbers # include <stdio.h> # include <conio.h> # include <stdio.h> # include <conio.h> main () { // (1) creating an array, array name is fib, array size is 20 int fibonacci(int n); main () { int n; // number entered by the user int fn; // stores the fibonacci number int fib[20]; int n; // (2) storing values to array based on THE FIBONACCI ALGORITHM // ask the user to enter number n printf(" Enter a number n = "); scanf("%d", &n); for(n=0; n<20; n++) { if (n<2) fib[n] = n; else fib[n] = fib [n-1] + fib [n-2]; } // call the function fibonacci that contins the algorithm fn = fibonacci(n); // (4) printing the fibonacci number for the entered number // (3) ask the user to enter number between 0 & 19 printf("\n The fibonacci number for %d is %d", n, fn); getch(); return 0; printf(" Enter a number between 0 and 19: "); scanf("%d",&n); } // (4) printing the fibonacci number for the entered number // the FIBONACCI ALGORITHM printf("\n The fibonacci number for %d is %d", n, fib[n]); int fibonacci(int n) { if (n<2) return n; else return (fibonacci(n-1) + fibonacci(n-2)); } getch(); return 0; } Q1. Observe the differences of the two programs. Write all your observations below. Answer: Program 1 Used array Limited numbers Fibonacci numbers are stored in the array The array is accessed to get the output Program 2 Used functions Any value of n Fibonacci number are not pre-stored The algorithm needs to be executed to get the output. Use more space Use less space ___________________________________________________________________________________ 3 ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ Q2: Compare the two algorithms in terms of space and time (e.g. which one uses more space?). Answer: ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ ____________________________________________________________________________________ 4