Chapter 10 Class Vector and String and Enumeration




![Search (continued) public static int seq. Search(int[] list, int list. Length, int search. Item) Search (continued) public static int seq. Search(int[] list, int list. Length, int search. Item)](https://slidetodoc.com/presentation_image/1c56668e7e0b0f4d36a1614d1b7d4a25/image-5.jpg)
![Bubble Sort s Suppose list[0. . . n - 1] is a list of Bubble Sort s Suppose list[0. . . n - 1] is a list of](https://slidetodoc.com/presentation_image/1c56668e7e0b0f4d36a1614d1b7d4a25/image-6.jpg)


![Bubble Sort (continued) public static void bubble. Sort(int list[], int list. Length) { int Bubble Sort (continued) public static void bubble. Sort(int list[], int list. Length) { int](https://slidetodoc.com/presentation_image/1c56668e7e0b0f4d36a1614d1b7d4a25/image-9.jpg)



![Selection Sort (continued) public static void selection. Sort(int[] list, int list. Length) { int Selection Sort (continued) public static void selection. Sort(int[] list, int list. Length) { int](https://slidetodoc.com/presentation_image/1c56668e7e0b0f4d36a1614d1b7d4a25/image-13.jpg)




![Insertion Sort (continued) public static void insertion. Sort(int[] list, int no. Of. Elements) { Insertion Sort (continued) public static void insertion. Sort(int[] list, int no. Of. Elements) {](https://slidetodoc.com/presentation_image/1c56668e7e0b0f4d36a1614d1b7d4a25/image-18.jpg)
![Sequential Ordered Search public static int seq. Ordered. Search(int[] list, int list. Length, int Sequential Ordered Search public static int seq. Ordered. Search(int[] list, int list. Length, int](https://slidetodoc.com/presentation_image/1c56668e7e0b0f4d36a1614d1b7d4a25/image-19.jpg)


![Binary Search Algorithm public static int binary. Search(int[] list, int list. Length, int search. Binary Search Algorithm public static int binary. Search(int[] list, int list. Length, int search.](https://slidetodoc.com/presentation_image/1c56668e7e0b0f4d36a1614d1b7d4a25/image-22.jpg)


















- Slides: 40

Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures

Chapter Objectives s Learn how to implement the sequential search algorithm s Explore how to sort an array using bubble sort, selection sort, and insertion sort algorithms s Learn how to implement the binary search algorithm s Become aware of the class Vector s Learn more about manipulating strings using the class String Java Programming: Program Design Including Data Structures 2

List Processing s List: A set of values of the same type s Basic operations performed on a list: s s Search list for given item Sort list Insert item in list Delete item from list Java Programming: Program Design Including Data Structures 3

Search s Necessary components to search a list: s Array containing the list s Length of the list s Item for which you are searching s After search completed: s If item found, report “success” and return location in array s If item not found, report “failure” Java Programming: Program Design Including Data Structures 4
![Search continued public static int seq Searchint list int list Length int search Item Search (continued) public static int seq. Search(int[] list, int list. Length, int search. Item)](https://slidetodoc.com/presentation_image/1c56668e7e0b0f4d36a1614d1b7d4a25/image-5.jpg)
Search (continued) public static int seq. Search(int[] list, int list. Length, int search. Item) { int loc; boolean found = false; for (loc = 0; loc < list. Length; loc++) if (list[loc] == search. Item) { found = true; break; } if (found) return loc; else return -1; } Java Programming: Program Design Including Data Structures 5
![Bubble Sort s Suppose list0 n 1 is a list of Bubble Sort s Suppose list[0. . . n - 1] is a list of](https://slidetodoc.com/presentation_image/1c56668e7e0b0f4d36a1614d1b7d4a25/image-6.jpg)
Bubble Sort s Suppose list[0. . . n - 1] is a list of n elements s We want to rearrange (sort) the elements of list s The bubble sort algorithm works as follows: s In a series of n - 1 iterations, the successive elements, list[index] and list[index + 1], of list are compared s If list[index] is greater than list[index + 1], then the elements list[index] and list[index + 1] are swapped (interchanged) Java Programming: Program Design Including Data Structures 6

Bubble Sort Java Programming: Program Design Including Data Structures 7

Bubble Sort (continued) Java Programming: Program Design Including Data Structures 8
![Bubble Sort continued public static void bubble Sortint list int list Length int Bubble Sort (continued) public static void bubble. Sort(int list[], int list. Length) { int](https://slidetodoc.com/presentation_image/1c56668e7e0b0f4d36a1614d1b7d4a25/image-9.jpg)
Bubble Sort (continued) public static void bubble. Sort(int list[], int list. Length) { int temp; int counter, index; for (counter = 0; counter < list. Length - 1; counter++) { for (index = 0; index < list. Length - 1 – counter; index++) if (list[index] > list[index + 1]) { temp = list[index]; list[index] = list[index + 1]; list[index + 1] = temp; } } } Java Programming: Program Design Including Data Structures 9

Selection Sort s List is sorted by selecting list element and moving it to its proper position s Algorithm finds position of smallest element and moves it to top of unsorted portion of list s Repeats process above until entire list is sorted Java Programming: Program Design Including Data Structures 10

Selection Sort (continued) Java Programming: Program Design Including Data Structures 11

Selection Sort (continued) Java Programming: Program Design Including Data Structures 12
![Selection Sort continued public static void selection Sortint list int list Length int Selection Sort (continued) public static void selection. Sort(int[] list, int list. Length) { int](https://slidetodoc.com/presentation_image/1c56668e7e0b0f4d36a1614d1b7d4a25/image-13.jpg)
Selection Sort (continued) public static void selection. Sort(int[] list, int list. Length) { int index; int smallest. Index; int min. Index; int temp; for (index = 0; index < list. Length – 1; index++) { smallest. Index = index; for (min. Index = index + 1; min. Index < list. Length; min. Index++) if (list[min. Index] < list[smallest. Index]) smallest. Index = min. Index; temp = list[smallest. Index]; list[smallest. Index] = list[index]; list[index] = temp; } } Java Programming: Program Design Including Data Structures 13

Insertion Sort The insertion sort algorithm sorts the list by moving each element to its proper place Java Programming: Program Design Including Data Structures 14

Insertion Sort (continued) Java Programming: Program Design Including Data Structures 15

Insertion Sort (continued) Java Programming: Program Design Including Data Structures 16

Insertion Sort (continued) Java Programming: Program Design Including Data Structures 17
![Insertion Sort continued public static void insertion Sortint list int no Of Elements Insertion Sort (continued) public static void insertion. Sort(int[] list, int no. Of. Elements) {](https://slidetodoc.com/presentation_image/1c56668e7e0b0f4d36a1614d1b7d4a25/image-18.jpg)
Insertion Sort (continued) public static void insertion. Sort(int[] list, int no. Of. Elements) { int first. Out. Of. Order, location; int temp; for (first. Out. Of. Order = 1; first. Out. Of. Order < no. Of. Elements; first. Out. Of. Order++) { if (list[first. Out. Of. Order] < list[first. Out. Of. Order - 1]) { temp = list[first. Out. Of. Order]; location = first. Out. Of. Order; do { list[location] = list[location - 1]; location--; } while(location > 0 && list[location - 1] > temp); list[location] = temp; } } } //end insertion. Sort Java Programming: Program Design Including Data Structures 18
![Sequential Ordered Search public static int seq Ordered Searchint list int list Length int Sequential Ordered Search public static int seq. Ordered. Search(int[] list, int list. Length, int](https://slidetodoc.com/presentation_image/1c56668e7e0b0f4d36a1614d1b7d4a25/image-19.jpg)
Sequential Ordered Search public static int seq. Ordered. Search(int[] list, int list. Length, int search. Item) { int loc; //Line 1 boolean found = false; //Line 2 for (loc = 0; loc < list. Length; loc++) { //Line 3 if (list[loc] >= search. Item) { //Line 4 found = true; //Line 5 break; //Line 6 } } if (found) //Line 7 if (list[loc] == search. Item) //Line 8 return loc; //Line 9 else //Line 10 return -1; //Line 11 else //Line 12 return -1; //Line 13 } // In Line 11, return –(loc+1); Java Programming: Program Design Including Data Structures 19

Binary Search Algorithm s Search item is compared with middle element of list s If search item < middle element of list, search is restricted to first half of the list s If search item > middle element of list, search is restricted to second half of the list s If search item = middle element, search is complete Java Programming: Program Design Including Data Structures 20

Binary Search Algorithm Determine whether 75 is in the list. Java Programming: Program Design Including Data Structures 21
![Binary Search Algorithm public static int binary Searchint list int list Length int search Binary Search Algorithm public static int binary. Search(int[] list, int list. Length, int search.](https://slidetodoc.com/presentation_image/1c56668e7e0b0f4d36a1614d1b7d4a25/image-22.jpg)
Binary Search Algorithm public static int binary. Search(int[] list, int list. Length, int search. Item) { int first = 0; int last = list. Length - 1; int mid; boolean found = false; while (first <= last && !found) { mid = (first + last) / 2; if (list[mid] == search. Item) found = true; else if (list[mid] > search. Item) last = mid - 1; else first = mid + 1; } if (found) return mid; else return – 1; } //end binary. Search Java Programming: Program Design Including Data Structures 22

Vectors s The class Vector can be used to implement a list s Unlike an array, the size of a Vector object can grow/shrink during program execution s You do not need to worry about the number of data elements in a vector Java Programming: Program Design Including Data Structures 23

Members of the class Vector Java Programming: Program Design Including Data Structures 24

Members of the class Vector (continued) Java Programming: Program Design Including Data Structures 25

Members of the class Vector (continued) Java Programming: Program Design Including Data Structures 26

Members of the class Vector (continued) Java Programming: Program Design Including Data Structures 27

Vectors (continued) s Every element of a Vector object is a reference variable of the type Object s To add an element into a Vector object: s Create appropriate object s Store data into object s Store address of object holding data into Vector object element Java Programming: Program Design Including Data Structures 28

Vectors (continued) Vector<String> string. List = new Vector<String>(); string. List. add. Element("Spring"); string. List. add. Element("Summer"); string. List. add. Element("Fall"); string. List. add. Element("Winter"); Java Programming: Program Design Including Data Structures 29

String s The class String is a very important class in Java. s Additional String methods are discussed. Java Programming: Program Design Including Data Structures 30

Additional String Methods Java Programming: Program Design Including Data Structures 31

Additional String Methods (continued) Java Programming: Program Design Including Data Structures 32

Additional String Methods (continued) Java Programming: Program Design Including Data Structures 33

Additional String Methods (continued) Java Programming: Program Design Including Data Structures 34

Executing String Methods s Consider the following statements: String sentence; String str 1; str 2; str 3; str 4; sentence = “It is sunny and warm. ”; str 1 = “warm. ”; str 2 = “Programming with Java”; str 3 = “sunny”; str 4 = “Learning Java Programming is exciting”; Java Programming: Program Design Including Data Structures 35

Effects of Some String Methods Java Programming: Program Design Including Data Structures 36

Enumeration Types s Java allows programmers to create their own data types by specifying the values of that data type. s The values belonging to primitive data types are predefined. s enumeration of enum types using the key word enum s The values of the enum data type are identifiers. s Used as a public static reference variables to objects of the enum type enum Grades {A, B, C, D, F}; where A, B, …, F are enum constants (unique values) Grades an enum type Grades my. Grade; my. Grade = Grades. B; Java Programming: Program Design Including Data Structures 37

Enumeration Methods s Methods in enumeration of enum types s ordinal() returns the ordinal value of an enum constant s name() returns the name of the enum value s values() returns the values of an enum type as a list Java Programming: Program Design Including Data Structures 38

Chapter Summary s Lists s Searching lists: s Sequential searching on an order list s Binary search s Sorting lists: s Bubble sort s Selection sort s Insertion sort Java Programming: Program Design Including Data Structures 39

Chapter Summary (continued) s Programming examples s The class Vector s Members of the class Vector s The class String s Additional methods of the class String s Enumeration enum Types s Members of the class Vector Java Programming: Program Design Including Data Structures 40