seattle preparatory school advanced placement computer science seattle preparatory school advanced...

14
Seattle Preparatory School Advanced Placement Computer Science Seattle Preparatory School Advanced Placement Computer Science LESSON 62 FEBRUARY 12, 2015 Quiz Review and MBCS Ch 4 Consider this method: public int mystery(int a, int b) { return a * mystery(a, b-1); } 1. What does the method do? 2. The method has a bug in it – what is it? 3. How could you fix it?

Upload: thomasine-cross

Post on 24-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Seattle Preparatory School Advanced Placement Computer Science Seattle Preparatory School Advanced Placement Computer Science LESSON 62 FEBRUARY 12, 2015

Seattle Preparatory SchoolAdvanced Placement Computer ScienceSeattle Preparatory SchoolAdvanced Placement Computer Science

LESSON 62FEBRUARY 12, 2015

Quiz Review and MBCS Ch 4

Consider this method:

public int mystery(int a, int b) {return a * mystery(a, b-1);

}

1. What does the method do?2. The method has a bug in it – what is it?3. How could you fix it?

Page 2: Seattle Preparatory School Advanced Placement Computer Science Seattle Preparatory School Advanced Placement Computer Science LESSON 62 FEBRUARY 12, 2015

Seattle Preparatory SchoolAdvanced Placement Computer Science

Quiz Review

Page 3: Seattle Preparatory School Advanced Placement Computer Science Seattle Preparatory School Advanced Placement Computer Science LESSON 62 FEBRUARY 12, 2015

Seattle Preparatory SchoolAdvanced Placement Computer ScienceSeattle Preparatory SchoolAdvanced Placement Computer Science

1. Describe sequential search• It will examine each element in array A in order until X is

found. If it reaches the end of the array without finding X, it will return -1.

Page 4: Seattle Preparatory School Advanced Placement Computer Science Seattle Preparatory School Advanced Placement Computer Science LESSON 62 FEBRUARY 12, 2015

Seattle Preparatory SchoolAdvanced Placement Computer ScienceSeattle Preparatory SchoolAdvanced Placement Computer Science

2. What is the complexity of sequential search?• O(N)

• This is because if X is the last element in array A, then sequential search will have to examine N elements in an array A of size N.

Page 5: Seattle Preparatory School Advanced Placement Computer Science Seattle Preparatory School Advanced Placement Computer Science LESSON 62 FEBRUARY 12, 2015

Seattle Preparatory SchoolAdvanced Placement Computer ScienceSeattle Preparatory SchoolAdvanced Placement Computer Science

3. Describe binary search• It will divide the sorted array A in half, and check if X is

greater than or less than the midpoint to determine if it should take the right or left side. Once a side is selected, it will repeat the process with that side, until an array of length 1 is found. That position will contain X and be returned, or if it does not contain X, the search will return -1.

Page 6: Seattle Preparatory School Advanced Placement Computer Science Seattle Preparatory School Advanced Placement Computer Science LESSON 62 FEBRUARY 12, 2015

Seattle Preparatory SchoolAdvanced Placement Computer ScienceSeattle Preparatory SchoolAdvanced Placement Computer Science

4. What is the complexity of binary search?• O(logN)

• This is because the array of length N is split in half repeatedly until there us an array of length 1.

• Remember that big O logs are base 2.

Page 7: Seattle Preparatory School Advanced Placement Computer Science Seattle Preparatory School Advanced Placement Computer Science LESSON 62 FEBRUARY 12, 2015

Seattle Preparatory SchoolAdvanced Placement Computer ScienceSeattle Preparatory SchoolAdvanced Placement Computer Science

5. Name three sorting algorithms• You could have named three of any of the following:• Bubble

• Insertion

• Radix

• Selection

• Merge

• Quick

Page 8: Seattle Preparatory School Advanced Placement Computer Science Seattle Preparatory School Advanced Placement Computer Science LESSON 62 FEBRUARY 12, 2015

Seattle Preparatory SchoolAdvanced Placement Computer ScienceSeattle Preparatory SchoolAdvanced Placement Computer Science

6. What is the complexity of selection sort?• O(N2)

• This is because selection sort scans the unsorted part of the array of length N once to find the next element for each element in the array.

Page 9: Seattle Preparatory School Advanced Placement Computer Science Seattle Preparatory School Advanced Placement Computer Science LESSON 62 FEBRUARY 12, 2015

Seattle Preparatory SchoolAdvanced Placement Computer ScienceSeattle Preparatory SchoolAdvanced Placement Computer Science

7. Suppose we have a program that takes 100 + N/10 milliseconds to process an array of size N. What is the complexity of the program?

• O(N)

• When using big O notation, we remove constant factors (1/10) and reduce summations to the term with the largest growth rate (N as 100 never changes).

Page 10: Seattle Preparatory School Advanced Placement Computer Science Seattle Preparatory School Advanced Placement Computer Science LESSON 62 FEBRUARY 12, 2015

Seattle Preparatory SchoolAdvanced Placement Computer ScienceSeattle Preparatory SchoolAdvanced Placement Computer Science8. How long can one expect the

algorithm to take on an array of size 2000?• 4 seconds

• Because a quadratic equation is exponential, if you double the base by two, it will case the time to increase by that to the power of 2.

Page 11: Seattle Preparatory School Advanced Placement Computer Science Seattle Preparatory School Advanced Placement Computer Science LESSON 62 FEBRUARY 12, 2015

Seattle Preparatory SchoolAdvanced Placement Computer ScienceSeattle Preparatory SchoolAdvanced Placement Computer Science

9. What is the complexity of finding the largest element? Why?• O(n) because you have no way of knowing where the

largest element is in the array, so worse case scenario you have to check each element in the array once.

Page 12: Seattle Preparatory School Advanced Placement Computer Science Seattle Preparatory School Advanced Placement Computer Science LESSON 62 FEBRUARY 12, 2015

Seattle Preparatory SchoolAdvanced Placement Computer ScienceSeattle Preparatory SchoolAdvanced Placement Computer Science

10. What is the complexity of finding the largest element? Why?• O(1) because if the array is sorted, then you already

know that the smallest is at one end and the largest is at the other.

Page 13: Seattle Preparatory School Advanced Placement Computer Science Seattle Preparatory School Advanced Placement Computer Science LESSON 62 FEBRUARY 12, 2015

Seattle Preparatory SchoolAdvanced Placement Computer ScienceSeattle Preparatory SchoolAdvanced Placement Computer Science

Extra Creditpublic static in findNumberOfDuplicates(int[] a, int x) {

int count = 0;

for(int position = 0; position < a.length; position++) {int currentValue = a[position];if(currentValue == x) {

count++;}

}

return count;

}

Page 14: Seattle Preparatory School Advanced Placement Computer Science Seattle Preparatory School Advanced Placement Computer Science LESSON 62 FEBRUARY 12, 2015

Seattle Preparatory SchoolAdvanced Placement Computer ScienceSeattle Preparatory SchoolAdvanced Placement Computer Science

MBCS Chapter 4 – Different types of Fish• Due Thursday

• Quiz a week from Tuesday

• Four more DAT files on Haiku

• When it asks you to save a file, please make sure you turn that in with your answers. When it asks you to write code, please make sure to turn in that code as well as any analysis you do with the code.