Upcoming SlideShare
×

# Ch05 Black Jack

1,271 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
1,271
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
24
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Ch05 Black Jack

1. 1. Chapter 11 Sorting and Searching
2. 2. Chapter Objectives <ul><li>Examine the linear search and binary search algorithms </li></ul><ul><li>Examine several (vài) sorting algorithms, including: </li></ul><ul><ul><li>selection sort </li></ul></ul><ul><ul><li>insertion sort </li></ul></ul><ul><ul><li>bubble sort </li></ul></ul><ul><ul><li>quick sort </li></ul></ul><ul><ul><li>merge sort </li></ul></ul><ul><li>Discuss the complexity (phức tạp) of these algorithms </li></ul>
3. 3. Searching <ul><li>Searching is the process of finding a target element among a group of items (the search pool ), or determining that it isn't there </li></ul><ul><li>This requires repetitively comparing the target to candidates in the search pool </li></ul><ul><li>An efficient sort performs no more comparisons than it has to </li></ul><ul><li>The size of the search pool is a factor </li></ul>
4. 4. The Comparable Interface <ul><li>We want to define the algorithms such that they can search any set of objects </li></ul><ul><li>Therefore we will search objects that implement the Comparable interface </li></ul><ul><li>It contains one method, compareTo , which is designed to return an integer that specifies the relationship between two objects: </li></ul><ul><li>obj1.compareTo(obj2) </li></ul><ul><li>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 </li></ul>
5. 5. The Comparable Interface <ul><li>All of the methods presented in this chapter are part of the SortingandSearching class </li></ul><ul><li>This class makes use of the generic type T </li></ul><ul><li>For this class, we make the further distinction (khác biệt) that T extends Comparable </li></ul><ul><li>public class SortingandSearching<T extends Comparable> </li></ul>
6. 6. Linear Search <ul><li>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 </li></ul><ul><li>This approach does not assume the items in the search pool are in any particular order </li></ul><ul><li>We just need to be able to examine each element in turn (in a linear fashion) </li></ul><ul><li>It's fairly easy to understand, but not very efficient </li></ul>
7. 7. FIGURE 11.1 A linear search
8. 8. linearSearch
9. 10. Binary Search <ul><li>If the search pool is sorted, then we can be more efficient than a linear search </li></ul><ul><li>A binary search eliminates large parts of the search pool with each comparison </li></ul><ul><li>Instead of starting the search at one end, we begin in the middle </li></ul><ul><li>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 </li></ul><ul><li>We can then jump to the middle of that half, and continue similarly </li></ul>
10. 11. FIGURE 11.2 A binary search
11. 12. Binary Search <ul><li>For example, find the number 29 in the following sorted list of numbers: </li></ul><ul><li>8 15 22 29 36 54 55 61 70 73 88 </li></ul><ul><li>Compare the target to the middle value 54 </li></ul><ul><li>We now know that if 29 is in the list, it is in the front half of the list </li></ul><ul><li>With one comparison, we've eliminated half of the data </li></ul><ul><li>Then compare to 22, eliminating another quarter of the data, etc. </li></ul>
12. 13. Binary Search <ul><li>A binary search algorithm is often implemented recursively </li></ul><ul><li>Each recursive call searches a smaller portion of the search pool </li></ul><ul><li>The base case of the recursion is running out of viable candidates to search, which means the target is not in the search pool </li></ul><ul><li>At any point there may be two &quot;middle&quot; values, in which case either could be used </li></ul>
13. 14. binarySearch
14. 15. Comparing Search Algorithms <ul><li>On average, a linear search would examine n/2 elements before finding the target </li></ul><ul><li>Therefore, a linear search is O(n) </li></ul><ul><li>The worst case for a binary search is (log 2 n) comparisons </li></ul><ul><li>A binary search is a logarithmic algorithm </li></ul><ul><li>It has a time complexity of O(log 2 n) </li></ul><ul><li>But keep in mind that the search pool must be sorted </li></ul><ul><li>For large n, a binary search is much faster </li></ul>
15. 16. Sorting <ul><li>Sorting is the process of arranging a group of items into a defined order based on particular criteria </li></ul><ul><li>Many sorting algorithms have been designed </li></ul><ul><li>Sequential sorts require approximately n 2 comparisons to sort n elements </li></ul><ul><li>Logarithmic sorts typically require nlog 2 n comparisons to sort n elements </li></ul>
16. 17. Sorting <ul><li>Let's define a generic sorting problem that any of our sorting algorithms could help solve </li></ul><ul><li>As with searching, we must be able to compare one element to another </li></ul>
17. 18. Listing 11.1
18. 19. Listing 11.1 (cont.)
19. 20. Listing 11.2
20. 21. Listing 11.2 (cont.)
21. 22. Selection Sort <ul><li>Selection sort orders a list of values by repetitively putting a particular value into its final position </li></ul><ul><li>More specifically: </li></ul><ul><ul><li>find the smallest value in the list </li></ul></ul><ul><ul><li>switch it with the value in the first position </li></ul></ul><ul><ul><li>find the next smallest value in the list </li></ul></ul><ul><ul><li>switch it with the value in the second position </li></ul></ul><ul><ul><li>repeat until all values are in their proper places </li></ul></ul>
22. 23. FIGURE 11.3 Illustration of selection sort processing
23. 24. selectionSort
24. 25. Insertion Sort <ul><li>Insertion sort orders a list of values by repetitively inserting a particular value into a sorted subset of the list </li></ul><ul><li>More specifically: </li></ul><ul><ul><li>consider the first item to be a sorted sublist of length 1 </li></ul></ul><ul><ul><li>insert the second item into the sorted sublist, shifting the first item if needed </li></ul></ul><ul><ul><li>insert the third item into the sorted sublist, shifting the other items as needed </li></ul></ul><ul><ul><li>repeat until all values have been inserted into their proper positions </li></ul></ul>
25. 26. FIGURE 11.4 Illustration of insertion sort processing
26. 27. insertionSort
27. 28. Bubble Sort <ul><li>Bubble sort orders a list of values by repetitively comparing neighboring elements and swapping their positions if necessary </li></ul><ul><li>More specifically: </li></ul><ul><ul><li>scan the list, exchanging adjacent elements if they are not in relative order; this bubbles the highest value to the top </li></ul></ul><ul><ul><li>scan the list again, bubbling up the second highest value </li></ul></ul><ul><ul><li>repeat until all elements have been placed in their proper order </li></ul></ul>
28. 29. bubbleSort
29. 30. Comparing Sorts <ul><li>We've seen three sorts so far: </li></ul><ul><ul><li>selection sort </li></ul></ul><ul><ul><li>insertion sort </li></ul></ul><ul><ul><li>bubble sort </li></ul></ul><ul><li>They all use nested loops and perform approximately n 2 comparisons </li></ul><ul><li>They are relatively inefficient </li></ul><ul><li>Now we will turn our attention to more efficient sorts </li></ul>
30. 31. Quick Sort <ul><li>Quick sort orders a list of values by partitioning the list around one element, then sorting each partition </li></ul><ul><li>More specifically: </li></ul><ul><ul><li>choose one element in the list to be the partition element </li></ul></ul><ul><ul><li>organize the elements so that all elements less than the partition element are to the left and all greater are to the right </li></ul></ul><ul><ul><li>apply the quick sort algorithm (recursively) to both partitions </li></ul></ul>
31. 32. Quick Sort <ul><li>The choice of the partition element is arbitrary </li></ul><ul><li>For efficiency, it would be nice if the partition element divided the list roughly in half </li></ul><ul><li>The algorithm will work in any case, however </li></ul><ul><li>We will divide the work into two methods: </li></ul><ul><ul><li>quickSort – performs the recursive algorithm </li></ul></ul><ul><ul><li>findPartition – rearranges the elements into two partitions </li></ul></ul>
32. 33. quickSort
33. 34. findPartition
34. 35. findPartition
35. 36. findPartition
36. 37. Merge Sort <ul><li>Merge sort orders a list of values by recursively dividing the list in half until each sub-list has one element, then recombining </li></ul><ul><li>More specifically: </li></ul><ul><ul><li>divide the list into two roughly equal parts </li></ul></ul><ul><ul><li>recursively divide each part in half, continuing until a part contains only one element </li></ul></ul><ul><ul><li>merge the two parts into one sorted list </li></ul></ul><ul><ul><li>continue to merge parts as the recursion unfolds </li></ul></ul>
37. 38. FIGURE 11.5 The decomposition of merge sort
38. 39. FIGURE 11.6 The merge portion of the merge sort algorithm
39. 40. mergeSort
40. 41. mergeSort
41. 42. Efficiency of quickSort and mergeSort <ul><li>Both quickSort and mergeSort use a recursive structure that takes log 2 n processing steps to decompose the original list into lists of length one </li></ul><ul><li>At each step, both algorithms either compare or merge all n elements </li></ul><ul><li>Thus both algorithms are O(nlogn) </li></ul>