Chapter 6: Arrays Java Software Solutions for AP* Computer Science

Slides:



Advertisements
Similar presentations
Chapter 10: Data Structures II
Advertisements

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) Sorting a list of test.
Visual C++ Programming: Concepts and Projects
Lecture 6 b Last time: array declaration and instantiationarray declaration and instantiation array referencearray reference bounds checkingbounds checking.
Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Algorithm An algorithm is a step-by-step set of operations to be performed. Real-life example: a recipe Computer science example: determining the mode.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
8. Arrays 8.1 Using Arrays 8.2 Reference versus Value Again 8.3 Passing Array Arguments and Returning Arrays 8.4 An Example: Simulating Rolls of the Dice.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CHAPTER 11 Sorting.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
Chapter Day 22. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 22 Problem set 4 Posted  10 problems from chapters 7 & 8  Due Nov 21 (right.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Algorithm Efficiency and Sorting
Chapter 9 Polymorphism 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
C++ for Engineers and Scientists Third Edition
Chapter 6: Arrays Java Software Solutions Third Edition
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Aalborg Media Lab 15-Jul-15 Polymorphism Lecture 12 Chapter 9.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Do Now Take out ch6 test answers – be ready to hand it in Pick a leader in each group of up to 3 students; Leader will retrieve a whiteboard, marker, and.
Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java Software.
Chapter 9 Searching and Sorting
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
1 Sorting b Sorting is the process of arranging a list of items into a particular order b There must be some value on which the order is based b There.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
ArrayList is a class that implements the List interface. The List interface is a blueprint for its “implementor” classes. There is another implementor.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
CSCI 51 Introduction to Programming March 10, 2009.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
Searching and Sorting Searching algorithms with simple arrays
Chapter 9: Sorting and Searching Arrays
Chapter 6: Arrays Java Software Solutions
Chapter 5: Enhancing Classes
Chapter 9 Polymorphism.
Chapter 8: Recursion Java Software Solutions
Chapter 10: Non-linear Data Structures
Sorts.
Chapter 3: Program Statements
Data Structures and Algorithms
Chapter 8: Recursion Java Software Solutions
Outline Late Binding Polymorphism via Inheritance
Chapter 8: Recursion Java Software Solutions
Sorting and Searching -- Introduction
Arrays.
Presentation transcript:

Chapter 6: Arrays Java Software Solutions for AP* Computer Science Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Modified by Margery Waldron Java Software Solutions is published by Addison-Wesley Presentation slides are copyright 2002 by John Lewis, William Loftus, and Cara Cocking. All rights reserved. Instructors using the textbook may use and modify these slides for pedagogical purposes. *AP is a registered trademark of The College Entrance Examination Board which was not involved in the production of, and does not endorse, this product.

Arrays Arrays are objects that help us organize large amounts of information This lesson will focus on: array declaration and use passing arrays and array elements as parameters arrays of objects searching an array sorting elements in an array

Each value has a numeric index Arrays An array is an ordered list of values scores The entire array has a single name Each value has a numeric index 0 1 2 3 4 5 6 7 8 9 79 87 94 82 67 98 87 81 74 91 An array of size N is indexed from zero to N-1 This array holds 10 values that are indexed from 0 to 9

Declaring Arrays Some examples of array declarations: double[] prices = new double[500]; boolean[] flags; flags = new boolean[20]; char[] codes = new char[1750];

String[] words = new String[25]; Arrays of Objects The elements of an array can be object references The following declaration reserves space to store 25 references to String objects String[] words = new String[25]; It does NOT create the String objects themselves Each object stored in an array must be instantiated separately See GradeRange.java (page 310)

Arrays of Objects Objects can have arrays as instance variables Many useful structures can be created with arrays and objects The software designer must determine carefully an organization of data and objects that makes sense for the situation See Tunes Project (page 312)

Searching A common task when working with arrays is to search an array for a particular element A linear or sequential search examines each element of the array in turn until the desired element is found A binary search is more efficient than a linear search but it can only be performed on an ordered list A binary search examines the middle element and moves left if the desired element is less than the middle, and right if the desired element is greater This process repeats until the desired element is found See Searches.java (page 319)

Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) sorting a list of test scores in ascending numeric order sorting a list of people alphabetically by last name There are many algorithms for sorting a list of items These algorithms vary in efficiency We will examine two specific algorithms: Selection Sort Insertion Sort

Selection Sort The approach of Selection Sort: In more detail: select a value and put it in its final place into the list repeat for all other values In more detail: find the smallest value in the list switch it with the value in the first position find the next smallest value in the list switch it with the value in the second position repeat until all values are in their proper places

Selection Sort An example: See SortGrades (page 323) original: 3 9 6 1 2 smallest is 1: 1 9 6 3 2 smallest is 2: 1 2 6 3 9 smallest is 3: 1 2 3 6 9 smallest is 6: 1 2 3 6 9 See SortGrades (page 323)

Swapping Swapping is the process of exchanging two values Swapping requires three assignment statements temp = first; first = second; second = temp;

Insertion Sort The approach of Insertion Sort: In more detail: pick any item and insert it into its proper place in a sorted sublist repeat until all items have been inserted In more detail: consider the first item to be a sorted sublist (of one item) insert the second item into the sorted sublist, shifting the first item as needed to make room to insert the new addition insert the third item into the sorted sublist (of two items), shifting items as necessary repeat until all values are inserted into their proper positions

Insertion Sort An example: original: 3 9 6 1 2 insert 9: 3 9 6 1 2 insert 6: 3 6 9 1 2 insert 1: 1 3 6 9 2 insert 2: 1 2 3 6 9 See SortGrades2 (page 324) -- the insertionSort method

Sorting Objects Integers have an inherent order, but the ordering criteria of a collection of objects must be defined Recall that a Java interface can be used as a type name and guarantees that a particular class implements particular methods We can use the Comparable interface and the compareTo method to develop a generic sort for a set of objects See SortPhoneLists (page 328)

Comparing Sorts Time efficiency refers to how long it takes an algorithm to run Space efficiency refers to the amount of space an algorithm uses Algorithms are compared to each other by expressing their efficiency in big-oh notation An efficiency of O(n) is better than O(n2), where n refers to the size of the input Time efficiency O(2n) means that as the size of the input increases, the running time increases exponentially

Comparing Sorts Both Selection and Insertion sorts are similar in efficiency They both have outer loops that scan all elements, and inner loops that compare the value of the outer loop with almost all values in the list Approximately n2 number of comparisons are made to sort a list of size n We therefore say that these sorts have efficiency O(n2), or are of order n2 Other sorts are more efficient: O(n log2 n)