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

Slides:



Advertisements
Similar presentations
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Advertisements

Chapter 10: Applications of Arrays and Strings J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Data Structures Using C++ 2E
CHAPTER 10 ARRAYS II Applications and Extensions.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Searching and Sorting Algorithms Based on D. S
Searching Slowly And Fastly. public static int find(int x, int[] a, int n) { for (int loc = 0; loc < n; loc++) if (a[loc] == x) return loc;//found return.
Data Structures & Algorithms CHAPTER 3 Sorting Ms. Manal Al-Asmari.
1 Sorting II: Bubble Sort and Selection Sort CSC326 Information Structure Spring 2009.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 10 Applications of Arrays.
Problem Solving #6: Search & Sort ICS Outline Review of Key Topics Review of Key Topics Problem 1: Recursive Binary Search … Problem 1: Recursive.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Data Structures Using Java1 Chapter 8 Search Algorithms.
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Sorting Algorithms: Selection, Insertion and Bubble.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006.
Chapter 8 ARRAYS Continued
Data Structures Using Java1 Chapter 8 Search Algorithms.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Chapter 16: Searching, Sorting, and the vector Type.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
Data Structures and Algorithms.
Chapter 10 Applications of Arrays and Strings. Chapter Objectives Learn how to implement the sequential search algorithm Explore how to sort an array.
Applications of Arrays (Searching and Sorting) and Strings
Chapter 19: Searching and Sorting Algorithms
Lecture 12. Searching Algorithms and its analysis 1.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Computer Science Searching & Sorting.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Vectors, Strings, and Enumeration Data Types.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
Java Programming: Program Design Including Data Structures 1 Vectors The class Vector can be used to implement a list Unlike an array, the size of a Vector.
Chapter 14: Searching and Sorting
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 10: Applications of Arrays (Searching and Sorting) and the vector Type.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Searching and Sorting.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Sorting Algorithms: Selection, Insertion and Bubble.
Data Structures Using C++1 Chapter 10 Sorting Algorithms.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Data Structures Using Java1 Chapter 9 Sorting Algorithms.
1 CS 132 Spring 2008 Chapter 10 Sorting Algorithms.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Chapter 16: Searching, Sorting, and the vector Type.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching and Sorting Arrays
Chapter 16: Searching, Sorting, and the vector Type
Chapter 9: Sorting and Searching Arrays
Sorting Mr. Jacobs.
Chapter 18: Searching and Sorting Algorithms
Introduction to Search Algorithms
Data Structures Using C++
Sorting Algorithms: Selection, Insertion and Bubble
Searching and Sorting Arrays
Searching and Sorting Arrays
Module 8 – Searching & Sorting Algorithms
Module 8 – Searching & Sorting Algorithms
Applications of Arrays
Presentation transcript:

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

Java Programming: Program Design Including Data Structures2 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: Program Design Including Data Structures3 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

Java Programming: Program Design Including Data Structures4 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”

Java Programming: Program Design Including Data Structures5 Search (continued) 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; }

Java Programming: Program Design Including Data Structures6 Bubble Sort  Suppose list[0...n - 1] is a list of n elements  We want to rearrange (sort) the elements of list  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 (interchanged)

Java Programming: Program Design Including Data Structures7 Bubble Sort

Java Programming: Program Design Including Data Structures8 Bubble Sort (continued)

Java Programming: Program Design Including Data Structures9 public static void bubbleSort(int list[], int listLength) { int temp; int counter, index; for (counter = 0; counter < listLength - 1; counter++) { for (index = 0; index < listLength - 1 – counter; index++) if (list[index] > list[index + 1]) { temp = list[index]; list[index] = list[index + 1]; list[index + 1] = temp; } Bubble Sort (continued)

Java Programming: Program Design Including Data Structures10 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: Program Design Including Data Structures11 Selection Sort (continued)

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

Java Programming: Program Design Including Data Structures13 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 (continued)

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

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

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

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

Java Programming: Program Design Including Data Structures18 public static void insertionSort(int[] list,int noOfElements) { int firstOutOfOrder, location; int temp; for (firstOutOfOrder = 1; firstOutOfOrder < noOfElements; firstOutOfOrder++) { if (list[firstOutOfOrder] < list[firstOutOfOrder - 1]) { temp = list[firstOutOfOrder]; location = firstOutOfOrder; do { list[location] = list[location - 1]; location--; } while(location > 0 && list[location - 1] > temp); list[location] = temp; } } //end insertionSort Insertion Sort (continued)

Java Programming: Program Design Including Data Structures19 Sequential 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 < listLength; loc++) { //Line 3 if (list[loc] >= searchItem) { //Line 4 found = true; //Line 5 break; //Line 6 } if (found) //Line 7 if (list[loc] == searchItem) //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 Structures20 Binary Search Algorithm  Search item is compared with middle element of list  If search item < middle element of list, search is restricted to first half of the list  If search item > middle element of list, search is restricted to second half of the list  If search item = middle element, search is complete

Java Programming: Program Design Including Data Structures21 Binary Search Algorithm Determine whether 75 is in the list.

Java Programming: Program Design Including Data Structures22 public static int binarySearch(int[] list, int listLength, int searchItem) { int first = 0; int last = listLength - 1; int mid; boolean found = false; while (first <= last && !found) { mid = (first + last) / 2; if (list[mid] == searchItem) found = true; else if (list[mid] > searchItem) last = mid - 1; else first = mid + 1; } if (found) return mid; else return –1; } //end binarySearch Binary Search Algorithm

Java Programming: Program Design Including Data Structures23 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

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

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

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

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

Java Programming: Program Design Including Data Structures28 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: Program Design Including Data Structures29 Vector stringList = new Vector (); stringList.addElement("Spring"); stringList.addElement("Summer"); stringList.addElement("Fall"); stringList.addElement("Winter"); Vectors (continued)

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

Java Programming: Program Design Including Data Structures31 Additional String Methods

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

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

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

Executing String Methods  Consider the following statements: String sentence; String str1; String str2; String str3; String str4; sentence = “It is sunny and warm.”; str1 = “warm.”; str2 = “Programming with Java”; str3 = “sunny”; str4 = “Learning Java Programming is exciting”; Java Programming: Program Design Including Data Structures35

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

Java Programming: Program Design Including Data Structures37 Enumeration Types  Java allows programmers to create their own data types by specifying the values of that data type.  The values belonging to primitive data types are predefined.  enumeration of enum types using the key word enum  The values of the enum data type are identifiers.  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 myGrade; myGrade = Grades.B;

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

Java Programming: Program Design Including Data Structures39 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: Program Design Including Data Structures40 Chapter Summary (continued)  Programming examples  The class Vector  Members of the class Vector  The class String  Additional methods of the class String  Enumeration enum Types  Members of the class Vector