Chapter 10: Applications of Arrays and Strings J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second.

Slides:



Advertisements
Similar presentations
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 21: Graphs.
Advertisements

FIRST COURSE Outlook Tutorial 1 Communicating with Outlook 2007.
Chapter 6: User-Defined Functions I
Excel Tutorial 2 Formatting a Workbook
FIRST COURSE Excel Tutorial 3 Working with Formulas and Functions.
Guide to Networking Essentials Fifth Edition
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.
Chapter 10: Applications of Arrays and the class vector
CCNA Guide to Cisco Networking Fundamentals Fourth Edition
C++ Programming:. From Problem Analysis
Dr. Liu1 Chapter 3 Array-Based Lists. Dr. Liu2 Chapter Objectives Learn about lists Explore how various operations, such as search, insert, and remove,
Data Structures Using Java1 Chapter 3 Array-Based Lists.
ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 10 Creating Classes and Objects.
An Introduction to Programming with C++ Fifth Edition
An Introduction to Programming with C++ Fifth Edition Chapter 3 Completing the Problem-Solving Process and Getting Started with C++
Chapter 12: Inheritance and Composition
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 20: Binary Trees.
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Microsoft Office 2007 Access Integration Feature Sharing Data Among Applications.
Network+ Guide to Networks 6 th Edition Chapter 9 In-Depth TCP/IP Networking.
Solving Problems with Computers
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Visual C++ Programming: Concepts and Projects
CHAPTER 10 ARRAYS II Applications and Extensions.
Searching and Sorting Algorithms Based on D. S
Data Structures & Algorithms CHAPTER 3 Sorting Ms. Manal Al-Asmari.
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.
1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Chapter 8 ARRAYS Continued
Data Structures Using Java1 Chapter 8 Search Algorithms.
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.
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.
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Sorting Algorithms: Selection, Insertion and Bubble.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
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.
Common Elementary Algorithms Some of the basic but frequently used algorithms for manipulating arrays. These algorithms are so important that: a)Some programming.
Java Programming: Guided Learning with Early Objects Chapter 8 Applications of Arrays (Sorting and Searching) and Strings.
Chapter 16: Searching, Sorting, and the vector Type
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
Chapter 9: Sorting and Searching Arrays
Introduction to Search Algorithms
Sorting Algorithms: Selection, Insertion and Bubble
Describing algorithms in pseudo code
Applications of Arrays
Presentation transcript:

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

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.

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.

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.

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; }

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.

Java Programming: From Problem Analysis to Program Design, Second Edition7 Selection Sort

Java Programming: From Problem Analysis to Program Design, Second Edition8 Selection Sort

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

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

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.

Java Programming: From Problem Analysis to Program Design, Second Edition12 Members of the class Vector

Java Programming: From Problem Analysis to Program Design, Second Edition13 Members of the class Vector

Java Programming: From Problem Analysis to Program Design, Second Edition14 Members of the class Vector

Java Programming: From Problem Analysis to Program Design, Second Edition15 Members of the class Vector

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.

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

Java Programming: From Problem Analysis to Program Design, Second Edition18 Additional String Methods

Java Programming: From Problem Analysis to Program Design, Second Edition19 Additional String Methods

Java Programming: From Problem Analysis to Program Design, Second Edition20 Additional String Methods

Java Programming: From Problem Analysis to Program Design, Second Edition21 Additional String Methods

Java Programming: From Problem Analysis to Program Design, Second Edition22 Effects of Some String Methods

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

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.

Java Programming: From Problem Analysis to Program Design, Second Edition25 Programming Example: Pig Latin Strings (Sample Runs)

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

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