1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Introduction to Java 2 Programming Lecture 5 Array and Collections.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
1 Various Methods of Populating Arrays Randomly generated integers.
Basic Algorithms on Arrays. Learning Objectives Arrays are useful for storing data in a linear structure We learn how to process data stored in an array.
Sorting. “Sorting” When we just say “sorting,” we mean in ascending order (smallest to largest) The algorithms are trivial to modify if we want to sort.
1 Selection Sort and Quick Sort Instructor: Mainak Chaudhuri
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Simple Sorting Algorithms
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 2D Arrays, Sorting Lecture 16, Tue Mar
Quicksort.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Lecture 14: A Programming Example: Sorting Yoni Fridman 7/24/01 7/24/01.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 2D Arrays, Sorting Lecture 23, Fri Mar
BUILDING JAVA PROGRAMS CHAPTER 7 Array Algorithms.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Yet More Array Practice Lecture 24, Mon Mar
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.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Chapter 8 ARRAYS Continued
Part 2. Searching Arrays Looking for a specific element in an array E.g., whether a certain score (85) is in a list of scores Linear search Binary search.
Week 2 - Monday.  What did we talk about last time?  Exceptions  Threads  OOP  Interfaces.
Java Arrays [Lists] in 10 Minutes. Declaring an array (and its types) int[] myIntArray; double[] myDoubleArray; String[] myStrings; //What’s the pattern?
Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
Arrays Art &Technology, 3rd Semester Aalborg University Programming David Meredith
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Sorting. Algorithms Sorting reorders the elements in an array or list in either ascending or descending order. Sorting reorders the elements in an array.
Enhanced For Loops (For Each Loops) Less Code and Less Overhead for Writing For Loops.
CSI1390 – Java Programming Methods II Instructor: Saeid Nourian
CS 162 Intro to Programming II Bubble Sort 1. Compare adjacent elements. If the first is greater than the second, swap them. Do this for each pair of.
Sorting Algorithms: Selection, Insertion and Bubble.
***** SWTJC STEM ***** Chapter 7 cg 50 Arrays as Parameters Regular variables are passed to a method by value; i.e., the variable value is copied to a.
Output Programs These slides will present a variety of small programs. Each program has some type of array that was introduced in this chapter. Our concern.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
1 Insertion sort [ ] i=1 j=1 i=2 j=2 insert i=3 at j=1 [ ] i=3 j=1 insert i=4 at j=0 [
Week 14 - Monday.  What did we talk about last time?  Heaps  Priority queues  Heapsort.
Chapter 10 Arrays. Learning Java through Alice © Daly and Wrigley Objectives Declare and use arrays in programs. Access array elements within an array.
CS 162 Intro to Programming II Insertion Sort 1. Assume the initial sequence a[0] a[1] … a[k] is already sorted k = 0 when the algorithm starts Insert.
1 Arrays and Methods Java always passes arguments by value – that is a copy of the value is made in the called method and this is modified in the method.
COP 3540 Data Structures with OOP
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays days until the AP Computer Science test.
Data Structures Arrays and Lists Part 2 More List Operations.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
Arrays and Sorting. Process Open a file that contains integers, one per line. Read each line, convert to short and store each into an array Sort the array.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
Array Review Selection Sort Get out your notes.. Learning Objectives Be able to dry run programs that use arrays Be able to dry run programs that use.
Week 13: Searching and Sorting
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Recitation 13 Searching and Sorting.
Quicksort 1.
Selection Sort Find the smallest value in the array. Put it in location zero. Find the second smallest value in the array and put it in location 1. Find.
Selection sort Given an array of length n,
Building Java Programs
2 code samples int [] array; int i, j, temp; for(i = 0; i < array.length/2; i++) { j = array.length-1-i; temp = array[i]; array[i] = array[j]; array[j]
Building Java Programs
Building Java Programs
Simple Sorting Algorithms
Presentation transcript:

1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS

2 OBJECTIVES! Create a method that traverses an array Reverse an array

3 PRINTING AN ARRAY… If the values are in an array, however, Java provides a much easier way… what is it? (This is actually from the very end of 7.1.) int[] myArray = {8, 42, 13, 77, 8, 16}; System.out.println(Arrays.toString(myArray));

4 ARRAY TRAVERSAL We’ve already discussed using for and for-each loops to walk through the values in an array. As you do this you can test each element and/or perform an action on that element. We traversed a list of word lengths and counted the number of them greater than the average length. (At least, that was the goal!)

5 ARRAY TRAVERSAL When traversing an array using a for loop (not a for-each loop!), you can modify the array by changing the values stored at a given index. How would you create a method to double all of the elements in an int array (int[])? public static void doubleIt(int[] array) { for (int i = 0; i < array.length; i++) { array[i] *= 2; // or array[i] = array[i] * 2; } }

6 SWAPPING There are several algorithms that can take advantage of swapping two elements in an array. What are they? Reversing and Sorting both require swapping elements. How would you create a method to swap any two elements (given their indexes) in a double array (double[])? public static void swap(double[] array, int i, int j) { double temp = array[i]; array[i] = array[j]; array[j] = temp; }

7 SWAPPING / REVERSING Now that we have a helper method to swap any two element values, we can write methods to do more complex changes. How would you create a method to reverse the elements in a double array (double[])? public static void reverse(double[] array) { for (int i = 0; i < array.length / 2; i++) { int j = array.length – 1 – i; swap(array, i, j); } }

8 LET’S TRY IT! Change the average word length program to 1. Print the complete array of words using Arrays.toString(). 2. Create a new array that contains just the long words. 3. Print the array of long words. 4. Write a method to reverse a String array (String[]) in place. 5. Reverse the complete and long word arrays and print them out. This sounds like a lot, but it’s really not, and we just went over everything you need for reversing!

9 LET’S TRY IT! Before you start… what approach would you use? How many words? 5 Word #1? ice Word #2? cream Word #3? social Word #4? sprinkles Word #5? chocolate Average word length: 6.4 Number of words longer than average: 2 All Words : [ice, cream, social, chocolate, sprinkles] Long Words: [chocolate, sprinkles] Reversed Words: [sprinkles, chocolate, social, cream, ice] Reversed Long : [sprinkles, chocolate]

10 WHAT WE COVERED You should be able to create a method that properly traverses an array You should be able reverse an array