Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.

Slides:



Advertisements
Similar presentations
1. List Static List: no adding or deleting Dynamic List: can add or delete items from the list Both static and dynamic lists: linear search, update item.
Advertisements

One Dimensional Arrays
CSE Lecture 3 – Algorithms I
1 Various Methods of Populating Arrays Randomly generated integers.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Searching and Sorting Linear Search Binary Search Selection Sort
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Sorting and Searching. Problem Read in a parameter value n, then read in a set of n numbers. Print the numbers in their original order. Sort the numbers.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
Sorting Algorithms: Selection, Insertion and Bubble.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Unit 271 Searching and Sorting Linear Search Binary Search Selection Sort Insertion Sort Bubble (or Exchange) Sort Exercises.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Searching and Sorting Arrays
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Chapter 14: Sorting and searching. Chapter Goals To study several sorting and searching algorithms To appreciate that algorithms for the same task can.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Applications of Arrays (Searching and Sorting) and Strings
Computer Science Searching & Sorting.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Lec 20 More Arrays--Algorithms. Agenda Array algorithms (section 7.5 in the book) – An algorithm is a well-defined specification for solving a problem.
Sorting. Algorithms Sorting reorders the elements in an array or list in either ascending or descending order. Sorting reorders the elements in an array.
Chapter 14: Searching and Sorting
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.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
CSCI 51 Introduction to Programming March 12, 2009.
Sorting Algorithms: Selection, Insertion and Bubble.
Lec 21 More Fun with Arrays: For Loops. Agenda Some backfill for Lab 20: – Using an array in an applet or class – array instance variables – using Math.random()
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Arrays, Methods, Error Handling. Using Arrays as Parameters int[] anArray = ; int index = min(anArray); // Index of smallest int index = search(anArray,
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
C Lecture Notes 1 Arrays (...cont.). C Lecture Notes 2 6.6Sorting Arrays Sorting data –Important computing application –Virtually every organization must.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Sorting and Searching. Searching  Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Week 9 - Wednesday.  What did we talk about last time?  2D arrays  Queen attacking pawn example  Started Game of Life.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
CSE 251 Dr. Charles B. Owen Programming in C1 Intro to Arrays Storing List of Data.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
Data Structures Arrays and Lists Part 2 More List Operations.
CSCI 51 Introduction to Programming March 10, 2009.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 4 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.
CS 116 Object Oriented Programming II Lecture 4 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Algorithm Definition An algorithm is a step-by-step solution to a problem.
Algorithm Definition An algorithm is a step-by-step solution to a problem.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. System.out.println(“Please enter grade.
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
Describing algorithms in pseudo code
Introduction to Programming
CISC181 Introduction to Computer Science Dr
Lec 21 More Fun with Arrays: For Loops
Presentation transcript:

Array Processing - 2

Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.

Swapping two elements in an array. Often have to swap the values in two cells in an array. The following code attempts to swap the third and fourth element in the array (remember arrays are 0-based). IT DOESN’T WORK properly. Why? What are the resulting values in the array? int[] anArray = { 24, 36, 64, 48 }; anArray[2] = anArray[3]; anArray[3] = anArray[2];

Demo – Swap two array elements. Demo ArraySwap.java

Linear Search for a Value. Commonly need to search arrays to see if they contain a value. Technique involves : Iterate from first through last element of the array. For each iteration, compare value at current index with searchValue. If it is equal you can stop your search otherwise continue. Stop searching at the end of the array.

Demo: Linear Search public class LinearSearch1 { public static void main() { boolean found = false; int searchValue = 36; int[] anArray = { 24, 36, 64, 48 }; for( int i = 0 ; i < anArray.length && found != true ; i++ ){ if( anArray[i] == searchValue ){ found = true; } System.out.println(“Our search for " + searchValue + “ returned “ + found ); }

Finding a high value. Commonly need to find the largest or smallest element in an array. For a high value search: Create a simple variable, highValue, to store our highest value. Set it to the first element of the array. Iterate from second through last element of the array. For each iteration, compare value at current index with stored highValue. If it is greater it is the new high value, so reset highValue.

Demo: Find High Value public class HighValueSearch { public static void main() { int highValue = 0; int[] anArray = { 24, 36, 64, 48 }; highValue = anArray[0]; for( int i = 1 ; i < anArray.length; i++ ){ if( anArray[i] > highValue ){ highValue = anArray[i]; } System.out.println("High Value is " + highValue ); }

Your Turn!

Task:SimpleStats.java We want to write some code to generate simple statistics from our temperature array. Declare and initialize an array with the values 21.2, 22.0, 24.5, 27.2, Write the code to detect the maximum value, the minimum value, the range (highest value minus lowest value) and the average temperature.

Passing and Returning Arrays. You will often want to pass an array to method or return an array from a method.

Array Parameters. you can use arrays as parameters. public static void printArray(double[] numbers) { for( double number: numbers ){ System.out.println( number); }....main double[] anArray = { 1.0,2.0,3.0 }; printArray( anArray );...end main.

Array Parameters. you can use arrays as parameters. public static int[] getMonthLengths() { int[] monthLengths = {31,28,31,30,31,30,31,31,30,31,30,31}; return monthLengths; }....main int[] anArray = getMonthLengths();...end main.

Your Turn Your previous in-class assignment was supposed to generate simple statistics (min,max,range,and mean). Now refactor this code this code so that the stats are calculate in a getStats method. getStats receives an array of double and returns an array of stats representing the min [0],max [1], range [2] and mean [3]. Use the method by calling it from main() and printing the output. public static double[] getStats( double[] temp );