Presentation is loading. Please wait.

Presentation is loading. Please wait.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11 Sorting and Searching.

Similar presentations


Presentation on theme: "©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11 Sorting and Searching."— Presentation transcript:

1 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11 Sorting and Searching

2 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11 Objectives After you have read and studied this chapter, you should be able to Perform linear and binary search algorithms on small arrays. Determine whether a linear or binary search is more effective for a given situation. Perform selection and bubble sort algorithms.

3 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11 Objectives, cont. After you have read and studied this chapter, you should be able to Describe the heapsort algorithm and show how its performance is superior to that of the other two algorithms. Apply basic sorting algorithms to sort an array of objects, using the Comparator interface. Define the interface to specify common behavior and provide different versions of classes that implement the interface.

4 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.1 Searching Searching an array or other data structure has two possible outcomes: Successful search (value found) Unsuccessful search (value not found) It is possible for one searching algorithm to perform very well for successful searches, and very poorly for unsuccessful searches.

5 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Figure 11.1 Successful and unsuccessful searches.

6 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.1 Searching We will examine two searching algorithms: Linear search Binary search A linear search searches the array from the first index position to the last in a linear progression. This search is also known as a sequential search.

7 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.1 Searching In a linear search, if the number of entries in the array is N, there will be N comparisons for an unsuccessful search. For a successful search, there will be a minimum of 1 and a maximum of N comparisons. On average, there will be N/2 comparisons.

8 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.1 Searching Below is a linear search algorithm. public int linearSearch (int[] number, int searchValue) { int loc = 0; while ( loc < number.length && number[loc] != searchValue) {loc++; } if ( loc == number.length) { //Not found return NOT_FOUND; } else { return loc;//Found, return the position }

9 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.1 Searching If the values in an array are arranged in ascending or descending order, the array is sorted. For a binary search to be applied, the array must be sorted. The idea behind the binary search is to divide the array elements in half each time, until the particular element is located.

10 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.1 Searching If we are searching for the value 77, we first find the middle position of the array. We see that 77 is not in index position [4].

11 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.1 Searching We know the array is sorted. Since 77 is larger than 38, the value must be in the right half of the array. We next search the middle position of the right half of the array, which is index position [6], and locate the value.

12 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 11.2 Effect of one comparison in binary search.

13 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.1 Searching Using the binary search, in the worst case, the number of comparisons is the number of times you can divide the array in half. The maximum number of comparisons K is derived by solving the equation N = 2 K log 2 N = K

14 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.1 Searching Below is a sample binary search method. public int binarySearch (int[] number, int searchValue) { int low = 0, high = number.length - 1, mid = (low + high) / 2; while (low <= high && number[mid] != searchValue ) { System.out.println(mid); if (number[mid] < searchValue) { low = mid + 1; }

15 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.1 Searching else { //number[mid] > searchValue high = mid - 1; } mid = (low + high) / 2; } if ( low > high) { mid = NOT_FOUND; } return mid; }

16 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 11.3 How the unsuccessful search is terminated in the binary search routine.

17 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.2 Sorting Sorting is a technique to arrange elements in some order. We will examine three sorting algorithms: Selection sort Bubble sort Heapsort

18 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.2 Sorting A natural sorting algorithm for a human looks like this: 1.Find the smallest integer in the list. 2.Cross out the number and copy it to a new sorted list. 3.Repeat steps 1 and 2 until all numbers in the list are crossed out.

19 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 11.4 Human sorting algorithm after three numbers are moved to the sorted list.

20 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.2 Sorting Selection sort is somewhat like the human approach. It finds the smallest number in a given list and moves it to the correct position in the list.

21 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.2 Sorting Selection sort is comprised of sorting passes. We locate the smallest element in the array and set the index min to point to this element. We then exchange number[start] and number[min]. After the first pass, the smallest element is moved to the correct position. We increase the value of start by 1 and execute the second pass.

22 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 11.5 Effect of executing the first pass in the selection sort.

23 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.2 Sorting We start the first pass with start = 0 and end the last pass with start = N-2.

24 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 11.6 Eight passes to sort the sample array of nine elements.

25 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.2 Sorting In selection sort, we make one data exchange per pass. In bubble sort, we make pairwise comparisons and exchange the pair if they are out of order.

26 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 11.7 Effect of executing the first pass in the bubble sort.

27 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.2 Sorting In the worst case, bubble sort will make N-1 passes, so its worst-case performance is the same as that of selection sort. Bubble sort exhibits two properties: After one pass through the array, the largest element will be at the end of the array. During one pass, if no pair of consecutive entries is out of order, then the array is sorted.

28 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.2 Sorting On average, we expect the bubble sort to finish sorting sooner than selection sort because there are more data movements for the same number of comparisons.

29 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort The heapsort algorithm is generally much more efficient than either the bubble sort or the selection sort. The heapsort algorithm uses a special data structure called a heap.

30 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort A heap consists of nodes, which contain data values, and edges, which link the nodes. The topmost node is called the root node of a heap. Nodes in a heap are indexed 0, 1, 2, and so on in top-to-bottom, left-to-right order.

31 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 11.8 A sample heap that includes nine nodes.

32 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort A node in a heap has zero, one, or two children. The children of a node are distinguished as the node’s left or right children.

33 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort A heap must satisfy two constraints: Structural constraint: Nodes in a heap with N nodes must occupy the positions numbered 0, 1,...,N-1. Value relationship constraint: A value stored in a node must be larger than the maximum of the values stored in its left and right children.

34 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 11.9 Sample heaps and nonheaps that violate the structural constraint.

35 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 11.10 Sample heaps and nonheaps that violate the value relationship constraint. Violations are indicated by blue ellipses.

36 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort Heapsort is carried out in two phases: Construction phase: Construct a heap given N elements. Extraction phase: Pull out the value in the root successively, creating a new heap with one less element after each extraction step.

37 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort If every node in the heap satisfies the value relationship constraint, then the value in the root node is the largest value in the heap. We then remove that value, and reconstruct a heap by moving the last element to the root position.

38 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort We examine each node to confirm that both the structural and value relationship constraints are met. If the value relationship constraint is not met, we swap values until the relationship is satisfied. This swapping process is called a rebuild step.

39 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 11.11 One rebuild step after the value 90 is pulled out of the heap. The net result of a single rebuild step is a new heap that contains one fewer element.

40 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort In a heap of N elements, we can sort the N elements by performing the rebuild steps N-1 times. The array for the sorted list is filled from the end.

41 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 11.12 Eight rebuild steps to sort a heap with nine elements.

42 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort We will examine the construction phase using the following unsorted numbers: 23, 17, 5, 90, 12, 44, 38, 84, 77 Assigning the numbers to a heap structure in ascending index order produces a heap that violates the value relationship constraint. The construction phase eliminates these violations.

43 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 11.13 A heap structure with given numbers assigned in ascending index order.

44 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort The rebuild step is critical for constructing a heap. We build a heap in bottom-up fashion, starting small, and gradually building larger until all elements are contained in the heap.

45 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 11.14 Sequence of rebuild steps applied in the construction phase.

46 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort The array is one of the most effective data structures we can use to implement the heapsort in Java. With an array implementation, we can easily locate a given node’s left and right children. A node with index I has its left child at index 2I + 1 and its right child at index 2I + 2.

47 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 11.15 A sample heap and the corresponding array implementation.

48 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort /* Chapter 11 Sample Program: Heap algorithm. File: Heap.java */ /** * Basic class for implementing the heapsort algorithm. * This class works only for integers. */ public class Heap { private int[ ] heap; private int[ ] sortedList; public Heap( ) { }

49 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort //------------------------------------------------- // Public Methods: //------------------------------------------------ public void setData( int[ ] data ) { heap = new int[ data.length ]; sortedList = new int[ data.length ]; for (int i = 0; i < data.length; i++ ) { heap[i] = data[i]; } public int[] sort( ) { construct( ); //construction phase: construct the heap extract( ); //extraction phase: extract root nodes return sortedList; }

50 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort //------------------------------------------------- // Private Methods: //------------------------------------------------ private void construct( ) { int current, maxChildIndex; boolean done; for (int i = (heap.length-2) / 2; i >= 0; i--) { current = i; done = false; while ( !done ) { if ( 2*current+1 > heap.length-1 ) { //current node has no children, so stop done = true; } else { //current node has at least one child, //get the index of larger child maxChildIndex = maxChild( current, heap.length-1 );

51 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort if ( heap[current] < heap[maxChildIndex] ) { swap( current, maxChildIndex ); current = maxChildIndex; } else { done = true; } assert isValidHeap(heap, i, heap.length-1): "Error: Construction phase is not working " + "correctly"; } //testPrint(heap.length); //TEMP } private boolean isValidHeap(int[] heap, int start, int end) { for (int i = 0; i < end/ 2; i++) { if (heap[i] < Math.max(heap[2*i+1], heap[2*i+2])){ return false; } return true; }

52 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort private void extract( ) { int current, maxChildIndex; boolean done; for (int size = heap.length-1; size >= 0; size--) { //remove the root node data sortedList[size] = heap[ 0 ]; //move the last node to the root heap[ 0 ] = heap[size]; //rebuild current = 0; done = false; while ( !done ) { if ( 2*current+1 > size ) { //current node has no children, so stop done = true; } else { //current node has at least one child, //get the index of larger child maxChildIndex = maxChild( current, size );

53 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort if ( heap[current] < heap[maxChildIndex] ) { swap( current, maxChildIndex ); current = maxChildIndex; } else { //value relationship constraint //is satisfied, so stop done = true; } assert isValidHeap(heap, 0, size): "Error: Extraction phase is not working ” + “correctly"; //testPrint( size ); //TEMP }

54 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort private int maxChild( int location, int end ) { int result, leftChildIndex, rightChildIndex; rightChildIndex = 2*location + 2; leftChildIndex = 2*location + 1; //Precondition: node at 'location' has at least //one child assert leftChildIndex <= end: "Error: node at position " + location + "has no children."; if ( rightChildIndex <= end && heap[leftChildIndex] < heap[rightChildIndex]) { result = rightChildIndex; } else { result = leftChildIndex; } return result; }

55 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort private void swap ( int loc1, int loc2 ) { int temp; temp = heap[loc1]; heap[loc1] = heap[loc2]; heap[loc2] = temp; } private void testPrint( int limit ) { for (int i = 0; i < limit; i++) { System.out.print(" " + heap[i]); } System.out.println( " " ); }

56 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort Bubble sort and selection sort perform roughly N 2 comparisons for sorting N elements. Heapsort performs roughly 1.5N log 2 N comparisons for sorting N elements.

57 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 11.3 Heapsort The level of a node in a heap refers to the number of nodes in the path from the root to the node in question. The depth of a heap is defined to be the largest level of the nodes in the heap.

58 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 11.16 A sample heap with depth = 4, which is defined to be the largest level of all nodes in a heap.


Download ppt "©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11 Sorting and Searching."

Similar presentations


Ads by Google