Download presentation
Presentation is loading. Please wait.
Published byDarrell Hamilton Modified over 9 years ago
1
Outline ArrayList Searching Sorting Copyright © 2012 Pearson Education, Inc.
2
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.
3
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.
4
The ArrayList Class Copyright © 2012 Pearson Education, Inc. ArrayList cityList = new ArrayList (); //Add some cities in the list cityList.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]
5
See TestArrayList.java TestArrayList.java Copyright © 2012 Pearson Education, Inc.
6
ArrayList Methods ArrayList() Creates an empty list. add(o: Object): boolean Appends 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.
7
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.
8
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.
9
The ArrayList Class The type of object stored in the list is established when the ArrayList object is created: ArrayList names = new ArrayList (); ArrayList list = new ArrayList (); An ArrayList object cannot store primitive types, but wrapper classes are used instead. See Beatles.javaBeatles.java Copyright © 2012 Pearson Education, Inc.
10
//******************************************************************** // 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 band = new ArrayList (); band.add ("Paul"); band.add ("Pete"); band.add ("John"); band.add ("George"); continue
11
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++; }
12
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: John Size of the band: 4 Paul John Ringo George
13
Examples See ReverseFile.java ReverseFile.java See CountVowels.java CountVowels.java See CountWords.javaCountWords.java Copyright © 2012 Pearson Education, Inc.
14
Outline ArrayList Searching Sorting Copyright © 2012 Pearson Education, Inc.
15
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.
16
Linear Search ArrayList implementation: LinearSearch2.java LinearSearch2.java Array implementation: LinearSearch.java LinearSearch.java Copyright © 2012 Pearson Education, Inc.
17
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.
18
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.
19
Binary Search Given a key and sorted array a[], find index i such that a[i] = key, or report that no such index exists.
20
Binary Search for 33 82134657109111214130 64141325335143538472939597966 lo hi
21
Binary Search for 33 82134657109111214130 64141325335143538472939597966 lo hi mid
22
Binary Search for 33 82134657109111214130 64141325335143 53 8472939597966 lo hi
23
Binary Search for 33 82134657109111214130 64141325335143538472939597966 lo midhi
24
Binary Search for 33 82134657109111214130 64141325335143538472939597966 lohi
25
Binary Search for 33 82134657109111214130 64141325335143538472939597966 lohimid
26
Binary Search for 33 82134657109111214130 64141325335143538472939597966 lo hi
27
Binary Search for 33 82134657109111214130 64141325335143538472939597966 lo hi
28
Binary Search Array implementation: BinarySearch2.java BinarySearch2.java ArrayList implementation: BinarySearch.java BinarySearch.java Copyright © 2012 Pearson Education, Inc.
29
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.
30
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.
31
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.
32
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)
33
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... / 2 k times You can simplify this formula using basic mathematics: 1 = N / 2 k 2 k = N k = log 2 N Copyright © 2012 Pearson Education, Inc. O (log N)
34
Outline ArrayList Searching Sorting Copyright © 2012 Pearson Education, Inc.
35
Detailed Outline Sorting Selection Sort Insertion Sort Bubble Sort Copyright © 2012 Pearson Education, Inc.
36
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.
37
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.
38
Outline Sorting Selection Sort Insertion Sort Bubble Sort Copyright © 2012 Pearson Education, Inc.
39
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.
40
Selection Sort Copyright © 2012 Pearson Education, Inc.
41
Selection Sort Wikipedia
42
Selection Sort Implementations See SelectionSortDesc.java SelectionSortDesc.java See SelectionSortStr.java SelectionSortStr.java Sorting an array of String objects containing Turkish letters: ı ö ü ç ğ ş SelectionSortStrTR.javaSelectionSortStrTR.java Copyright © 2012 Pearson Education, Inc.
43
Outline Sorting Selection Sort Insertion Sort Bubble Sort Copyright © 2012 Pearson Education, Inc.
44
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.
45
Insertion Sort Copyright © 2012 Pearson Education, Inc.
46
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 457211191220 Value 1714 Iteration 0 23450189Array index67
47
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 457211191220 Value 1714 Iteration 1 23450189Array index67
48
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 457211191220 Value 1714 Iteration 2 419 23450189Array index67
49
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 195721141220 Value 1714 Iteration 2 411 23450189Array index67
50
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 195721141220 Value 1714 Iteration 2 23450189Array index67
51
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 195721141220 Value 1714 Iteration 3: step 0. 519 23450189Array index67
52
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 195721141220 Value 1714 Iteration 3 511 23450189Array index67
53
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 195721141220 Value 1714 Iteration 3 23450189Array index67
54
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 195721141220 Value 1714 Iteration 4 719 23450189Array index67
55
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 195721141220 Value 1714 23450189Array index67 Iteration 4 711
56
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 195721141220 Value 1714 Iteration 4 23450189Array index67
57
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 195721141220 Value 1714 Iteration 5 219 23450189Array index67
58
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 195721141220 Value 1714 Iteration 5: 211 23450189Array index67
59
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 195721141220 Value 1714 Iteration 5: 27 23450189Array index67
60
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 195721141220 Value 1714 Iteration 5 25 23450189Array index67
61
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 195721141220 Value 1714 Iteration 5 24 23450189Array index67
62
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 195721141220 Value 1714 Iteration 5 23450189Array index67
63
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. 195721141220 Value 1714 Iteration 6 1719 23450189Array index67
64
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 195721141220 Value 1714 Iteration 6 23450189Array index67
65
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 195721141220 Value 1714 Iteration 7 1419 23450189Array index67
66
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 195721141220 Value 1714 Iteration 7 1417 23450189Array index67
67
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 195721141220 Value 1714 Iteration 7 23450189Array index67
68
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 195721141220 Value 1714 Iteration 8 1219 23450189Array index67
69
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 195721141220 Value 1714 Iteration 8 1217 23450189Array index67
70
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 195721141220 Value 1714 Iteration 8 1214 23450189Array index67
71
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 195721141220 Value 1714 Iteration 8 23450189Array index67
72
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 195721141220 Value 1714 Iteration 9 23450189Array index67
73
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 195721141220 Value 1714 Iteration 10 DONE. 23450189Array index67
74
Insertion Sort Wikipedia
75
See InsertionSort.java InsertionSort.java Copyright © 2012 Pearson Education, Inc.
76
Outline Sorting Selection Sort Insertion Sort Bubble Sort Copyright © 2012 Pearson Education, Inc.
77
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.
78
Pass 1 30, 50, 40, 80, 70, 10, 90, 60 30, 40, 50, 80, 70, 10, 90, 60 30, 40, 50, 70, 80, 10, 90, 60 30, 40, 50, 70, 10, 80, 90, 60 30, 40, 50, 70, 10, 80, 60, 90 Copyright © 2012 Pearson Education, Inc.
79
Pass 2 30, 40, 50, 70, 10, 80, 60, 90 30, 40, 50, 10, 70, 80, 60, 90 30, 40, 50, 10, 70, 60, 80, 90 Continue.. Copyright © 2012 Pearson Education, Inc.
80
Bubble Sort Wikipedia
81
See BubbleSortAl.java BubbleSortAl.java See BubbleSortStr.java BubbleSortStr.java Copyright © 2012 Pearson Education, Inc.
82
Example Count the words in a file and sort them See CountWords2.java and CountWordsJava.pdf CountWords2.java CountWordsJava.pdf Copyright © 2012 Pearson Education, Inc.
83
Advice! Try to implement sorting algorithms by yourself Copyright © 2012 Pearson Education, Inc.
84
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 N 2 number of comparisons are made to sort a list of size n We therefore say that these sorts are of order N 2 There are other sorting algorithms that are more efficient: order N log 2 N Copyright © 2012 Pearson Education, Inc.
85
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
86
Comparison of sorting algorithms Graph from Alan Jepson University of Torontoc.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.