Академический Документы
Профессиональный Документы
Культура Документы
Chapter Objectives
Learn how to implement the sequential search algorithm Explore how to sort an array using bubble sort, selection sort, and insertion sort algorithms Learn how to implement the binary search algorithm Become aware of the class Vector Learn more about manipulating strings using the class String
Java Programming: From Problem Analysis to Program Design, 3e 2
List Processing
List: a set of values of the same type Basic operations performed on a list
Search list for given item Sort list Insert item in list Delete item from list
Search
Necessary components to search a list
Array containing the list Length of the list Item for which you are searching
Search (continued)
Suppose that you want to determine whether 27 is in the list First compare 27 with list[0]; that is, compare 27 with 35 Because list[0] 27, you then compare 27 with list[1] Because list[1] 27, you compare 27 with the next element in the list Because list[2] = 27, the search stops This search is successful
Java Programming: From Problem Analysis to Program Design, 3e 5
Search (continued)
Lets now search for 10 The search starts at the first element in the list; that is, at list[0] Proceeding as before, we see that this time the search item, which is 10, is compared with every item in the list Eventually, no more data is left in the list to compare with the search item; this is an unsuccessful search
Java Programming: From Problem Analysis to Program Design, 3e 6
Search (continued)
Search (continued)
Search (continued)
Using a while (or a for) loop, the definition of the method seqSearch can also be written without the break statement as:
Sorting a List
Bubble sort
Suppose list[0...n - 1] is a list of n elements, indexed 0 to n - 1 We want to rearrange; that is, sort, the elements of list in increasing order The bubble sort algorithm works as follows:
In a series of n - 1 iterations, the successive elements, list[index] and list[index + 1] of list are compared If list[index] is greater than list[index + 1], then the elements list[index] and list[index + 1] are swapped, that is, interchanged
Java Programming: From Problem Analysis to Program Design, 3e 10
Bubble Sort
11
12
13
Selection Sort
List is sorted by selecting list element and moving it to its proper position Algorithm finds position of smallest element and moves it to top of unsorted portion of list Repeats process above until entire list is sorted
Java Programming: From Problem Analysis to Program Design, 3e 15
16
17
19
Insertion Sort
The insertion sort algorithm sorts the list by moving each element to its proper place
20
21
22
23
25
1 2 3 4
//Line 5 //Line 6
7 8 9 10 11 12 13
26
Binary Search
Can only be performed on a sorted list Uses divide and conquer technique to search list
27
29
30
32
33
34
35
In general, if L is a sorted list of size n, to determine whether an element is in L, the binary search makes at most 2log2n + 2 key (item) comparisons
Java Programming: From Problem Analysis to Program Design, 3e 37
Vectors
The class Vector can be used to implement a list Unlike an array, the size of a Vector object can grow/shrink during program execution You do not need to worry about the number of data elements in vector
38
39
40
41
42
43
Vectors (continued)
Every element of a Vector object is a reference variable of the type Object To add an element into a Vector object
Create appropriate object Store data into object Store address of object holding data into Vector object element
Java Programming: From Problem Analysis to Program Design, 3e 44
Vectors (continued)
Vector<String> stringList = new Vector<String>(); stringList.addElement("Spring"); stringList.addElement("Summer"); stringList.addElement("Fall"); stringList.addElement("Winter");
45
46
47
48
49
50
51
52
53
54
55
56
58
59
Chapter Summary
Lists Searching lists
Sequential searching Sequential searching on an order list Binary Search
Sorting lists
Bubble Sort Selection Sort Insertion Sort
Java Programming: From Problem Analysis to Program Design, 3e 60
61