Upload
darrell-hamilton
View
215
Download
0
Embed Size (px)
Citation preview
The ArrayList Class• An ArrayList object stores a list of objects
• An ArrayList object grows and shrinks as needed, adjusting its capacity as necessary
• The ArrayList class is part of the java.util package
• You can reference each object in the list using a numeric index just like the arrays
Copyright © 2012 Pearson Education, Inc.
The ArrayList Class• Index values of an ArrayList begin at 0 (not 1):
0 "Bashful"1 "Sleepy"2 "Happy"3 "Dopey"4 "Doc"
• Elements can be inserted and removed
• The indices of the elements adjust accordingly
Copyright © 2012 Pearson Education, Inc.
The ArrayList Class
Copyright © 2012 Pearson Education, Inc.
ArrayList<String> cityList = new ArrayList<String>();
//Add some cities in the listcityList.add("London");// cityList now contains [London]cityList.add("Denver");// cityList now contains [London, Denver]cityList.add("Paris");// cityList now contains [London, Denver, Paris]cityList.add("Ankara");// cityList now contains [London, Denver, Paris, Ankara]
ArrayList Methods• ArrayList() Creates an empty list.
• add(o: Object): booleanAppends a new element o at the end of this list.
• add(index: int, o: Object): void Adds a new element o at the specified index in this list.
• clear(): void Removes all the elements from this list.
Copyright © 2012 Pearson Education, Inc.
ArrayList Methods• contains(o: Object): boolean Returns true if this list contains the element o.
• get(index: int): Object Returns the element from this list at the specified index.
• indexOf(o: Object): int Returns the index of the first matching element in this list.
• isEmpty(): boolean Returns true if this list contains no elements.
Copyright © 2012 Pearson Education, Inc.
ArrayList Methods• lastIndexOf(o: Object): int Returns the index of the last matching element in this list.
• remove(o: Object): boolean removes the element o from this list.
• size(): int returns the number of elements in this list.
• remove(index: int): boolean removes the element at the specified index.
• set(index: int, o: Object): Object sets the element at the specified index.
Copyright © 2012 Pearson Education, Inc.
The ArrayList Class• The type of object stored in the list is established
when the ArrayList object is created:
ArrayList<String> names = new ArrayList<String>();
ArrayList<Book> list = new ArrayList<Book>();
• An ArrayList object cannot store primitive types, but wrapper classes are used instead.
• See Beatles.java
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
//********************************************************************// Beatles.java Author: Lewis/Loftus//// Demonstrates the use of a ArrayList object.//********************************************************************
import java.util.ArrayList;
public class Beatles{ //----------------------------------------------------------------- // Stores and modifies a list of band members. //----------------------------------------------------------------- public static void main (String[] args) { ArrayList<String> band = new ArrayList<String>();
band.add ("Paul"); band.add ("Pete"); band.add ("John"); band.add ("George");
continue
Copyright © 2012 Pearson Education, Inc.
continue
System.out.println (band); int location = band.indexOf ("Pete"); band.remove (location);
System.out.println (band); System.out.println ("At index 1: " + band.get(1)); band.add (2, "Ringo");
System.out.println ("Size of the band: " + band.size()); int index = 0; while (index < band.size()) { System.out.println (band.get(index)); index++; } }}
Copyright © 2012 Pearson Education, Inc.
continue
System.out.println (band); int location = band.indexOf ("Pete"); band.remove (location);
System.out.println (band); System.out.println ("At index 1: " + band.get(1)); band.add (2, "Ringo");
System.out.println ("Size of the band: " + band.size()); int index = 0; while (index < band.size()) { System.out.println (band.get(index)); index++; } }}
Output[Paul, Pete, John, George][Paul, John, George]At index 1: JohnSize of the band: 4PaulJohnRingoGeorge
Examples• See ReverseFile.java
• See CountVowels.java
• See CountWords.java
Copyright © 2012 Pearson Education, Inc.
Linear Search• A linear search begins at one end of a list and examines
each element in turn
• Eventually, either the item is found or the end of the list is encountered
Copyright © 2012 Pearson Education, Inc.
Linear Search• ArrayList implementation: LinearSearch2.java• Array implementation: LinearSearch.java
Copyright © 2012 Pearson Education, Inc.
Binary Search Algorithm• A binary search first examines the middle element
of the list -- if it matches the target, the search is over
• If it doesn't, only one half of the remaining elements need be searched
• Since they are sorted, the target can only be in one half of the other
Copyright © 2012 Pearson Education, Inc.
Binary Search• The process continues by comparing the middle element of
the remaining viable candidates
• Each comparison eliminates approximately half of the remaining data
• Eventually, the target is found or the data is exhausted
Copyright © 2012 Pearson Education, Inc.
Binary Search• Given a key and sorted array a[], find index i
such that a[i] = key, or report that no such index exists.
Binary Search• Array implementation: BinarySearch2.java• ArrayList implementation: BinarySearch.java
Copyright © 2012 Pearson Education, Inc.
Problem size• In many applications, it is easy to come up with a numeric
value that specifies the problem size, which is generally denoted by the letter N.
• For most array applications, the problem size is simply the size of the array as indicated by the length constant.
• For most ArrayList applications, the problem size is simply the size of the ArrayList as indicated by the size() method.
Copyright © 2012 Pearson Education, Inc.
Time complexity• A measure of the amount of time required to execute an
algorithm.
• Time complexity expresses the relationship between the size of the problem and the run time for the algorithm
• Time complexity analysis is independent of the programming language chosen to implement the algorithm and the speed of the computer on which the algorithm is executed
Copyright © 2012 Pearson Education, Inc.
Time complexity• Analysis is usually based on:
– Number of arithmetic operations performed– Number of comparisons made– Number of times through a critical loop– Number of array elements accessed– … etc
Copyright © 2012 Pearson Education, Inc.
Efficiency of Linear Search• In the worst case—which occurs when the value you’re
searching for comes at the end of the array or does not appear at all—linear search requires N steps. On average, it takes approximately half that.
Copyright © 2012 Pearson Education, Inc.
O (N)
Efficiency of Binary Search• On each step in the process, the binary search algorithm
rules out half of the remaining possibilities. • In the worst case, the number of steps required is equal to
the number of times you can divide the original size of the array in half until there is only one element remaining. In other words, what you need to find is the value of k that satisfies the following equation:
1 = N / 2 / 2 / 2 / 2 . . . / 2k times
• You can simplify this formula using basic mathematics:
1 = N / 2k
2k = N
k = log2 N
Copyright © 2012 Pearson Education, Inc.
O (log N)
Detailed Outline
Sorting
Selection Sort
Insertion Sort
Bubble Sort
Copyright © 2012 Pearson Education, Inc.
Sorting• Sorting data is one of the most important computing
applications
• Sorting is the process of arranging a list of items in a particular order
9 2 4 5 8 1 3
After sorting in ascending order:
1 2 3 4 5 8 9
Copyright © 2012 Pearson Education, Inc.
Sorting• There are many algorithms, which vary in efficiency, for
sorting a list of items
• We will examine three specific algorithms, which are all quadratic in time: – Selection Sort– Insertion Sort– Bubble Sort
Copyright © 2012 Pearson Education, Inc.
Selection Sort• The strategy of Selection Sort:
– select a value and put it in its final place in the list– repeat for all other values
• In more detail:
– find the smallest value in the list– switch it with the value in the first position– find the next smallest value in the list– switch it with the value in the second position– repeat until all values are in their proper places
Copyright © 2012 Pearson Education, Inc.
Selection Sort Implementations• See SelectionSortDesc.java• See SelectionSortStr.java• Sorting an array of String objects containing Turkish
letters: ı ö ü ç ğ ş SelectionSortStrTR.java
Copyright © 2012 Pearson Education, Inc.
Insertion Sort• Pick any item and insert it into its proper place in a sorted
sublist– repeat until all items have been inserted
• In more detail:– consider the first item to be a sorted sublist (of one item)– insert the second item into the sorted sublist, shifting the
first item as needed to make room to insert the new one– insert the third item into the sorted sublist (of two items),
shifting items as necessary– repeat until all values are inserted into their proper
positions
Copyright © 2012 Pearson Education, Inc.
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
46
4 5 7 211 19 12 20Value 17 14
Iteration 0
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
47
4 5 7 211 19 12 20Value 17 14
Iteration 1
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
48
4 5 7 211 19 12 20Value 17 14
Iteration 2
4 19
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
49
19 5 7 211 4 12 20Value 17 14
Iteration 2
4 11
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
50
19 5 7 2114 12 20Value 17 14
Iteration 2
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
51
19 5 7 2114 12 20Value 17 14
Iteration 3: step 0.
5 19
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
52
195 7 2114 12 20Value 17 14
Iteration 3
5 11
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
53
195 7 2114 12 20Value 17 14
Iteration 3
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
54
195 7 2114 12 20Value 17 14
Iteration 4
7 19
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
55
195 7 2114 12 20Value 17 14
2 3 4 50 1 8 9Array index 6 7
Iteration 4
7 11
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
56
195 7 2114 12 20Value 17 14
Iteration 4
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
57
195 7 2114 12 20Value 17 14
Iteration 5
2 19
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
58
195 7 2114 12 20Value 17 14
Iteration 5:
2 11
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
59
195 7 2 114 12 20Value 17 14
Iteration 5:
2 7
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
60
195 72 114 12 20Value 17 14
Iteration 5
2 5
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
61
195 72 114 12 20Value 17 14
Iteration 5
2 4
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
62
195 72 114 12 20Value 17 14
Iteration 5
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
195 72 114 12 20Value 17 14
Iteration 6
17 19
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
64
195 72 114 12 20Value 17 14
Iteration 6
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
65
195 72 114 12 20Value 17 14
Iteration 7
14 19
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
66
195 72 114 12 20Value 17 14
Iteration 7
14 17
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
67
195 72 114 12 20Value 1714
Iteration 7
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
68
195 72 114 12 20Value 1714
Iteration 8
12 19
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
69
195 72 114 12 20Value 1714
Iteration 8
12 17
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
70
195 72 114 12 20Value 1714
Iteration 8
12 14
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
71
195 72 114 12 20Value 1714
Iteration 8
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
72
195 72 114 12 20Value 1714
Iteration 9
2 3 4 50 1 8 9Array index 6 7
Insertion Sort• Iteration i. Repeatedly swap element i with the one
to its left if smaller.
• Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.
73
195 72 114 12 20Value 1714
Iteration 10 DONE.
2 3 4 50 1 8 9Array index 6 7
Bubble Sort• It’s called bubble sort because
– larger values gradually “bubble” their way upward to the end of the array like air bubbles rising in water.
• The technique is to make several passes through the array. – On each pass, pairs of adjacent elements are compared. – If the pair is in increasing order, we leave the values as
they are.– If the pair is in decreasing order, we swap the values.
Pass 130, 50, 40, 80, 70, 10, 90, 6030, 40, 50, 80, 70, 10, 90, 6030, 40, 50, 80, 70, 10, 90, 6030, 40, 50, 70, 80, 10, 90, 6030, 40, 50, 70, 10, 80, 90, 6030, 40, 50, 70, 10, 80, 90, 6030, 40, 50, 70, 10, 80, 60, 90
Copyright © 2012 Pearson Education, Inc.
Pass 230, 40, 50, 70, 10, 80, 60, 9030, 40, 50, 70, 10, 80, 60, 9030, 40, 50, 70, 10, 80, 60, 9030, 40, 50, 10, 70, 80, 60, 9030, 40, 50, 10, 70, 80, 60, 9030, 40, 50, 10, 70, 60, 80, 90
Continue..
Copyright © 2012 Pearson Education, Inc.
Example• Count the words in a file and sort them
• See CountWords2.java and CountWordsJava.pdf
Copyright © 2012 Pearson Education, Inc.
Comparing Sorts• The Selection and Insertion sort algorithms are similar in
efficiency
• They 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
• Approximately N2 number of comparisons are made to sort a list of size n
• We therefore say that these sorts are of order N2
• There are other sorting algorithms that are more efficient: order N log2 N
Copyright © 2012 Pearson Education, Inc.
Advantages / Disadvantages of Bubble Sort, Insertion, Selection Sort
• Advantage: they are easy to program.
• Disadvantage: it is very slow.
• Much work in computer science has been geared towards creating more efficient sorting algorithms.
• For example: – Merge Sort– Heapsort– Quick Sort