Chapter 13: 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

Garfield AP Computer Science
Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) Sorting a list of test.
Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
CHAPTER 11 Sorting.
CHAPTER 11 Searching. 2 Introduction Searching is the process of finding a target element among a group of items (the search pool), or determining that.
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
Chapter 18 Searching and Sorting
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Do Now Take out ch6 test answers – be ready to hand it in Pick a leader in each group of up to 3 students; Leader will retrieve a whiteboard, marker, and.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Computer Science Searching & Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 8: Sorting and Searching Java Software Structures: Designing.
Chapter 9 – Part 2 Polymorphism: Sorting & Searching.
Chapter 9 Searching and Sorting
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
COS 312 DAY 24 Tony Gauvin. Ch 1 -2 Agenda Questions? Last Capstone Progress reports over due Assignment 6 corrected – 2 A, 1 B, 1 C, 1 F and 2 MIA’s.
Searching and Sorting Chapter 18 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Sort Algorithms.
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.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Searching and Sorting Searching algorithms with simple arrays
Prof. U V THETE Dept. of Computer Science YMA
Searching and Sorting Algorithms
Sorting Mr. Jacobs.
Chapter 9 Polymorphism.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Ch 14: Search and Sorting Yonglei Tao.
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Quicksort 1.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Sorts.
Advanced Sorting Methods: Shellsort
SORTING AND SEARCHING.
Unit-2 Divide and Conquer
Data Structures and Algorithms
Outline Late Binding Polymorphism via Inheritance
C++ Plus Data Structures
Searching and Sorting 1-D Arrays
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
24 Searching and Sorting.
Sorting Chapter 8 CS 225.
Chapter 4.
Sorting Chapter 8.
Algorithm Efficiency and Sorting
Sorting Chapter 10.
Quicksort.
Searching/Sorting/Searching
Sorting and Searching -- Introduction
Algorithm Efficiency and Sorting
Module 8 – Searching & Sorting Algorithms
Advanced Sorting Methods: Shellsort
Chapter 18 Searching and Sorting
Presentation transcript:

Chapter 13: Searching and Sorting

Searching and Sorting Two common tasks in software development are searching for a particular element in a group and sorting a group of elements Chapter 13 focuses on the linear and binary search algorithms several sorting algorithms, including: selection sort insertion sort bubble sort quick sort merge sort the complexity of the search and sort algorithms

Outline Searching Sorting Analyzing Searching and Sorting Algorithms

13.1 – Searching Searching is the process of finding a target element among a group of items (the search pool), or determining that it isn't there This requires repetitively comparing the target to candidates in the search pool An efficient search performs no more comparisons than it has to The size of the search pool is a factor

13.1 – The Comparable Interface We want to define the algorithms such that they can search any set of objects, therefore we will search objects that implement the Comparable interface It contains one method, compareTo, which is designed to return an integer that specifies the relationship between two objects: obj1.compareTo(obj2) This call returns a number less than, equal to, or greater than 0 if obj1 is less than, equal to, or greater than obj2, respectively

13.1 – The Comparable Interface All of the methods presented in this chapter are part of the Searching and Sorting classes These methods operate on arrays of Comparable objects

13.1 – Linear Search A linear search simply examines each item in the search pool, one at a time, until either the target is found or until the pool is exhausted This approach does not assume the items in the search pool are in any particular order We just need to be able to examine each element in turn (in a linear fashion) It's fairly easy to understand, but not very efficient

13.1 – A linear search start

13.1 – The linearSearch Algorithm //----------------------------------------------------------------- // Searches the specified array of objects using a linear search // algorithm. Returns null if the target is not found. public static Comparable linearSearch (Comparable[] data, Comparable target) { Comparable result = null; int index = 0; while (result == null && index < data.length) if (data[index].compareTo(target) == 0) result = data[index]; index++; } return result;

13.1 – Binary Search If the search pool is sorted, then we can be more efficient than a linear search A binary search eliminates large parts of the search pool with each comparison Instead of starting the search at one end, we begin in the middle If the target isn't found, we know that if it is in the pool at all, it is in one half or the other We can then jump to the middle of that half, and continue similarly

13.1 – A binary search start

13.1 – Binary Search For example, find the number 29 in the following sorted list of numbers 8 15 22 29 36 54 55 61 70 73 88 Compare the target to the middle value 54 We now know that if 29 is in the list, it is in the front half of the list With one comparison, we’ve eliminated half of the data Then compare to 22, eliminating another quarter of the data, etc.

13.1 – Binary Search A binary search algorithm is often implemented recursively Each recursive call searches a smaller portion of the search pool The base case of the recursion is running out of viable candidates to search, which means the target is not in the search pool At any point there may be two “middle” values, in which case either could be used

13.1 – The binarySearch Algorithm //----------------------------------------------------------------- // Searches the specified array of objects using a binary search // algorithm. Returns null if the target is not found. public static Comparable binarySearch (Comparable[] data, Comparable target) { Comparable result = null; int first = 0, last = data.length-1, mid; while (result == null && first <= last) mid = (first + last) / 2; // determine midpoint if (data[mid].compareTo(target) == 0) result = data[mid]; else if (data[mid].compareTo(target) > 0) last = mid - 1; first = mid + 1; } return result;

Outline Searching Sorting Analyzing Searching and Sorting Algorithms

13.2 – Sorting Sorting is the process of arranging a group of items into a defined order based on particular criteria Many sorting algorithms have been designed Sequential sorts require approximately n2 comparisons to sort n elements Logarithmic sorts typically require nlog2n comparisons to sort n elements Let's define a generic sorting problem that any of our sorting algorithms could help solve As with searching, we must be able to compare one element to another

13.2 – SortPlayerList.java //******************************************************************** // SortPlayerList.java Java Foundations // // Demonstrates a selection sort of Comparable objects. public class SortPlayerList { //----------------------------------------------------------------- // Creates an array of Contact objects, sorts them, then prints // them. public static void main (String[] args) Contact[] players = new Contact[7]; players[0] = new Contact ("Rodger", "Federer", "610-555-7384"); players[1] = new Contact ("Andy", "Roddick", "215-555-3827"); players[2] = new Contact ("Maria", "Sharapova", "733-555-2969"); players[3] = new Contact ("Venus", "Williams", "663-555-3984"); players[4] = new Contact ("Lleyton", "Hewitt", "464-555-3489"); players[5] = new Contact ("Eleni", "Daniilidou", "322-555-2284"); players[6] = new Contact ("Serena", "Williams", "243-555-2837"); Sorting.selectionSort(players); for (Comparable player : players) System.out.println (player); }

13.2 – Contact.java //******************************************************************** // Contact.java Java Foundations // // Represents a phone contact that implements Comparable. public class Contact implements Comparable { private String firstName, lastName, phone; //----------------------------------------------------------------- // Sets up this contact with the specified information. public Contact (String first, String last, String telephone) firstName = first; lastName = last; phone = telephone; } (more…)

13.2 – Contact.java //----------------------------------------------------------------- // Returns a string representation of this contact. public String toString () { return lastName + ", " + firstName + ": " + phone; } // Uses both last and first names to determine lexical ordering. public int compareTo (Object other) int result; if (lastName.equals(((Contact)other).lastName)) result = firstName.compareTo(((Contact)other).firstName); else result = lastName.compareTo(((Contact)other).lastName); return result;

13.2 – Selection Sort Selection sort orders a list of values by repetitively putting a particular value into its final position More specifically find the smallest value in the list switch it with the value in the first position find the next smallest value in the list switch it with the value in the second position repeat until all values are in their proper places

13.2 – Illustration of selection sort processing Scan right starting with 3. 1 is the smallest, exchange 1 and 3. 3 9 6 1 2 Scan right starting with 9. 2 is the smallest, exchange 9 and 2. 1 9 6 3 2 Scan right starting with 6. 3 is the smallest, exchange 6 and 3. 1 2 6 3 9 Scan right starting with 6. 6 is the smallest, exchange 6 and 6. 1 2 3 6 9

13.2 – The selectionSort Algorithm //----------------------------------------------------------------- // Sorts the specified array of integers using the selection // sort algorithm. public static void selectionSort (Comparable[] data) { int min; for (int index = 0; index < data.length-1; index++) min = index; for (int scan = index+1; scan < data.length; scan++) if (data[scan].compareTo(data[min]) < 0) min = scan; swap (data, min, index); }

13.2 – The swap Supporting Method //----------------------------------------------------------------- // Swaps two elements in the specified array. private static void swap (Comparable[] data, int index1, int index2) { Comparable temp = data[index1]; data[index1] = data[index2]; data[index2] = temp; }

13.2 – Insertion Sort Insertion sort orders a list of values by repetitively inserting a particular value into a sorted subset of the list More specifically consider the first item to be a sorted sublist of length 1 insert the second item into the sorted sublist, shifting the first item if needed insert the third item into the sorted sublist, shifting the other items as needed repeat until all values have been inserted into their proper positions

13.2 – Illustration of insertion sort processing 3 is sorted. Shift nothing, insert 9. 3 9 6 1 2 3 and 9 are sorted. Shift 9 to the right, insert 6. 3 9 6 1 2 3, 6, and 9 are sorted. Shift 9, 6, and 3 to the right, insert 1. 3 6 9 1 2 1, 3, 6, and 9 are sorted. Shift 9, 6, and 3 to the right, insert 2. 1 3 6 9 2 1 2 3 6 9

13.2 – The insertionSort Algorithm //----------------------------------------------------------------- // Sorts the specified array of objects using an insertion // sort algorithm. public static void insertionSort (Comparable[] data) { for (int index = 1; index < data.length; index++) Comparable key = data[index]; int position = index; // Shift larger values to the right while (position > 0 && data[position-1].compareTo(key) > 0) data[position] = data[position-1]; position--; } data[position] = key;

13.2 – Bubble Sort Bubble sort orders a list of values by repetitively comparing neighboring elements and swapping their positions if necessary More specifically scan the list, exchanging adjacent elements if they are not in relative order; this bubbles the highest value to the top scan the list again, bubbling up the second highest value repeat until all elements have been placed in their proper order

13.2 – The bubbleSort Algorithm //----------------------------------------------------------------- // Sorts the specified array of objects using a bubble sort // algorithm. public static void bubbleSort (Comparable[] data) { int position, scan; for (position = data.length - 1; position >= 0; position--) for (scan = 0; scan <= position - 1; scan++) if (data[scan].compareTo(data[scan+1]) > 0) swap (data, scan, scan+1); }

13.2 – Comparing Sorts We have seen three sorts so far selection sort insertion sort bubble sort They are relatively inefficient - all are O(n2) More detail about the analysis later First let's look at more efficient sort algorithms

13.2 – Quick Sort Quick sort orders a list of values by partitioning the list around one element, then sorting each partition More specifically choose one element in the list to be the partition element organize the elements so that all elements less than the partition element are to the left and all greater are to the right apply the quick sort algorithm (recursively) to both partitions

13.2 – Quick Sort The choice of the partition element is arbitrary For efficiency, it would be nice if the partition element divided the list roughly in half The algorithm will work in any case, however We will divide the work into two methods quickSort – performs the recursive algorithm findPartition – rearranges the elements into two partitions

13.2 – The quickSort Algorithm //----------------------------------------------------------------- // Sorts the specified array of objects using the quick sort // algorithm. public static void quickSort (Comparable[] data, int min, int max) { int pivot; if (min < max) pivot = partition (data, min, max); // make partitions quickSort(data, min, pivot-1); // sort left partition quickSort(data, pivot+1, max); // sort right partition }

13.2 – The partition Algorithm //----------------------------------------------------------------- // Creates the partitions needed for quick sort. private static int partition (Comparable[] data, int min, int max) { // Use first element as the partition value Comparable partitionValue = data[min]; int left = min; int right = max; while (left < right) // Search for an element that is > the partition element while (data[left].compareTo(partitionValue) <= 0 && left < right) left++; // Search for an element that is < the partitionelement while (data[right].compareTo(partitionValue) > 0) right--; if (left < right) swap(data, left, right); } // Move the partition element to its final position swap (data, min, right); return right;

13.2 – Merge Sort Merge sort orders a list of values by recursively dividing the list in half until each sub-list has one element, then recombining More specifically divide the list into two roughly equal parts recursively divide each part in half, continuing until a part contains only one element merge the two parts into one sorted list continue to merge parts as the recursion unfolds

13.2 – The decomposition of merge sort 90 65 7 305 120 110 8 90 65 7 305 120 110 8 90 65 7 305 120 110 8 90 65 7 305 120 110 8

13.2 – The merge portion of merge sort 65 7 90 305 120 110 8 7 65 90 120 305 8 110 8 110 120 305 7 65 90 7 8 65 90 110 120 305

13.2 – The mergeSort Algorithm //----------------------------------------------------------------- // Sorts the specified array of objects using the merge sort // algorithm. public static void mergeSort (Comparable[] data, int min, int max) { if (min < max) int mid = (min + max) / 2; mergeSort (data, min, mid); mergeSort (data, mid+1, max); merge (data, min, mid, max); }

13.2 – The merge Algorithm //----------------------------------------------------------------- // Sorts the specified array of objects using the merge sort algorithm. public static void merge (Comparable[] data, int first, int mid, int last) { Comparable[] temp = new Comparable[data.length]; int first1 = first, last1 = mid; // endpoints of first subarray int first2 = mid+1, last2 = last; // endpoints of second subarray int index = first1; // next index open in temp array // Copy smaller item from each subarray into temp until one // of the subarrays is exhausted while (first1 <= last1 && first2 <= last2) if (data[first1].compareTo(data[first2]) < 0) temp[index] = data[first1]; first1++; } else temp[index] = data[first2]; first2++; index++; (more…)

13.2 – The merge Algorithm (cont.) // Copy remaining elements from first subarray, if any while (first1 <= last1) { temp[index] = data[first1]; first1++; index++; } // Copy remaining elements from second subarray, if any while (first2 <= last2) temp[index] = data[first2]; first2++; // Copy merged data into original array for (index = first; index <= last; index++) data[index] = temp[index];

Outline Searching Sorting Analyzing Searching and Sorting Algorithms

13.3 – Analyzing Searching and Sorting Algorithms Consider the best-case and worst-case situations for algorithm For some problems (dishwashing or Towers of Hanoi) there is no best or worse case Thinking through best- and worst-case situations helps us define the expected-case (or average-case) situation

13.3 – Comparing Search Algorithms Linear search best-case would be to find the target as the first element in the group worst-case would examine all n elements and not find the target expected-case would examine n/2 elements before finding the target therefore, a linear search is O(n) Binary search best-case would be to find the target in one comparison worst-case is (log2n) comparisons expected-case is (log2n)/2 comparisons binary search has a time complexity of O(log2n) but keep in mind that the search pool must be sorted For large n, a binary search is much faster

13.3 – Comparing Sort Algorithms Selection sort, insertion sort, bubble sort each solve the problem with a different technique all three algorithms use two loops, nested to arrange the elements in order each approach leads to O(n2) they are the quadratic sorts

13.3 – Comparing Sort Algorithms Quick sort the partition element chosen each time will divide the elements in half, so that each recursive call will operate on about half of the current data the act of partitioning the elements at each level requires one pass through the data, effort to partition is O(n) effort to sort the entire array is O(n log n) Merge sort analysis is similar to quick sort effort for merge sort is O(n log n) in best-, worst-, and expected-case

13.3 – Comparing Sort Algorithms

Chapter 13 – Summary Chapter 13 focused on examining the linear and binary search algorithms examine several sorting algorithms, including: selection sort insertion sort bubble sort quick sort merge sort exploring the complexity of the search and sort algorithms