Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.

Similar presentations


Presentation on theme: "Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3."— Presentation transcript:

1 Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3

2 Table of Contents Strings – 3 Searching – 5 –Binary Search – 6 Sorts – 7 Insertions and Removals – 11 Arrays of Objects – 12 ArrayList – 13 Wrappers – 16 Review Questions – 17

3 Strings There are many predefined methods for strings that allow programmers to manipulate strings more easily. A table of these functions can be found on page 317 of the textbook. **There are no mutator functions for strings because they are immutable objects**

4

5 Searching Sometimes you need to find a target element in a collection. Searching is the general term for this. Two main searches: –Linear Search Simple, examines elements in order until target element is found or collection is exhausted –Binary Search More Complicated but more efficient

6 Binary Search Unlike linear search, binary search uses the divide and conquer approach which is more efficient. Each time the middle term is examined to see if it is the target term. If it is not the collection is split into two halves and the appropriate half is examined in the same way. Binary search requires an ordered list and elements that implement the comparable interface.

7 Sorts In order to use binary search, the collection must be sorted. This process of placing items stored in a linear collection in order is called sorting. There are several sorting algorithms that you must know for the midterm and AP test.

8 Selection Sort Basically find the smallest value and replace it with the first element and then continue doing the same thing with the rest of the elements. Really easy to write the code for, but inefficient (think about how you sort things in real life) Code can be found on pg 325

9 Bubble Sort Bubble sort works by swapping two adjacent elements if they are out of order with respect to each other. This causes the last element to bubble to the end of the array after every pass. If a pass doesn't interchange any items, the array must be sorted, which allows for the partial ordering of the array to reduce the number of passes.

10 Insertion Sort During each pass the kth element is inserted into the correct spot in the previously sorted elements. This is like how most people sort their hand when playing cards. This sort takes k passes to sort an array of k items and takes advantage of partial ordering of the array.

11 Insertions and Removals Arrays have a fixed size, but a variable logical size. You must compare the sizes before trying to insert or remove. When inserting shift the last item one further. And then do the same for the previous spot until you get to the target index. When removing move the next element one back and then repeat with the next element until the last element is reached.

12 Arrays of Objects An array can contain objects that aren't all of the exact same type. When using an array of objects you must cast the objects to their type before using class specific methods. Using the instanceof method allows you to check what type of object is in the array.

13 ArrayList Arrays have a fixed size which poses many problems to computer programmers To combat this the ArrayList class was made which basically makes an array with a dynamic size. The ArrayList class is found in java.util.ArrayList

14 ArrayList Continued The ArrayList also automatically shifts elements when adding or removing elements using the add and remove methods. For a complete list of ArrayList methods check page 353 of the textbook.

15 Sample Code

16 Wrapper Class ArrayList only works with objects not primitive types like int. An easy work around for this is to use wrapper classes. These classes function like their corresponding primitive data type, but are objects and immutable. For a list of wrapper classes and their unwrapping methods check page 356.

17 Multiple Choice Question 1 Identify the following code:

18 a) Selection Sort b) Insertion Sort c) Bubble Sort d) Binary Sort e) None of the Above Answer Choices

19 And the Answer is….!!! a) Selection Sort b) Insertion Sort c) Bubble Sort d) Binary Sort e) None of the Above The code finds the smallest value and then puts it in the front of the array. It then repeats this process for the rest of the elements. This is how selection sort works.

20 Question Number 2 If you performed an Insertion sort and outputted the numbers after each change what would the output look like with the following input: 7 3 18 2 13? a.2 3 18 7 13b. 3 7 18 2 13c. 3 7 18 2 13 2 3 7 18 132 3 7 18 133 7 2 18 13 2 3 7 13 182 3 7 13 183 7 2 13 18 3 2 7 13 18 d. 18 3 7 2 13e. None of these2 3 7 13 18 13 3 7 2 18 2 3 7 13 18

21 If you performed an Insertion sort and outputted the numbers after each change what would the output look like with the following input: 7 3 18 2 13? a.2 3 18 7 13b. 3 7 18 2 13c. 3 7 18 2 13 2 3 7 18 132 3 7 18 133 7 2 18 13 2 3 7 13 182 3 7 13 183 7 2 13 18 3 2 7 13 18 d. 18 3 7 2 13e. None of these2 3 7 13 18 13 3 7 2 18Explanation: Insertion sort places 2 3 7 13 18the Kth term in the right spot within the first k terms. B does this. And the Answer is…!

22 Code Writing When working with arrays it is sometimes useful to have structures capable of dynamic resizing. Write the code for the constructor, add method, and remove method for this class called dynamicArray. You can assume that all elements will be of type integer. The add method should allow you to insert an element at any index or after the last index. This should change the size of the array and shift the elements. The remove method should be capable of removing an element at a certain index, and also resize and shift the array. The constructer should create dynamicArray based on an integer array that is passed in.


Download ppt "Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3."

Similar presentations


Ads by Google