Download presentation
Presentation is loading. Please wait.
Published byAlessandra Westmoreland Modified over 10 years ago
1
Chapter 10: Applications of Arrays and Strings J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition
2
Java Programming: From Problem Analysis to Program Design, Second Edition2 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.
3
Java Programming: From Problem Analysis to Program Design, Second Edition3 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.
4
Java Programming: From Problem Analysis to Program Design, Second Edition4 Search 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.
5
Java Programming: From Problem Analysis to Program Design, Second Edition5 Search public static int seqSearch(int[] list, int listLength, int searchItem) { int loc; boolean found = false; for (loc = 0; loc < listLength; loc++) if (list[loc] == searchItem) { found = true; break; } if (found) return loc; else return -1; }
6
Java Programming: From Problem Analysis to Program Design, Second Edition6 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.
7
Java Programming: From Problem Analysis to Program Design, Second Edition7 Selection Sort
8
Java Programming: From Problem Analysis to Program Design, Second Edition8 Selection Sort
9
Java Programming: From Problem Analysis to Program Design, Second Edition9 public static void selectionSort(int[] list, int listLength) { int index; int smallestIndex; int minIndex; int temp; for (index = 0; index < listLength – 1; index++) { smallestIndex = index; for (minIndex = index + 1; minIndex < listLength; minIndex++) if (list[minIndex] < list[smallestIndex]) smallestIndex = minIndex; temp = list[smallestIndex]; list[smallestIndex] = list[index]; list[index] = temp; } Selection Sort
10
Java Programming: From Problem Analysis to Program Design, Second Edition10 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. Selection Sort
11
Java Programming: From Problem Analysis to Program Design, Second Edition11 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 a vector.
12
Java Programming: From Problem Analysis to Program Design, Second Edition12 Members of the class Vector
13
Java Programming: From Problem Analysis to Program Design, Second Edition13 Members of the class Vector
14
Java Programming: From Problem Analysis to Program Design, Second Edition14 Members of the class Vector
15
Java Programming: From Problem Analysis to Program Design, Second Edition15 Members of the class Vector
16
Java Programming: From Problem Analysis to Program Design, Second Edition16 Vectors 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.
17
Java Programming: From Problem Analysis to Program Design, Second Edition17 Vector stringList = new Vector (); stringList.addElement("Spring"); stringList.addElement("Summer"); stringList.addElement("Fall"); stringList.addElement("Winter"); Vectors
18
Java Programming: From Problem Analysis to Program Design, Second Edition18 Additional String Methods
19
Java Programming: From Problem Analysis to Program Design, Second Edition19 Additional String Methods
20
Java Programming: From Problem Analysis to Program Design, Second Edition20 Additional String Methods
21
Java Programming: From Problem Analysis to Program Design, Second Edition21 Additional String Methods
22
Java Programming: From Problem Analysis to Program Design, Second Edition22 Effects of Some String Methods
23
Java Programming: From Problem Analysis to Program Design, Second Edition23 Programming 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
24
Java Programming: From Problem Analysis to Program Design, Second Edition24 Programming 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.
25
Java Programming: From Problem Analysis to Program Design, Second Edition25 Programming Example: Pig Latin Strings (Sample Runs)
26
Java Programming: From Problem Analysis to Program Design, Second Edition26 Chapter Summary Lists Searching lists: Sequential searching Sequential searching on an order list Binary search Sorting lists: Bubble sort Selection sort Insertion sort
27
Java Programming: From Problem Analysis to Program Design, Second Edition27 Chapter Summary Programming examples The class Vector Members of the class Vector The class String Additional methods of the class String
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.