Title: Chapter 10: Applications of Arrays and Strings
1Chapter 10 Applications of Arrays and Strings
- Java Programming
- From Problem Analysis to Program Design,
- Second Edition
2Chapter 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.
3List 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.
4Search
- Necessary components to search a list
- Array containing the list.
- Length of the list.
- Item for which you are searching.
- After search completed
- If item found, report success and return
location in array. - If item not found, report failure.
5Search
- public static int seqSearch(int list,
- int listLength, int searchItem)
-
- int loc
- boolean found false
- for (loc 0 loc lt listLength loc)
- if (listloc searchItem)
-
- found true
- break
-
- if (found)
- return loc
- else
- return -1
6Sorting a List
- Bubble sort
- Suppose list0...n - 1 is a list of n elements,
indexed 0 to n - 1. We want to rearrange (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, listindex and listindex 1, of
list are compared. If listindex is greater than
listindex 1, then the elements listindex
and listindex 1 are swapped (interchanged).
7Bubble Sort
8Bubble Sort
9Bubble Sort
public static void bubbleSort(int list,
int listLength) int
temp int counter, index for (counter
0 counter lt listLength - 1
counter) for (index 0
index lt listLength - 1 counter
index) if (listindex gt
listindex 1)
temp listindex listindex
listindex 1 listindex 1
temp
10Bubble Sort
- For a list of length n, an average bubble sort
makes n(n 1) / 2 key comparisons and about n(n
1) / 4 item assignments. - Therefore, if n 1000, bubble sort makes about
500,000 key comparisons and about 250,000 item
assignments to sort the list.
11Selection 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.
12Selection Sort
13Selection Sort
14Selection Sort
public static void selectionSort(int list,
int listLength)
int index int smallestIndex int
minIndex int temp for (index 0 index
lt listLength 1 index)
smallestIndex index for (minIndex
index 1 minIndex lt listLength
minIndex) if (listminIndex lt
listsmallestIndex)
smallestIndex minIndex temp
listsmallestIndex listsmallestIndex
listindex listindex temp
15Selection Sort
- For a list of length n, an average selection sort
makes n(n 1) / 2 key comparisons and 3(n 1)
item assignments. - Therefore, if n 1000, selection sort makes
about 500,000 key comparisons and about 3000 item
assignments to sort the list. -
16Insertion Sort
The insertion sort algorithm sorts the list by
moving each element to its proper place.
17Insertion Sort
18Insertion Sort
19Insertion Sort
20Insertion Sort
public static void insertionSort(int list,
int noOfElements)
int firstOutOfOrder, location int temp
for (firstOutOfOrder 1
firstOutOfOrder lt noOfElements
firstOutOfOrder) if (listfirstOutOfOrder
lt listfirstOutOfOrder - 1) temp
listfirstOutOfOrder location
firstOutOfOrder do
listlocation listlocation - 1
location-- while(location gt 0
listlocation - 1 gt temp)
listlocation temp //end insertionSort
21Insertion Sort
- For a list of length n, on average, the insertion
sort makes (n2 3n 4) / 4 key comparisons
and about n(n 1) / 4 item assignments. - Therefore, if n 1000, the insertion sort makes
about 250,000 key comparisons and about 250,000
item assignments to sort the list.
22Sequential Ordered Search
public static int seqOrderedSearch(int list,
int listLength, int searchItem)
int loc
//Line 1 boolean found false
//Line 2 for (loc 0 loc lt
listLength loc) //Line 3 if
(listloc gt searchItem) //Line 4
found true
//Line 5 break
//Line 6 if (found)
//Line 7
if (listloc searchItem) //Line 8
return loc
//Line 9 else
//Line 10 return -1
//Line 11 else
//Line 12 return
-1 //Line 13
23Binary Search
- Can only be performed on a sorted list.
- Uses divide and conquer technique to search list.
- If L is a sorted list of size n, to determine
whether an element is in L, the binary search
makes at most 2 log2n 2 key comparisons. - (Faster than a sequential search.)
24Binary Search Algorithm
- Search item is compared with middle element of
list. - If search item lt middle element of list, search
is restricted to first half of the list. - If search item gt middle element of list, search
is restricted to second half of the list. - If search item middle element, search is
complete.
25Binary Search Algorithm
Determine whether 75 is in the list.
26Binary Search Algorithm
public static int binarySearch(int list, int
listLength, int
searchItem) int first 0 int last
listLength - 1 int mid boolean found
false while (first lt last !found)
mid (first last) / 2 if
(listmid searchItem) found
true else if (listmid gt
searchItem) last mid - 1
else first mid 1
if (found) return mid else
return 1 //end binarySearch
27Vectors
- 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 a vector.
28Members of the class Vector
29Members of the class Vector
30Members of the class Vector
31Members of the class Vector
32Vectors
- 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.
33Vectors
VectorltStringgt stringList new
VectorltStringgt() stringList.addElement("Spring")
stringList.addElement("Summer") stringList.addE
lement("Fall") stringList.addElement("Winter")
34Programming Example Election Results
- Input Two files
- File 1 Candidates names
- File 2 Voting data
- Voting data format
- candidate_name region number_of_votes_for_this_ca
ndidate
35Programming Example Election Results
- Output Election results in a tabular form.
- Each candidates name.
- Number of votes each candidate received in each
region. - Total number of votes each candidate received.
36Programming ExampleElection Results (Solution)
- The solution includes
- Reading the candidates names into the array
candidateName. - A two-dimensional array consisting of the votes
by region. - An array consisting of the total votes parallel
to the candidateName array.
37Programming ExampleElection Results (Solution)
- Sorting the array candidatesName.
- Processing the voting data.
- Calculating the total votes received by each
candidate. - Outputting the results in tabular form.
38Programming Example Election Results
39Programming Example Election Results
40Additional String Methods
41Additional String Methods
42Additional String Methods
43Additional String Methods
44Effects of Some String Methods
45Programming Example Pig Latin Strings
- If string begins with a vowel, -way is appended
to it. - If first character is not a vowel
- Add - to end.
- Rotate characters until the first character is a
vowel. - Append ay.
- Input String
- Output String in pig Latin
46Programming Example Pig Latin Strings (Solution)
- Methods isVowel, rotate, pigLatinString
- Use methods to
- Get the string (str).
- Find the pig Latin form of str by using the
method pigLatinString. - Output the pig Latin form of str.
47Programming Example Pig Latin Strings (Sample
Runs)
48Chapter Summary
- Lists
- Searching lists
- Sequential searching
- Sequential searching on an order list
- Binary search
- Sorting lists
- Bubble sort
- Selection sort
- Insertion sort
49Chapter Summary
- Programming examples
- The class Vector
- Members of the class Vector
- The class String
- Additional methods of the class String