Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

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
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.
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
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
Chapter 19: Searching and Sorting Algorithms
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.
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.
Searching Arrays Linear search Binary search small arrays
Analysis of Algorithm.
1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006.
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
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.
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.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
CSC 211 Data Structures Lecture 13
Chapter 14: Searching and Sorting
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 10: Applications of Arrays (Searching and Sorting) and the vector Type.
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.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
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,
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.
CHAPTER 10 ARRAYS II Applications and Extensions.
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.
Searching and Sorting Arrays
Chapter 16: Searching, Sorting, and the vector Type
Chapter 18: Searching and Sorting Algorithms
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Data Structures Using C++
Chapter 7 Single-Dimensional Arrays
Sorting Algorithms: Selection, Insertion and Bubble
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
Searching and Sorting Arrays
Module 8 – Searching & Sorting Algorithms
Applications of Arrays
Presentation transcript:

Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting

Java Programming: From Problem Analysis to Program Design, 4e2 Chapter Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using selection sort and insertion sort algorithms Learn how to implement the binary search algorithm

Java Programming: From Problem Analysis to Program Design, 4e3 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, 4e4 Search Necessary components to search a list –Array containing the list –Length of the list –Item for which you are searching Sequential search is discussed in Chapter 9

Java Programming: From Problem Analysis to Program Design, 4e5 Search (continued) If the search item is the second item in the list, the sequential search makes two key comparisons (also called item comparisons) to determine whether the search item is in the list If the search item is the 900th item in the list, the sequential search makes 900 key comparisons to determine whether the search item is in the list If the search item is not in the list, the sequential search makes 1000 key comparisons

Java Programming: From Problem Analysis to Program Design, 4e6 Search (continued) If searchItem is always at the end of the list, it will take many comparisons to find searchItem If searchItem is not in the list, then we will compare searchItem with every element in the list A sequential search is therefore not efficient for large lists; in fact, it can be proved that, on average, the number of comparisons made by a sequential search is equal to half the size of the list

Java Programming: From Problem Analysis to Program Design, 4e7 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, 4e8 Selection Sort (continued)

Java Programming: From Problem Analysis to Program Design, 4e9 Selection Sort (continued)

Java Programming: From Problem Analysis to Program Design, 4e10 Selection Sort (continued)

Java Programming: From Problem Analysis to Program Design, 4e11 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: From Problem Analysis to Program Design, 4e12 It is known that for a list of length n, on an average, selection sort makes n(n – 1) / 2 key comparisons and 3(n – 1) item assignments Therefore, if n = 1000, then to sort the list, selection sort makes about 500,000 key comparisons and about 3000 item assignments Selection Sort (continued)

Java Programming: From Problem Analysis to Program Design, 4e13 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, 4e14 Insertion Sort (continued)

Java Programming: From Problem Analysis to Program Design, 4e15 Insertion Sort (continued)

Java Programming: From Problem Analysis to Program Design, 4e16 Insertion Sort (continued)

Java Programming: From Problem Analysis to Program Design, 4e17 public static void insertionSort(int[] list, int listLength) { int firstOutOfOrder, location; int temp; for (firstOutOfOrder = 1; firstOutOfOrder < listLength; 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: From Problem Analysis to Program Design, 4e18 It is known that for a list of length n, on average, the insertion sort makes (n 2 + 3n – 4) / 4 key comparisons and about n(n – 1) / 4 item assignments Therefore, if n = 1000, then to sort the list, the insertion sort makes about 250,000 key comparisons and about 250,000 item assignments Insertion Sort (continued)

Java Programming: From Problem Analysis to Program Design, 4e19 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 second half of the list If search item = middle element, search is complete

Java Programming: From Problem Analysis to Program Design, 4e20 Binary Search Algorithm (continued) Determine whether 75 is in the list

Java Programming: From Problem Analysis to Program Design, 4e21 Binary Search Algorithm (continued)

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

Java Programming: From Problem Analysis to Program Design, 4e23 Binary Search Algorithm (continued)

Java Programming: From Problem Analysis to Program Design, 4e24 Binary Search Algorithm (continued)

Java Programming: From Problem Analysis to Program Design, 4e25 Suppose that L is a list of size Since  = 220, it follows that the while loop in binary search will have at most 21 iterations to determine whether an element is in L Every iteration of the while loop makes two key (that is, item) comparisons Performance of the Binary Search

Java Programming: From Problem Analysis to Program Design, 4e26 Performance of the Binary Search (continued) To determine whether an element is in L, binary search makes at most 42 item comparisons –On the other hand, on average, a sequential search will make 500,000 key (item) comparisons to determine whether an element is in L In general, if L is a sorted list of size n, to determine whether an element is in L, the binary search makes at most 2log2n + 2 key (item) comparisons

Java Programming: From Problem Analysis to Program Design, 4e27 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, 4e28 Programming Example: Election Results (continued) 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, 4e29 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

Java Programming: From Problem Analysis to Program Design, 4e30 Programming Example: Election Results (Solution) (continued) The solution includes (continued): –Sorting the array candidateName –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, 4e31 Programming Example: Election Results

Java Programming: From Problem Analysis to Program Design, 4e32 Programming Example: Election Results (continued)

Java Programming: From Problem Analysis to Program Design, 4e33 Chapter Summary Lists Searching lists –Sequential searching –Binary search Sorting lists –Selection sort –Insertion sort