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.

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

CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
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.
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.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Sorting1 Sorting Order in the court!. sorting2 Importance of sorting Sorting a list of values is a fundamental task of computers - this task is one of.
1 (c) elsaddik CSI 1102 Introduction to Software Design Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
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 6: Arrays & More GUI Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002, and used.
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.
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.
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
Aalborg Media Lab 15-Jul-15 Polymorphism Lecture 12 Chapter 9.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
CHP - 9 File Structures. INTRODUCTION In some of the previous chapters, we have discussed representations of and operations on data structures. These.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
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.
Outline ArrayList Searching Sorting Copyright © 2012 Pearson Education, Inc.
Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java Software.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
1 Advanced Polymorphism Polymorphism Review Comparator Interface Sorting with Comparators Selection Sort Insertion Sort.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Chapter 7 Arrays 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
1 Objects for Organizing Data -- Introduction zAs our programs get more sophisticated, we need assistance organizing large amounts of data zChapter 6 focuses.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
Chapter 6: Arrays and Vectors. 2 Arrays and Vectors  Arrays and vectors are objects that help us organize large amounts of information  Chapter 6 focuses.
Chapter 6: Arrays Java Software Solutions
Chapter 9 Polymorphism.
Two-Dimensional Arrays
Sorts.
Data Structures and Algorithms
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Outline Late Binding Polymorphism via Inheritance
The Vector Class An object of class Vector is similar to an array in that it stores multiple values However, a vector only stores objects does not have.
Chapter 6: Arrays and Vectors
Sorting and Searching -- Introduction
Arrays.
Presentation transcript:

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 are many algorithms for sorting a list of items b These algorithms vary in efficiency b We will examine two specific algorithms: Selection SortSelection Sort Insertion SortInsertion Sort

2 Selection Sort b The approach of Selection Sort: select one value and put it in its final place in the sort listselect one value and put it in its final place in the sort list repeat for all other valuesrepeat for all other values b In more detail: find the smallest value in the listfind the smallest value in the list switch it with the value in the first positionswitch it with the value in the first position find the next smallest value in the listfind the next smallest value in the list switch it with the value in the second positionswitch it with the value in the second position repeat until all values are placedrepeat until all values are placed

3 Selection Sort b An example: original: original: smallest is 1: smallest is 1: smallest is 2: smallest is 2: smallest is 3: smallest is 3: smallest is 6: smallest is 6:  See SortGrades.java (page 289) SortGrades.java  See Sorts.java (page 290) -- the selectionSort method Sorts.java Sorts.java

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

5 Insertion Sort b An example: original: original: insert 9: insert 9: insert 6: insert 6: insert 1: insert 1: insert 2: insert 2:  See Sorts.java (page 290) -- the insertionSort method Sorts.java

6 Sorting Objects b Integers have an inherent order, but the order of a set of objects must be defined by the person defining the class b Recall that a Java interface can be used as a type name and guarantees that a particular class has implemented particular methods  We can use the Comparable interface to develop a generic sort for a set of objects  See SortPhoneList.java (page 294) SortPhoneList.java  See Contact.java (page 295) Contact.java  See Sorts.java (page 290) Sorts.java

7 Comparing Sorts b Both Selection and Insertion sorts are similar in efficiency b The 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 b Therefore approximately n 2 number of comparisons are made to sort a list of size n b We therefore say that these sorts are of order n 2 b Other sorts are more efficient: order n log 2 n

8 Two-Dimensional Arrays b A one-dimensional array stores a simple list of values b A two-dimensional array can be thought of as a table of values, with rows and columns b A two-dimensional array element is referenced using two index values b To be precise, a two-dimensional array in Java is an array of arrays  See TwoDArray.java (page 299) TwoDArray.java

9 Multidimensional Arrays b An array can have as many dimensions as needed, creating a multidimensional array b Each dimension subdivides the previous one into the specified number of elements  Each array dimension has its own length constant b Because each dimension is an array of array references, the arrays within one dimension could be of different lengths

10 The Vector Class  An object of class Vector is similar to an array in that it stores multiple values b However, a vector only stores objectsonly stores objects does not have the indexing syntax that arrays havedoes not have the indexing syntax that arrays have  The methods of the Vector class are used to interact with the elements of a vector  The Vector class is part of the java.util package  See Beatles.java (page 304) Beatles.java Beatles.java

11 The Vector Class b An important difference between an array and a vector is that a vector can be thought of as a dynamic, able to change its size as needed b Each vector initially has a certain amount of memory space reserved for storing elements b If an element is added that doesn't fit in the existing space, more room is automatically acquired

12 The Vector Class  The Vector class is implemented using an array b Whenever new space is required, a new, larger array is created, and the values are copied from the original to the new array b To insert an element, existing elements are first copied, one by one, to another position in the array  Therefore, the implementation of Vector in the API is not very efficient for inserting elements

Polygons and Polylines b Arrays are often helpful in graphics processing b Polygons and polylines are shapes that are defined by values stored in arrays b A polyline is similar to a polygon except that its endpoints do not meet, and it cannot be filled  See Rocket.java (page 307) Rocket.java  There is also a separate Polygon class that can be used to define and draw a polygon

Saving Drawing State  Each time the repaint method is called on an applet, the window is cleared prior to calling paint b An array or vector can be used to store the objects drawn, and redraw them as necessary  See Dots2.java (page 310) Dots2.java Dots2.java