Presentation is loading. Please wait.

Presentation is loading. Please wait.

Warmup Which of the 4 sorting algorithms uses recursion?

Similar presentations


Presentation on theme: "Warmup Which of the 4 sorting algorithms uses recursion?"— Presentation transcript:

1 Warmup Which of the 4 sorting algorithms uses recursion?
Which algorithm uses a pivot value? What is Big-O Notation? What is an abstract class? How is an abstract class different from an interface? Can an interface have constants? What does the this keyword refer to? In a class diagram, describe the arrow that represents interface implementation.

2

3

4 In an ArrayList, the set( ) method returns the element that was PREVIOUSLY at the specified index. Examples: // assume ArrayList words = [“the”,“big”,“dog”] System.out.print(words.set(2, “cat”)); // prints “dog” System.out.print(words.get(2)); // prints “cat” String z = words.set(0, “my”); // z equals “the” String y = words.get(0); // y equals “my”

5 Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based on an IS-A relationship, composition is based on a HAS-A relationship. For example, a car is-a vehicle, and a car has-an engine: public class Car extends Vehicle { private Engine x = new Engine(); }

6 Composition is represented in a class diagram (also known as a UML diagram) by a down-arrow or side-arrow with a solid arrowhead (or a diamond): Car Transmission Engine Vehicle A car is-a vehicle. A car has-a transmission, and an engine.

7 Search algorithms We have learned the 4 most common algorithms for sorting an array. Searching an array means looking for an item in an array, and if it is found, determining the index at which it was found. There are 2 main algorithms for searching: Binary Search Sequential Search

8 Binary Search Note: you must begin with a sorted array.
The Binary Search algorithm uses these steps: Find the middle index Compare the element located there to the thing you are searching for (the “target”) If the target is larger, then ignore the lower half of the array, and find the middle index of the larger half If smaller, ignore the larger half, find middle index of smaller half. Repeat until you find (or do not find) the target Demo: BinarySearchDemo

9

10 Sequential Search The Sequential Search algorithm is much simpler, but less efficient than Binary Search. This algorithm simply looks at each element in the array, starting at the first element, until it finds the target.

11 null When you create an Object, such as a String, you must initialize it. If you do not, then its value is null. Then, if you attempt to display it, or call a method on that Object, such as finding the length of a String, you will get an error – either a compiler error, or a runtime error that is called NullPointerException. Sometimes, when you do not yet know the value of an Object, you deliberately set it to null, in order to avoid compiler errors. This is called a null reference. You can check for null references using if statements. demo: NullDemo

12

13

14

15


Download ppt "Warmup Which of the 4 sorting algorithms uses recursion?"

Similar presentations


Ads by Google