Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 13: Searching and Sorting

Similar presentations


Presentation on theme: "Chapter 13: Searching and Sorting"— Presentation transcript:

1 Chapter 13: Searching and Sorting

2 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

3 Outline Searching Sorting Analyzing Searching and Sorting Algorithms

4 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

5 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

6 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

7 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

8 13.1 – A linear search start

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

10 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

11 13.1 – A binary search start

12 13.1 – Binary Search For example, find the number 29 in the following sorted list of numbers 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 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

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

15 Outline Searching Sorting Analyzing Searching and Sorting Algorithms

16 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

17 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", " "); players[1] = new Contact ("Andy", "Roddick", " "); players[2] = new Contact ("Maria", "Sharapova", " "); players[3] = new Contact ("Venus", "Williams", " "); players[4] = new Contact ("Lleyton", "Hewitt", " "); players[5] = new Contact ("Eleni", "Daniilidou", " "); players[6] = new Contact ("Serena", "Williams", " "); Sorting.selectionSort(players); for (Comparable player : players) System.out.println (player); }

18 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…)

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

20 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

21 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 is the smallest, exchange 9 and 2. 1 9 6 3 2 Scan right starting with is the smallest, exchange 6 and 3. 1 2 6 3 9 Scan right starting with is the smallest, exchange 6 and 6. 1 2 3 6 9

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

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

24 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

25 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

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

27 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

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

29 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

30 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

31 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

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

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

34 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

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

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

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

38 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…)

39 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];

40 Outline Searching Sorting Analyzing Searching and Sorting Algorithms

41 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

42 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

43 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

44 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

45 13.3 – Comparing Sort Algorithms

46 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


Download ppt "Chapter 13: Searching and Sorting"

Similar presentations


Ads by Google