Download presentation
Presentation is loading. Please wait.
Published byVeroljub Dolenc Modified over 5 years ago
1
Java Coding 6 – part2 David Davenport Computer Eng. Dept.,
Collections – partially-filled arrays, searching & sorting David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. 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.)
2
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 saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David.
3
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
4
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
5
Searching & Sorting
6
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)
7
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)
8
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
9
From array to ArrayList…
What else?
10
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!
11
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
12
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.
13
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
14
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) ); }
15
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] ); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.