Chapter 11 Arrays Continued

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

CSE Lecture 3 – Algorithms I
Chapter 10 Introduction to Arrays
Visual C++ Programming: Concepts and Projects
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
HST 952 Computing for Biomedical Scientists Lecture 9.
Arrays.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11: Sorting and Searching  Searching Linear Binary  Sorting.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Chapter 9 Introduction to Arrays
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Chapter 9: Advanced Array Concepts
Chapter 16: Searching, Sorting, and the vector Type.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 2 ARRAYS.
Array Processing.
111 © 2002, Cisco Systems, Inc. All rights reserved.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Chapter 8: Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Sorting Chapter 10. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
Chapter 9 (modified) Abstract Data Types and Algorithms Nell Dale John Lewis.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009.
Chapter 12 Binary Search and QuickSort Fundamentals of Java.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CSCI 51 Introduction to Programming March 12, 2009.
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.
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
SORTING Chapter 8 CS Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 16: Searching, Sorting, and the vector Type.
Java Programming: Guided Learning with Early Objects Chapter 8 Applications of Arrays (Sorting and Searching) and Strings.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Sorts, CompareTo Method and Strings
Chapter 16: Searching, Sorting, and the vector Type
Sorting Mr. Jacobs.
Sixth Lecture ArrayList Abstract Class and Interface
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 8 Arrays Objectives
Can store many of the same kind of data together
Chapter 8 Arrays Objectives
Can store many of the same kind of data together
Principles of Computing – UFCFA3-30-1
Sorting Chapter 8.
Can store many of the same kind of data together
Chapter 8 Arrays Objectives
Principles of Computing – UFCFA3-30-1
Module 8 – Searching & Sorting Algorithms
Arrays.
Presentation transcript:

Chapter 11 Arrays Continued Fundamentals of Java

Objectives Use string methods appropriately. Write a method for searching an array. Understand why a sorted array can be searched more efficiently than an unsorted array. Write a method to sort an array. Fundamentals of Java

Objectives (cont.) Write methods to perform insertions and removals at given positions in an array. Understand the issues involved when working with arrays of objects. Perform simple operations with Java’s ArrayList class. Fundamentals of Java

Vocabulary Array list Binary search Bubble sort Immutable object Insertion sort Fundamentals of Java

Vocabulary (cont.) Linear search Selection sort Substring Wrapper class Fundamentals of Java

Advanced Operations on Strings Most text-processing applications examine and manipulate the characters in strings. Separating strings into segments Searching for/replacing specific characters or substrings Inserting text into a string String objects are immutable. No mutators in the String class Fundamentals of Java

Advanced Operations on Strings (cont.) Table 11-1: Some commonly used String methods Fundamentals of Java

Advanced Operations on Strings (cont.) Table 11-1: Some commonly used String methods (cont.) Fundamentals of Java

Advanced Operations on Strings (cont.) Table 11-1: Some commonly used String methods (cont.) Fundamentals of Java

Advanced Operations on Strings (cont.) Example 11.2: Count the words and compute the average word length in a sentence. Fundamentals of Java

Advanced Operations on Strings (cont.) Example 11.2: Count the words and compute the average word length in a sentence (cont.). Fundamentals of Java

Searching Linear search: Search a data structure (such as an array) from beginning to end Searching an array of objects: Fundamentals of Java

Searching (cont.) Binary search: An efficient search algorithm based on eliminating half of the data from the search at each iteration Data must be sorted first. Examine midpoint of data, then decide which half of the data to continue searching on. Discard other half of data. Fundamentals of Java

Searching (cont.) Binary search code: Fundamentals of Java

Figure 11-1: Trace of a binary search of an array Searching (cont.) Figure 11-1: Trace of a binary search of an array Fundamentals of Java

Table 11-2: Behavior of the method compareTo Searching (cont.) To compare objects, best if the class implements the Comparable interface compareTo method Table 11-2: Behavior of the method compareTo Fundamentals of Java

Searching (cont.) Binary search for objects: Fundamentals of Java

Searching (cont.) Implementing a Comparable class example: Fundamentals of Java

Figure 11-2: Array before and after sorting Arranging the elements of a collection of data (such as an array) in an ordered fashion Figure 11-2: Array before and after sorting Fundamentals of Java

Sorting: Selection Sort Basic idea: Table 11-3: Trace of data during a selection sort Fundamentals of Java

Sorting: Selection Sort (cont.) Must be able to find smallest number in an array and swap items in an array Fundamentals of Java

Table 11-4: Trace of data during one pass of a bubble sort Sorting: Bubble Sort Pass through array comparing adjacent elements If out of order, swap. Table 11-4: Trace of data during one pass of a bubble sort Fundamentals of Java

Sorting: Bubble Sort (cont.) Pseudocode: Fewer data exchanges than selection sort Sort can stop early if array already sorted Fundamentals of Java

Sorting: Insertion Sort After kth pass of sorting loop (k starting at 1), first k items should be in sorted order. Table 11-4: Trace of data during an insertion sort Fundamentals of Java

Sorting: Insertion Sort (cont.) Pseduocode: Fundamentals of Java

Sorting (cont.) Any of the search algorithms can be altered to support sorting of objects. Object’s class(es) should implement Comparable Have compareTo method Example: Fundamentals of Java

Insertions and Removals Steps for insertion: 1. Check for available space. 2. Check validity of target index. Between 0 and logical size 3. Shift items from logical end of array to target index down by one position. 4. Assign new item to cell at target index. 5. Increment logical size by one. Fundamentals of Java

Insertions and Removals (cont.) Figure 11-3: Inserting an item into an array Fundamentals of Java

Insertions and Removals (cont.) Steps for removal: 1. Check validity of target index. Between 0 and logical size 2. Shift items from target index to logical end of array up by one position. 3. Decrement logical size by one. Fundamentals of Java

Insertions and Removals (cont.) Figure 11-4: Removing an item from an array Fundamentals of Java

Working with Arrays of Objects When array type is an interface type, abstract class, or superclass of 1+ other classes, array may contain different object types. Might not all respond to common set of messages Fundamentals of Java

Working with Arrays of Objects (cont.) What if you want to perform an operation specific to one of the types in the array? Can use the instanceOf operator to determine the specific type of element in the array Most general arrays have type Object. Can hold any type of object Fundamentals of Java

The Class java.util.ArrayList Contains sequence of elements ordered by position Unlike an array in that: It uses methods rather than [] to manipulate elements. It tracks the logical size and physical size. The logical size is 0 when created. Size automatically adjusted as needed The positions available for access range from 0 to the logical size minus 1. Fundamentals of Java

The Class java.util.ArrayList (cont.) Generic array list: Programmer must specify element type for the list Raw array list: Can contain objects of any reference type Declaring/instantiating a generic array list: Fundamentals of Java

The Class java.util.ArrayList (cont.) Table 11-6: Some commonly used ArrayList methods Fundamentals of Java

The Class java.util.ArrayList (cont.) ArrayList objects cannot directly store primitive types. Must use wrapper classes Classes that contain the value of a primitive type Boolean, Integer, Double, Character Fundamentals of Java

The Class java.util.ArrayList (cont.) ArrayList objects automatically “box” and “unbox” primitive values when used with ArrayList methods. Fundamentals of Java

The Class java.util.ArrayList (cont.) Advantages of ArrayList over arrays: Includes many methods for tasks such as insertions, removals, and searches Tracks own logical size and grows or shrinks automatically with the number of elements contained in it Fundamentals of Java

Graphics and GUIs: Menus A drop-down menu system consists of a menu bar, a number of menus, and several selections for each menu. May have sub-menus Menu item object for each menu selection (class JMenuItem) Menu object for each menu (class JMenu) Menu bar object in which all of the menu objects will appear (class JMenuBar) Fundamentals of Java

Graphics and GUIs: Menus (cont.) Listener objects are attached to menus. When menu items are selected, events are fired and the listener objects respond. Figure 11-6: New user interface for the student test scores program Fundamentals of Java

Graphics and GUIs: Menus (cont.) Example 11.6: TestScoresView class (with menus) Fundamentals of Java

Graphics and GUIs: Menus (Cont.) Example 11.6: TestScoresView class (with menus, cont.) Fundamentals of Java

Summary Linear search: Simple search that works well for small- and medium-sized arrays Binary search: Clever search that works well for large arrays but assumes that the elements are sorted Comparisons of objects are accomplished by implementing the Comparable interface, which requires the compareTo method. Fundamentals of Java

Summary (cont.) Selection sort, bubble sort, and insertion sort are simple sort methods that work well for small- and medium-sized arrays. Insertions and removals of elements at arbitrary positions are complex operations that require careful design and implementation. Fundamentals of Java

Summary (cont.) One can insert objects of any class into an array of Object. When retrieved from the array, objects must be cast down to their classes before sending them most messages. The limitation of a fixed-size array can be overcome by using Java’s ArrayList class. Fundamentals of Java

Summary (cont.) An array list tracks and updates its logical size and provides many useful client methods. Wrapper class, such as Integer, provides a way of packaging a value of a primitive type, such as int, in an object so that it can be stored in an array of Object or an array list. Fundamentals of Java