Chapter 14: Searching and Sorting

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 the class vector
C++ Programming:. From Problem Analysis
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.
CHAPTER 10 ARRAYS II Applications and Extensions.
Searching and Sorting Algorithms Based on D. S
Chapter 19: Searching and Sorting Algorithms
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
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
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.
Data Structures and Algorithms.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 19: Searching and Sorting Algorithms.
Chapter 10 Applications of Arrays and Strings. Chapter Objectives Learn how to implement the sequential search algorithm Explore how to sort an array.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
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.
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.
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
CSC 211 Data Structures Lecture 13
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 10: Applications of Arrays (Searching and Sorting) and the vector Type.
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.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
©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.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
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.
Data Structures Using Java1 Chapter 9 Sorting Algorithms.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
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.
Java Programming: Guided Learning with Early Objects Chapter 8 Applications of Arrays (Sorting and Searching) and Strings.
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
Applications of Arrays
Presentation transcript:

Chapter 14: Searching and Sorting Java Programming: From Problem Analysis to Program Design, Fourth Edition

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 Edition

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 Edition

Search Necessary components to search a list: After search completed: 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 Edition

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 Edition

Sorting a List Bubble sort Suppose list[0...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, 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: From Problem Analysis to Program Design, Second Edition

Bubble Sort Java Programming: From Problem Analysis to Program Design, Second Edition

Bubble Sort Java Programming: From Problem Analysis to Program Design, Second Edition

Bubble Sort 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; } Java Programming: From Problem Analysis to Program Design, Second Edition

Bubble 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. Java Programming: From Problem Analysis to Program Design, Second Edition

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 Edition

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

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

Selection Sort 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; } Java Programming: From Problem Analysis to Program Design, Second Edition

Selection 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. Java Programming: From Problem Analysis to Program Design, Second Edition

Insertion Sort The insertion sort algorithm sorts the list by moving each element to its proper place. Java Programming: From Problem Analysis to Program Design, Second Edition

Insertion Sort Java Programming: From Problem Analysis to Program Design, Second Edition

Insertion Sort Java Programming: From Problem Analysis to Program Design, Second Edition

Insertion Sort Java Programming: From Problem Analysis to Program Design, Second Edition

Insertion Sort 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 Java Programming: From Problem Analysis to Program Design, Second Edition

Insertion 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. Java Programming: From Problem Analysis to Program Design, Second Edition

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 Java Programming: From Problem Analysis to Program Design, Second Edition

Binary 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.) Java Programming: From Problem Analysis to Program Design, Second Edition

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: From Problem Analysis to Program Design, Second Edition

Binary Search Algorithm Determine whether 75 is in the list. Java Programming: From Problem Analysis to Program Design, Second Edition

Binary 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 <= last && !found) mid = (first + last) / 2; if (list[mid] == searchItem) found = true; else if (list[mid] > searchItem) last = mid - 1; first = mid + 1; } if (found) return mid; return –1; } //end binarySearch Java Programming: From Problem Analysis to Program Design, Second Edition

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 Edition

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

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

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

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

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 Edition

Vectors Vector<String> stringList = new Vector<String>(); stringList.addElement("Spring"); stringList.addElement("Summer"); stringList.addElement("Fall"); stringList.addElement("Winter"); Java Programming: From Problem Analysis to Program Design, Second Edition

Programming 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_candidate Java Programming: From Problem Analysis to Program Design, Second Edition

Programming Example: Election Results Output: Election results in a tabular form. Each candidate’s name. Number of votes each candidate received in each region. Total number of votes each candidate received. Java Programming: From Problem Analysis to Program Design, Second Edition

Programming Example: Election 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. Java Programming: From Problem Analysis to Program Design, Second Edition

Programming Example: Election Results (Solution) Sorting the array candidatesName. Processing the voting data. Calculating the total votes received by each candidate. Outputting the results in tabular form. Java Programming: From Problem Analysis to Program Design, Second Edition

Programming Example: Election Results Java Programming: From Problem Analysis to Program Design, Second Edition

Programming Example: Election Results Java Programming: From Problem Analysis to Program Design, Second Edition

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

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

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

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

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

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 Edition

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 Edition

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

Chapter Summary Lists Searching lists: Sorting 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 Edition

Chapter Summary Programming examples The class Vector The class String Members of the class Vector The class String Additional methods of the class String Java Programming: From Problem Analysis to Program Design, Second Edition