html5-img
1 / 37

Chapter 10

Chapter 10. Sorting and Searching. Chapter 10 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.

vicky
Télécharger la présentation

Chapter 10

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 10 Sorting and Searching Introduction to Object-Oriented Programming with Java--Wu

  2. Chapter 10 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. • Describe the heapsort algorithm and show how its performance is superior to the other two algorithms. • Apply basic sorting algorithms to sort an array of objects. Introduction to Object-Oriented Programming with Java--Wu

  3. Searching • When we maintain a collection of data, one of the operations we need is a search routine to locate desired data quickly. • We will use an array of integers to present searching algorithms. Here’s the problem statement: Given a value X, return the index of X in the array, if such X exists. Otherwise, return NOT_FOUND (-1). We assume there are no duplicate entries in the array. • We will count the number of comparisons the algorithms make to analyze their performance. The ideal searching algorithm will make the least possible number of comparisons to locate the desired data. • Two separate performance analyses are normally done, one for successful search and another for unsuccessful search. Introduction to Object-Oriented Programming with Java--Wu

  4. search( 45 ) 4 NOT_FOUND 0 1 2 3 4 5 6 7 8 search( 12 ) 23 17 5 90 12 84 77 44 38 Search Result Unsuccessful Search: Successful Search: number Introduction to Object-Oriented Programming with Java--Wu

  5. 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 } } Linear Search • Search the array from the first to the last position in linear progression. Introduction to Object-Oriented Programming with Java--Wu

  6. Linear Search Performance • We analyze the successful and unsuccessful searches separately. • We count how many times the search value is compared against the array elements. • Successful Search • Best Case – 1 comparison • Worst Case – N comparisons (N – array size) • Unsuccessful Search • Best Case = Worst Case – N comparisons Introduction to Object-Oriented Programming with Java--Wu

  7. 0 1 2 3 4 5 6 7 8 5 12 17 23 38 84 90 44 77 Binary Search • If the array is sorted, then we can apply the binary search technique. number • The basic idea is straightforward. First search the value in the middle position. If X is less than this value, then search the middle of the left half next. If X is greater than this value, then search the middle of the right half next. Continue in this manner. Introduction to Object-Oriented Programming with Java--Wu

  8. 4 0 8 #1 0 1 2 3 4 5 6 7 8 low = mid+1 = 5 5 12 17 23 38 84 90 44 77 high low mid Sequence of Successful Search - 1 search( 44 ) low high mid 38 < 44 Introduction to Object-Oriented Programming with Java--Wu

  9. 0 8 5 8 #2 #1 6 0 1 2 3 4 5 6 7 8 5 12 17 23 38 84 90 44 77 low mid high high = mid-1=5 Sequence of Successful Search - 2 search( 44 ) low high mid 4 44 < 77 Introduction to Object-Oriented Programming with Java--Wu

  10. 5 8 0 8 5 5 #3 #2 #1 0 1 2 3 4 5 6 7 8 5 5 12 17 23 38 84 90 44 77 low high mid Sequence of Successful Search - 3 search( 44 ) low high mid 4 6 Successful Search!! 44 == 44 Introduction to Object-Oriented Programming with Java--Wu

  11. 4 0 8 #1 0 1 2 3 4 5 6 7 8 low = mid+1 = 5 5 12 17 23 38 84 90 44 77 high low mid Sequence of Unsuccessful Search - 1 search( 45 ) low high mid 38 < 45 Introduction to Object-Oriented Programming with Java--Wu

  12. 0 8 5 8 #2 #1 6 0 1 2 3 4 5 6 7 8 5 12 17 23 38 84 90 44 77 low mid high high = mid-1=5 Sequence of Unsuccessful Search - 2 search( 45 ) low high mid 4 45 < 77 Introduction to Object-Oriented Programming with Java--Wu

  13. 0 8 5 5 5 8 #2 #1 #3 0 1 2 3 4 5 6 7 8 5 5 12 17 23 38 84 90 44 77 low high mid low = mid+1 = 6 Sequence of Unsuccessful Search - 3 search( 45 ) low high mid 4 6 44 < 45 Introduction to Object-Oriented Programming with Java--Wu

  14. 5 5 5 8 0 8 6 5 #4 #3 #1 #2 0 1 2 3 4 5 6 7 8 5 12 17 23 38 84 90 44 77 high low no more elements to search Sequence of Unsuccessful Search - 4 search( 45 ) low high mid 4 6 5 Unsuccessful Search low > high Introduction to Object-Oriented Programming with Java--Wu

  15. public int binarySearch ( int[] number, int searchValue ) { int low = 0, high = number.length - 1, mid = (low + high) / 2; while ( low <= high && number[mid] != searchValue ) { if (number[mid] < searchValue) { low = mid + 1; } else { //number[mid] > searchValue high = mid - 1; } mid = (low + high) / 2; //integer division will truncate } if ( low > high) { mid = NOT_FOUND; } return mid; } Binary Search Routine Introduction to Object-Oriented Programming with Java--Wu

  16. Binary Search Performance • Successful Search • Best Case – 1 comparison • Worst Case – log2N comparisons • Unsuccessful Search • Best Case = Worst Case – log2N comparisons • Since the portion of an array to search is cut into half after every comparison, we compute how many times the array can be divided into halves. • After K comparisons, there will be N/2K elements in the list. We solve for K when N/2K = 1, deriving K = log2N. Introduction to Object-Oriented Programming with Java--Wu

  17. Comparing N and log2N Performance Array Size Linear – N Binary – log2N Introduction to Object-Oriented Programming with Java--Wu

  18. Sorting • When we maintain a collection of data, many applications call for rearranging the data in certain order, e.g. arranging Person information in ascending order of age. • We will use an array of integers to present sorting algorithms. Here’s the problem statement: Given an array of N integer values, arrange the values into ascending order. • We will count the number of comparisons the algorithms make to analyze their performance. The ideal sorting algorithm will make the least possible number of comparisons to arrange data in a designated order. • We will compare different sorting algorithms by analyzing their worst-case performance. Introduction to Object-Oriented Programming with Java--Wu

  19. min first 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 exchange 5 23 17 17 23 5 90 90 12 12 44 44 38 38 84 84 77 77 unsorted sorted Selection Sort • Find the smallest element in the list. • Exchange the element in the first position and the smallest element. Now the smallest element is in the first position. • Repeat Step 1 and 2 with the list having one less element (i.e., the smallest element is discarded from further processing). This is the result of one pass. Introduction to Object-Oriented Programming with Java--Wu

  20. Pass # 1 sorted 0 1 2 3 4 5 6 7 8 5 5 5 5 5 12 12 12 17 12 17 17 23 17 23 23 90 90 90 23 17 23 12 38 38 44 44 44 44 44 38 38 38 77 77 84 84 84 84 84 77 77 77 90 90 Selection Sort Passes Result AFTER one pass is completed. 2 3 7 8 Introduction to Object-Oriented Programming with Java--Wu

  21. public void selectionSort( int[] number ) { int startIndex, minIndex, length, temp; length = number.length; for (startIndex = 0; startIndex <= length-2; startIndex++){ //each iteration of the for loop is one pass minIndex = startIndex; //find the smallest in this pass at position minIndex for (i = startIndex+1; i <= length-1; i++) { if (number[i] < number[minIndex]) minIndex = i; } //exchange number[startIndex] and number[minIndex] temp = number[startIndex]; number[startIndex] = number[minIndex]; number[minIndex] = temp; } } Selection Sort Routine Introduction to Object-Oriented Programming with Java--Wu

  22. Selection Sort Performance • We derive the total number of comparisons by counting the number of times the inner loop is executed. • For each execution of the outer loop, the inner loop is executed length – start times. • The variable length is the size of the array. Replacing length with N, the array size, the sum is derived as… Introduction to Object-Oriented Programming with Java--Wu

  23. Bubble Sort • With the selection sort, we make one exchange at the end of one pass. • The bubble sort improves the performance by making more than one exchange during its pass. • By making multiple exchanges, we will be able to move more elements toward their correct positions using the same number of comparisons as the selection sort makes. • The key idea of the bubble sort is to make pairwise comparisons and exchange the positions of the pair if they are out of order. Introduction to Object-Oriented Programming with Java--Wu

  24. 17 23 23 17 5 5 90 90 12 12 44 44 38 38 84 84 77 77 exchange exchange 0 1 2 3 4 5 6 7 8 exchange exchange 17 17 17 17 17 17 5 5 5 5 5 5 23 23 23 23 23 23 12 12 12 12 12 90 44 44 90 12 44 44 90 38 44 38 44 38 38 38 90 38 84 84 84 90 84 84 77 84 90 77 77 77 77 77 exchange exchange exchange One Pass of Bubble Sort ok The largest value 90 is at the end of the list. Introduction to Object-Oriented Programming with Java--Wu

  25. public void bubbleSort( int[] number ) { int temp, bottom, i; boolean exchanged = true; bottom = number.length - 2; while ( exchanged ) { exchanged = false; for (i = 0; i <= bottom; i++) { if (number[i] > number[i+1]) { temp = number[i]; //exchange number[i] = number[i+1]; number[i+1] = temp; exchanged = true; //exchange is made } } bottom--; } } Bubble Sort Routine Introduction to Object-Oriented Programming with Java--Wu

  26. Bubble Sort Performance • In the worst case, the outer while loop is executed N-1 times for carrying out N-1 passes. • For each execution of the outer loop, the inner loop is executed bottom+1 times. The number of comparisons in each successive pass is N-1, N-2, … , 1. Summing these will result in the total number of comparisons. • So the performances of the bubble sort and the selection sort are approximately equivalent. However, on the average, the bubble sort performs much better than the selection sort because it can finish the sorting without doing all N-1 passes. Introduction to Object-Oriented Programming with Java--Wu

  27. Sorting and Searching in Java • Java provides a rich set of utilities, defined in the classes Arrays, for sorting and searching data. • static void sort(int[] a) –Sorts the specified array of ints into ascending numerical order. • static int binarySearch(int[] a, int key) –Searches the specified array of ints for the specified value using the binary search algorithm. • Similar methods for other data types byte, short, char, float and double are also provided. [e.g. binarySearch(float[] a, float key) ] • You even can sort and search any kinds of objects by using Arrays.sort() and Arrays.search(). Introduction to Object-Oriented Programming with Java--Wu

  28. Sorting in Java - Example import java.util.Arrays; class AP10_1 { public static void main(String s[]) { int data[] = {12, 91, 15, 21, 6, 17, 28, 30, 55, 10}; int N = data.length; int i; System.out.print("Data items in original order\n"); for (i = 0; i <= N - 1; i++) System.out.print(data[i]+ " " ); Arrays.sort(data); System.out.print("\nData items in ascending order\n"); for (i = 0; i <= N - 1; i++) System.out.print(data[i]+ " " ); System.out.println(); } } Data items in original order 12 91 15 21 6 17 28 30 55 10 Data items in ascending order 6 10 12 15 17 21 28 30 55 91 Introduction to Object-Oriented Programming with Java--Wu

  29. Searching in Java public static int binarySearch(float[] a, float key) Searches the specified array of floats for the specified value using the binary search algorithm. The array must be sorted (as by the sort method, above) prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found. Parameters: a - the array to be searched. key - the value to be searched for. Returns: index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size(), if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found. Introduction to Object-Oriented Programming with Java--Wu

  30. Searching in Java - Example import java.util.Arrays; class AP10_2 { public static void main(String s[]) { int data[] = {6, 10, 12, 15, 17, 21, 28, 30, 55, 91 }; System.out.print("Searching 12 result : "); System.out.println(Arrays.binarySearch(data, 12)); System.out.print("Searching 30 result : "); System.out.println(Arrays.binarySearch(data, 30)); System.out.print("Searching 91 result : "); System.out.println(Arrays.binarySearch(data, 91)); System.out.print("Searching 8 result : "); System.out.println(Arrays.binarySearch(data, 8)); System.out.print("Searching 123 result : "); System.out.println(Arrays.binarySearch(data, 123)); } } Searching 12 result : 2 Searching 30 result : 7 Searching 91 result : 9 Searching 8 result : -2 Searching 123 result : -11 Introduction to Object-Oriented Programming with Java--Wu

  31. Sorting Objects in Java • Problem Statement Add the sorting capability to the AddressBook class from Chapter 9. The new AddressBook class will include a method that sort Person objects in alphabetical order or in descending order of their ages. • How do we compare Person objects? The following is invalid: Person p1 = …;Person p2 = …; if ( p1 < p2 ) { … } Introduction to Object-Oriented Programming with Java--Wu

  32. Comparable • The sort method needs to know how to compare the objects defined by you so that it can determine the ordering of your objects. • You can let Arrays.sort() to do so by implementing the interface comparable in your class. • The interface Comparable has one method compareTo needed to define. • public int compareTo(Object o) - Compares this object with the specified object o for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. Introduction to Object-Oriented Programming with Java--Wu

  33. Comparable - Example class Person implements Comparable{ String name; int age; public Person(String n, int a) { name = n; age = a; } public String getName() { return name;} public int getAge() { return age;} public int compareTo(Object o) { Person p = (Person) o; return name.compareTo(p.name); // use String.compareTo } } Introduction to Object-Oriented Programming with Java--Wu

  34. Comparable – The main method import java.util.Arrays; class AP10_3 { public static void main(String s[]) { Person data[] = new Person[5]; data[0] = new Person("LAU Tak Wah, Andy", 43); data[1] = new Person("LAI Ming, Leon", 33); data[2] = new Person("Cheung Hok Yau, Jacky", 50); data[3] = new Person("KWOK Fu Shing, Arron", 26); data[4] = new Person("CHAN Tai Man, Johnny", 44); int N = data.length; int i; Arrays.sort(data); System.out.print("Data items in ascending order:\n"); for (i = 0; i <= N - 1; i++) System.out.println(data[i].getName()+" "+data[i].getAge()); } } Data items in ascending order: CHAN Tai Man, Johnny 44 Cheung Hok Yau, Jacky 50 KWOK Fu Shing, Arron 26 LAI Ming, Leon 33 LAU Tak Wah, Andy 43 Introduction to Object-Oriented Programming with Java--Wu

  35. Comparator • What happens if we want to sort the Person objects by age? • You have to modify the CompareTo method to compare the ages rather than the names. • Implementing Comparable can only let you sort your data by one attribute. i.e. You cannot sort by names any more. • You can use another interface called Comparator to perform sorting on more than one attributes. • For each attribute you want to perform the sorting, you need to write one class to implement the interface Comparator which has two methods to be defined by the programmer: • int compare(Object o1, Object o2) Compares its two arguments for order. • boolean equals(Object obj) Indicates whether some other object is "equal to" this Comparator. Introduction to Object-Oriented Programming with Java--Wu

  36. Comparator - Example // class Person same as before import java.util.Comparator; class ByAgeComparator implements Comparator { public int compare(Object o1, Object o2) { Person p1 = (Person) o1; Person p2 = (Person) o2; return p1.getAge() - p2.getAge(); } public boolean equals(Object obj) { return true; // for simplicity – always return true } } Introduction to Object-Oriented Programming with Java--Wu

  37. Comparator - Example class AP10_4 { public static void main(String s[]) { Person data[] = new Person[5]; data[0] = new Person("LAU Tak Wah, Andy", 43); data[1] = new Person("LAI Ming, Leon", 33); data[2] = new Person("Cheung Hok Yau, Jacky", 50); data[3] = new Person("KWOK Fu Shing, Arron", 26); data[4] = new Person("CHAN Tai Man, Johnny", 44); int N = data.length; int i; Arrays.sort(data, new ByAgeComparator()); System.out.print("\nData items in ascending order\n"); for (i = 0; i <= N - 1; i++) System.out.println(data[i].getName()+ " " + data[i].getAge()); } } Introduction to Object-Oriented Programming with Java--Wu

More Related