Warmup Which of the 4 sorting algorithms uses recursion?

Slides:



Advertisements
Similar presentations
COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
Advertisements

Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Chapter 9: Searching, Sorting, and Algorithm Analysis
CS0007: Introduction to Computer Programming Array Algorithms.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Efficiency of Algorithms February 19th. Today Binary search –Algorithm and analysis Order-of-magnitude analysis of algorithm efficiency –Review Sorting.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Efficiency of Algorithms Csci 107 Lecture 8. Last time –Data cleanup algorithms and analysis –  (1),  (n),  (n 2 ) Today –Binary search and analysis.
CHAPTER 11 Searching. 2 Introduction Searching is the process of finding a target element among a group of items (the search pool), or determining that.
Searching Chapter 18 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Searching – Linear and Binary Searches. Comparing Algorithms Should we use Program 1 or Program 2? Is Program 1 “fast”? “Fast enough”? P1P2.
Chapter 16: Searching, Sorting, and the vector Type.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Computer Science Searching & Sorting.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Efficiency of Algorithms Csci 107 Lecture 7. Last time –Data cleanup algorithms and analysis –  (1),  (n),  (n 2 ) Today –Binary search and analysis.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Chapter 9 Searching and Sorting
Sorting and Searching Pepper. Common Collection and Array Actions Sort in a certain order ◦ Max ◦ Min Shuffle Search ◦ Sequential (contains) ◦ Binary.
CSC 211 Data Structures Lecture 13
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Chapter 16: Searching, Sorting, and the vector Type.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Objectives You should be able to describe: One-Dimensional Arrays
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Searching Arrays Linear search Binary search small arrays
Chapter 16: Searching, Sorting, and the vector Type
16 Searching and Sorting.
19 Searching and Sorting.
Searching and Sorting Algorithms
Agenda Warmup Lesson 4.5 (Program Design & Analysis)
Top 50 Data Structures Interview Questions
Searching – Linear and Binary Searches
Warmup What is an abstract class?
Introduction to Algorithms
Chapter 13: Searching and Sorting
Teach A level Computing: Algorithms and Data Structures
Sorting Data are arranged according to their values.
Building Java Programs
Adapted from Pearson Education, Inc.
Lecture 14: binary search and complexity reading:
Algorithm design and Analysis
MSIS 670 Object-Oriented Software Engineering
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh)
Sorting Data are arranged according to their values.
Lecture 15: binary search reading:
Agenda Warmup Lesson 4.5 (Program Design & Analysis)
Searching: linear & binary
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
24 Searching and Sorting.
Building Java Programs
Topic 24 sorting and searching arrays
Given value and sorted array, find index.
Chapter 4.
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Applications of Arrays
Presentation transcript:

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.

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”

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(); }

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.

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

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

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.

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