Java Coding 6 – part2 David Davenport Computer Eng. Dept.,

Slides:



Advertisements
Similar presentations
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Example - The.
Advertisements

Java Coding OOP David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Towards Event-driven programming &
Everyday Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to.
Introduction to UML David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …Unified Modeling Language.
Chapter 7 – Arrays.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Java Coding 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Decisions, decisions…!
Java Coding 3 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
Review: OOP & Arrays David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …from CS101.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey.
Java Coding 4 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Method madness.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Java Coding 3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Over & over again!
For each primitive type there is a wrapper class for storing values of that type: Double d = new Double(29.95); Wrapper Classes Wrapper objects can be.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces +
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.
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
CSE 1201 Object Oriented Programming ArrayList 1.
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session.
Chapter 9 Introduction to Arrays Fundamentals of Java.
CS102 – Recursion David Davenport Bilkent University Ankara – Turkey
Java Coding 6 David Davenport Computer Eng. Dept.,
CMSC 202 ArrayList Aug 9, 2007.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Lecture 10 Collections Richard Gesick.
Week 13: Searching and Sorting
Java Coding – part 2 David Davenport Computer Eng. Dept.,
CSC 222: Object-Oriented Programming
Recitation 13 Searching and Sorting.
Java Coding 3 – part2 David Davenport Computer Eng. Dept.,
Java Coding 3 David Davenport Computer Eng. Dept.,
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept.,
Repetition-Counter control Loop
Objects First with Java CITS1001
CSS161: Fundamentals of Computing
Algorithm design and Analysis
CMSC 202 ArrayList Aug 9, 2007.
Object Oriented Programming in java
Java Coding 6-extra David Davenport Computer Eng. Dept.,
Parameter Passing in Java
CMSC 202 ArrayList Aug 9, 2007.
Collections Framework
Arrays in Java.
Java Coding 8 David Davenport Computer Eng. Dept.,
Data Structures & Algorithms
Java Coding 4 (part2) David Davenport Computer Eng. Dept.,
Amortized Analysis and Heaps Intro
CSE 143 Lecture 2 ArrayIntList, binary search
Java Coding 6_part3 David Davenport Computer Eng. Dept.,
CS101 - Algorithms & Programming I
Building Java Programs
Java Coding 6 David Davenport Computer Eng. Dept.,
Arrays.
Java Coding 6 David Davenport Computer Eng. Dept.,
Week 7 - Monday CS 121.
Selamat Datang di “Programming Essentials in Python”
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept.,
Presentation transcript:

Java Coding 6 – part2 David Davenport Computer Eng. Dept., Collections – partially-filled arrays, searching & sorting David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Last update: 22/12/2016 ~ added slides to show expand/reduce array size! Previously: 16/12/2012 ~ (added misc slides…) Previous update: 8/12/2012 (split JavaCoding6 into two parts, this is the second part.)

IMPORTANT… Students… Instructors… This presentation is designed to be used in class as part of a guided discovery sequence. It is not self-explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an email saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David.

Using part of an array (1) Array size specified & fixed at instantiation Problem if required size is unknown? Solution make big enough for worst-case & use part of it Must divide array into two sets, in-use & not in-use … but how? ? Other possible solutions if only positive values in data set then store –1 in each unused element or, define class to hold data & store objects in array, empty elements will be null or, have two parallel arrays, one for data the other for in-use flag. Note: properties of each of these solutions varies (for insert/delete/search/etc.) 3 6 5 1 10 2 4 grades -5 7 in-use not in-use One simple & common solution

Using part of an array (2) Store elements sequentially from element zero Keep count of number of in-use elements (valid) or grades.length 8 maxEls 3 6 5 1 10 2 4 grades ? 7 in-use not in-use Now, write add, addAt(i), remove(i), size, & init Can now solve read set of values until sentinel problem… do it? Form into IntBag class (equivalent to ArrayList… except only int’s & doesn’t expand… do it too?) 5 valid Now process only valid elements not maxEls

Searching & Sorting

Sequential search O(n) Searching Search the first n elements of x for a target value & return its location if found, else -1 public static int search(int n, int[] x, int target) { int pos = 0; while ( pos < n && x[pos] != target) pos++; if ( x[pos] == target) return pos; // found at pos else return –1; // not found } Good problem to work out together in class before showing solution above note the solution here relies on short circuit boolean evaluation – solve without this too. Note that it is incorrect – can they see why/when it might fail? The if ( x[pos] == target) fails if n = x.length & could return the wrong answer in other cases if the target is the element at x[n] it should be if ( pos != n ) Contrast efficiency of sequential search with binary search (as provided by Java Collections framework.) Sequential search O(n)

Sorting Selection sort 5 9 1 7 3 4 5 4 1 7 3 9 5 4 1 3 7 9 3 4 1 5 7 9 Explain selection sort Note efficiency is actual this –1 (since last step is unnecesaary) & +k(n-1) for the swaps Contrast efficiency with that of mergesort provided by Collections framework which is O(log n). n=6 n=5 n=4 n=3 n=2 n=1 + Sum = n.( n +1) / 2 = n2/2 + n/2 O( n2)

Selection Sort To sort first n elements of array X 9 9 1 7 3 5 3 9 while n > 1 find location of max value in first n elements of X swap element at location of max with n’th element decrement n tmp 9 Note that n’th element of X is actually X[n-1] because indexes start from zero. Find location of max is left as exercise! Other O( N2) sorts include: bubble sort & insertion sort. swap( int i, int j) { int tmp; tmp = X[i]; X[i} = X[j]: X[j] = tmp; } 9 1 7 3 5 2 4 X 3 9

From array to ArrayList… What else?

Expanding the array Create bigger array & copy! 9 1 7 3 5 ? 9 1 7 3 5 2 4 Emphasize this is an expensive operation… - do not do for each item added to collection! Can reduce size in a similar way. ArrayList is array of Object type. tmp ? 1 2 3 4 6 7 5 9 1 7 3 5 expensive!

Misc Notations, variable length param. lists, parallel arrays… ? Also include other misc. but useful bits ‘n bobs? For example, - int i = Integer.parseInt( aString); // where for example, aString = “254”; - String s = i + “”; - int code = aChar; // gives integer ASCII/UNICODE code of aChar - int digit = aChar – ‘0’; // assume… ! Misc

Two notations… Primitive types Object types int i, j, k; k = 5; j = k; if ( j == k ) if ( j > k ) Sys… ( i); OType o, p, q; q = null; p = q; o = p.add( q); p.add( q); if ( p == q ) if ( p.equals(q) ) if ( p.compareTo(q) > 0 ) Sys… ( o.toString() ); Object types generally use a different form of notation… e.g. Rather than “c = a + b;” you would have “c = a.add(b);” As an exercise, do problem such as factorial in both forms.

Notations – array & object Access any part of any structure groups 1 2 3 4 groups[1][0].getName() David 100 Use square bracket notation to access element of array structure dot notation to access element (property) of object structure Why have different notations to access parts of a structure? music groups[0][1] Queen 1977 1 2 3 groups[1][0].music[3].releaseDate

Variable length param. lists Methods with variable number of parameters Syntax: type… variable “…” is ellipsis public static int sum( int… values) { sum = 0; for ( int i = 0; i < values.length; i++) sum = sum + values[i]; return sum; } values is an array of specified type Note: can only have one variable length param type per method and it must be the last param, e.g. sumOver( int limit, int… values); Notice that the main method’s “String[] args” parameter functions in much the same fashion! public static void main( String[] args) { Sys… ( sum( 1, 2, 3, 4, 5) ); Sys… ( sum( 0) ); Sys… ( sum( 1, 2, 3) ); }

Parallel Arrays An alternative to arrays of objects… … name people 1 2 3 4 name 200 350 … 100 id_num false true isTeaDrinker David Gunes Derya 1 2 3 4 people David 100 true Gunes 200 false Derya 350 … Particularly useful in languages that do not have object types (or equivalent). for ( int i = 0; i < people.length; i++ { if ( people[i].isTeaDrinker ) Sys… ( people[i].name ); } for ( int i = 0; i < name.length; i++ { if ( isTeaDrinker[i] ) System.out.println( name[i] ); }