1 / 109

C++ Programming: Program Design Including Data Structures, Third Edition

C++ Programming: Program Design Including Data Structures, Third Edition. Chapter 19: Searching and Sorting Algorithms. Objectives. In this chapter you will: Learn the various search algorithms Explore how to implement the sequential and binary search algorithms

Télécharger la présentation

C++ Programming: Program Design Including Data Structures, Third Edition

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. C++ Programming: Program Design Including Data Structures, Third Edition Chapter 19: Searching and Sorting Algorithms

  2. Objectives In this chapter you will: • Learn the various search algorithms • Explore how to implement the sequential and binary search algorithms • Discover how the sequential and binary search algorithms perform • Become aware of the lower bound on comparison-based search algorithms • Learn the various sorting algorithms • Explore how to implement the bubble, selection, insertion, quick, and merge sorting algorithms • Discover how the sorting algorithms discussed in this chapter perform

  3. The most important operation that can be performed on a list is the search algorithm. Using the search algorithm, you can do the following: • Determine whether a particular item is in the list. • If the data is specially organized (for example, sorted), find the location in the list where a new item can be inserted. • Find the location of an item to be deleted.

  4. The searching and sorting algorithms that we describe are generic. • Because searching and sorting require comparisons of data, the algorithms should work on the type of data that provide appropriate functions to compare data items. • Data can be organized with the help of an array or a linked list. • You can create an array of data items or you can use the classunorderedLinkedList to organize data. • The algorithms that we describe should work on either organization. • We write function templates to implement a particular algorithm. • All algorithms described in this chapter, with the exception of the merge sort algorithms, are for array-based lists. • We show how to use the searching and sorting algorithms on objects of the classunorderedArrayListType. • We place all the array-based searching and sorting functions in the header file searchSortAlgorithms.h. • If you need to use a particular searching and/or sorting function designed in this chapter, your program can include this header file and use that function.

  5. Associated with each item in a data set is a special member that uniquely identifies the item in the data set. • This unique member of the item is called the key of the item. • The keys of the items in the data set are used in such operations as searching, sorting, inserting, and deleting. • When analyzing searching and sorting algorithms, the key comparisons refer to comparing the key of the search item with the key of an item in the list. • The number of key comparisons refers to the number of times the key of the search item (in algorithms such as searching and sorting) is compared with the keys of the items in the list.

  6. Sequential search does not require the list elements to be in any particular order.

  7. The statements before and after the loop are executed only once, and hence require very little computer time. • The statements in the for loop are the ones that are repeated several times. For each iteration of the loop, the search item is compared with an element in the list, and a few other statements are executed, including some other comparisons. • The loop terminates as soon as the search item is found in the list. • Execution of the other statements in the loop is directly related to the outcome of the key comparison. • Different programmers might implement the same algorithm differently, although the number of key comparisons would typically be the same. • The speed of a computer can also easily affect the time an algorithm takes to perform, but it of course does not affect the number of key comparisons required. • Therefore, when analyzing a search algorithm, we count the number of key comparisons because this number gives us the most useful information.

  8. Suppose that L is a list of length n. • If the search item is not in the list, we then compare the search item with every element in the list, making n comparisons. This is an unsuccessful case. • Suppose that the search item is in the list. • The number of key comparisons depends on where in the list the search item is located. • If the search item is the first element of L, we make only one key comparison. This is the best case. • On the other hand, if the search item is the last element in the list, the algorithm makes n comparisons. This is the worst case. • To determine the average number of comparisons in the successful case of the sequential search algorithm: • Consider all possible cases. • Find the number of comparisons for each case. • Add the number of comparisons and divide by the number of cases.

  9. If the search item, called the target, is the first element in the list, one comparison is required. If the target is the second element in the list, two comparisons are required. Similarly, if the target is the kth element in the list, k comparisons are required. We assume that the target can be any element in the list • The following expression gives the average number of comparisons: • This expression shows that on average, a successful sequential search searches half the list. • If the list size is 1,000,000, on average, the sequential search makes 500,000 comparisons. • The sequential search is not efficient for large lists.

  10. Binary search can be applied to sorted lists • Uses the “divide and conquer” technique • Compare search item to middle element • If search item is less than middle element, restrict the search to the lower half of the list • Otherwise search the upper half of the list

  11. Search item = 89

  12. Search item = 34

  13. Search item = 22

  14. Binary Search (continued) • Every iteration cuts size of search list in half • If list L has 1000 items • At most 11 iterations needed to determine if an item x is in list • Every iteration makes 2 key (item) comparisons • Binary search makes at most 22 key comparisons to determine if x is in L • Sequential search makes 500 key comparisons (average) if x is in L for the same size list

  15. Suppose that L is a sorted list of size n. • Suppose that n is a power of 2, that is, n = 2m, for some nonnegative integer m. • After each iteration of the for loop, about half the elements are left to search. • For example, after the first iteration the search sublist is of the size about n /2 = 2m1. • It is easy to see that the maximum number of the iteration of the for loop is about m + 1. Also, m = log2n. • Each iteration makes 2 key comparisons. • The maximum number of comparisons to determine whether an element x is in L is 2(m + 1) = 2(log2n + 1) = 2log2n + 2.

  16. Just as a problem is analyzed before writing the algorithm and the computer program, after an algorithm is designed it should also be analyzed. • There are various ways to design a particular algorithm. • Certain algorithms take very little computer time to execute, while others take a considerable amount of time.

  17. Lines 1 to 6 each have 1 operation, << or >>. • Line 7 has 1 operation, >=. • Either Line 8 or Line 9 executes; each has 1 operation. • There are 3 operations, <<, in Line 11. • The total number of operations executed in this code is 6 + 1 + 1 + 3 = 11.

  18. This algorithm has 5 operations (Lines 1 through 5) before the while loop. Similarly, there are 9 or 8 operations after the while loop, depending on whether Line 11 or Line 13 executes. • Line 5 has 1 operation, and 4 operations within the while loop (Lines 6 through 8). • Lines 5 through 8 have 5 operations. If the while loop executes 10 times, these 5 operations execute 10 times, plus one extra operation is executed at Line 5 to terminate the loop. Therefore, the number of operations executed from Lines 5 through 8 is 51. • If the while loop executes 10 times, the total number of operations executed is: 5 × 10 + 1 + 5 + 9 or 5 × 10 + 1 + 5 + 8 that is, 5 × 10+ 15 or 5 × 10 + 14 • When the while loop executes n times: If the while loop executes n times, the number of operations executed is: 5n + 15 or 5n + 14 • In these expressions, for very large values of n, the term 5n becomes the dominating term and the terms 15 and 14 become negligible.

  19. Table 19-4 shows how certain functions grow as the parameter n grows. • Suppose that the problem size is doubled. • If the number of basic operations is a function of f(n) = n2; the number of basic operations is quadrupled. • If the number of basic operations is a function of f(n) = 2n, then the number of basic operations is squared. • However, if the number of operations is a function of f(n) = log2n, the change in the number of basic operations is insignificant.

  20. Sorting a List: Bubble Sort • Suppose list[0]...list[n - 1] is a list of n elements, indexed 0 to n – 1 • Bubble sort algorithm: • In a series of n - 1 iterations, compare successive elements, list[index] and list[index + 1] • If list[index] is greater than list[index + 1], then swap them

  21. Suppose a list L of length n is to be sorted using bubble sort. • Consider the function bubbleSort. • This function contains nested for loops. • The outer loop executes n – 1 times. • For each iteration of the outer loop, the inner loop executes a certain number of times. Let us consider the first iteration of the outer loop. • During the first iteration of the outer loop, the number of iterations of the inner loop is n – 1. So there are n – 1 comparisons. • During the second iteration of the outer loop, the number of iterations of the inner loop is n – 2, and so on. Thus, the total number of comparisons is

  22. In the worst case, the number of assignments is

  23. template <class elemType> void unorderedArrayListType<elemType>::sort() { bubbleSort(list, length); }

  24. Selection sort: rearrange list by selecting an element and moving it to its proper position • Find the smallest (or largest) element and move it to the beginning (end) of the list

  25. Selection Sort (continued) • On successive passes, locate the smallest item in the list starting from the next element

  26. Suppose that a list L of length n is to be sorted using the selection sort algorithm. • The function swap does three item assignments and is executed n − 1 times. • The number of item assignments is 3(n − 1) = O(n). • The key comparisons are made by the function minLocation. • For a list of length k, the function minLocation makes k − 1 key comparisons. Also, the function minLocation is executed n − 1 times (by the function selectionSort). • The first time, the function minLocation finds the index of the smallest key item in the entire list and therefore makes n − 1 comparisons. • The second time, the function minLocation finds the index of the smallest element in the sublist of length n − 1 and makes n − 2 comparisons, and so on. • The number of key comparisons is as follows:

  27. If n = 1000, the number of key comparisons the selection sort algorithm makes is

  28. The insertion sort algorithm sorts the list by moving each element to its proper place.

  29. Let L be a list of length n. • The for loop executes n – 1 times. • In the best case, when the list is already sorted, for each iteration of the for loop, the if statement evaluates to false, so there are n – 1 key comparisons. • In the best case, the number of key comparisons is n – 1 = O(n). • Let us consider the worst case. In this case, for each iteration of the for loop, the if statement evaluates to true. Moreover, in the worst case, for each iteration of the for loop, the do…while loop executes firstOutOfOrder – 1 times. It follows that in the worst case, the number of key comparisons is: • 1 + 2 + … + (n – 1) = n(n – 1 ) / 2 = O(n2).

More Related