1. Trang chủ
  2. » Giáo án - Bài giảng

C++ programming program design including data structure 7th ch18

70 144 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Slide 1

  • Objectives

  • Objectives (cont’d.)

  • Objectives (cont’d.)

  • Introduction

  • Searching and Sorting Algorithms

  • Search Algorithms

  • Sequential Search

  • Sequential Search Analysis

  • Sequential Search Analysis (cont’d.)

  • Binary Search

  • Binary Search (cont’d.)

  • Binary Search (cont’d.)

  • Performance of Binary Search

  • Binary Search Algorithm and the class orderedArrayListType

  • Asymptotic Notation: Big-O Notation

  • Asymptotic Notation: Big-O Notation (cont’d.)

  • Asymptotic Notation: Big-O Notation (cont’d.)

  • Asymptotic Notation: Big-O Notation (cont’d.)

  • Asymptotic Notation: Big-O Notation (cont’d.)

  • Asymptotic Notation: Big-O Notation (cont’d.)

  • Asymptotic Notation: Big-O Notation (cont’d.)

  • Asymptotic Notation: Big-O Notation (cont’d.)

  • Lower Bound on Comparison-Based Search Algorithms

  • Sorting Algorithms

  • Sorting a List: Bubble Sort

  • Sorting a List: Bubble Sort (cont’d.)

  • Sorting a List: Bubble Sort (cont’d.)

  • Analysis: Bubble Sort

  • Bubble Sort Algorithm and the class unorderedArrayListType

  • Selection Sort: Array-Based Lists

  • Analysis: Selection Sort

  • Insertion Sort: Array-Based Lists

  • Insertion Sort: Array-Based Lists (cont’d.)

  • Insertion Sort: Array-Based Lists (cont’d.)

  • Insertion Sort: Array-Based Lists (cont’d.)

  • Insertion Sort: Array-Based Lists (cont’d.)

  • Insertion Sort: Array-Based Lists (cont’d.)

  • Analysis: Insertion Sort

  • Analysis: Insertion Sort (cont’d.)

  • Lower Bound on Comparison-Based Sort Algorithms

  • Lower Bound on Comparison-Based Sort Algorithms (cont’d.)

  • Lower Bound on Comparison-Based Sort Algorithms (cont’d.)

  • Lower Bound on Comparison-Based Sort Algorithms (cont’d.)

  • Lower Bound on Comparison-Based Sort Algorithms (cont’d.)

  • Quick Sort: Array-Based Lists

  • Quick Sort: Array-Based Lists (cont’d.)

  • Quick Sort: Array-Based Lists (cont’d.)

  • Quick Sort: Array-Based Lists (cont’d.)

  • Quick Sort: Array-Based Lists (cont’d.)

  • Quick Sort: Array-Based Lists (cont’d.)

  • Quick Sort: Array-Based Lists (cont’d.)

  • Quick Sort: Array-Based Lists (cont’d.)

  • Quick Sort: Array-Based Lists (cont’d.)

  • Analysis: Quick Sort

  • Merge Sort: Linked List-Based Lists

  • Merge Sort: Linked List-Based Lists (cont’d.)

  • Merge Sort: Linked List-Based Lists (cont’d.)

  • Divide

  • Divide (cont’d.)

  • Merge

  • Merge (cont’d.)

  • Merge (cont’d.)

  • Analysis: Merge Sort

  • Analysis: Merge Sort (cont’d.)

  • Analysis: Merge Sort (cont’d.)

  • Analysis: Merge Sort (cont’d.)

  • Summary

  • Summary (cont’d.)

  • Summary (cont’d.)

Nội dung

Chapter 18: Searching and Sorting Algorithms Objectives In this chapter, you will: • • • • Learn about the various search algorithms Explore how to implement the sequential search algorithm and how it performs Explore how to implement the binary search algorithm and how it performs Learn about the asymptotic notation, Big-O, used in algorithm analysis C++ Programming: Program Design Including Data Structures, Seventh Edition Objectives (cont’d.) • • • • • Become familiar with the lower bound on comparison-based search algorithms Learn about the various sorting algorithms Explore how to implement the bubble sort algorithm and how it performs Become familiar with the performance of the selection sort algorithm Explore how to implement the insertion sort algorithm and how it performs C++ Programming: Program Design Including Data Structures, Seventh Edition Objectives (cont’d.) • • • Become familiar with the lower bound on comparison-based sorting algorithms Explore how to implement the quick sort algorithm and how it performs Explore how to implement the merge sort algorithm and how it performs C++ Programming: Program Design Including Data Structures, Seventh Edition Introduction • Using a search algorithm, you can: – – Determine whether a particular item is in a 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 C++ Programming: Program Design Including Data Structures, Seventh Edition Searching and Sorting Algorithms • Data can be organized with the help of an array or a linked list – – unorderedLinkedList unorderedArrayListType C++ Programming: Program Design Including Data Structures, Seventh Edition Search Algorithms • Key of the item – • Special member that uniquely identifies the item in the data set Key comparison: comparing the key of the search item with the key of an item in the list – Can count the number of key comparisons C++ Programming: Program Design Including Data Structures, Seventh Edition Sequential Search • Sequential search (linear search): – – • Same for both array-based and linked lists Starts at first element and examines each element until a match is found Our implementation uses an iterative approach – Can also be implemented with recursion C++ Programming: Program Design Including Data Structures, Seventh Edition Sequential Search Analysis • Statements before and after the loop are executed only once – • Statements in the while loop repeated several times – • Require very little computer time Execution of the other statements in loop is directly related to outcome of key comparison Speed of a computer does not affect the number of key comparisons required C++ Programming: Program Design Including Data Structures, Seventh Edition Sequential Search Analysis (cont’d.) • • • L: a list of length n If search item (target) is not in the list: n comparisons If the search item is in the list: – – – As first element of L  comparison (best case) As last element of L  n comparisons (worst case) Average number of comparisons: C++ Programming: Program Design Including Data Structures, Seventh Edition 10 Merge Sort: Linked List-Based Lists • Quick sort: O(nlog2n) average case; O(n ) worst case • Merge sort: always O(nlog2n) – Uses the divide-and-conquer technique • • • – Partitions the list into two sublists Sorts the sublists Combines the sublists into one sorted list Differs from quick sort in how list is partitioned • Divides list into two sublists of nearly equal size C++ Programming: Program Design Including Data Structures, Seventh Edition 56 Merge Sort: Linked List-Based Lists (cont’d.) C++ Programming: Program Design Including Data Structures, Seventh Edition 57 Merge Sort: Linked List-Based Lists (cont’d.) • General algorithm: • Uses recursion C++ Programming: Program Design Including Data Structures, Seventh Edition 58 Divide C++ Programming: Program Design Including Data Structures, Seventh Edition 59 Divide (cont’d.) C++ Programming: Program Design Including Data Structures, Seventh Edition 60 Merge • Sorted sublists are merged into a sorted list – – Compare elements of sublists Adjust pointers of nodes with smaller info C++ Programming: Program Design Including Data Structures, Seventh Edition 61 Merge (cont’d.) C++ Programming: Program Design Including Data Structures, Seventh Edition 62 Merge (cont’d.) C++ Programming: Program Design Including Data Structures, Seventh Edition 63 Analysis: Merge Sort • • Suppose that L is a list of n elements, with n > Suppose that n is a power of 2; that is, n = m for some integer m > 0, so that we can divide the list into two sublists, each of size: – m will be the number of recursion levels C++ Programming: Program Design Including Data Structures, Seventh Edition 64 Analysis: Merge Sort (cont’d.) C++ Programming: Program Design Including Data Structures, Seventh Edition 65 Analysis: Merge Sort (cont’d.) • • To merge two sorted lists of size s and t, the maximum number of comparisons is s + t − Function mergeList merges two sorted lists into a sorted list – • This is where the actual comparisons and assignments are done Max # of comparisons at level k of recursion: C++ Programming: Program Design Including Data Structures, Seventh Edition 66 Analysis: Merge Sort (cont’d.) • The maximum number of comparisons at each level of the recursion is O(n) – – Maximum number of comparisons is O(nm), where m = number of levels of recursion Thus, O(nm) ≡ O(n log2n) • W(n): # of key comparisons in worst case • A(n): # of key comparisons in average case C++ Programming: Program Design Including Data Structures, Seventh Edition 67 Summary • On average, a sequential search searches half the list and makes O(n) comparisons – • A binary search requires the list to be sorted – • Not efficient for large lists 2log2n – key comparisons Let f be a function of n: by asymptotic, we mean the study of the function f as n becomes larger and larger without bound C++ Programming: Program Design Including Data Structures, Seventh Edition 68 Summary (cont’d.) • Binary search algorithm is the optimal worst-case algorithm for solving search problems by using the comparison method – • • To construct a search algorithm of the order less than log 2n, it cannot be comparison based Bubble sort: O(n ) key comparisons and item assignments Selection sort: O(n ) key comparisons and O(n) item assignments C++ Programming: Program Design Including Data Structures, Seventh Edition 69 Summary (cont’d.) • • Insertion sort: O(n ) key comparisons and item assignments Both the quick sort and merge sort algorithms sort a list by partitioning it – Quick sort: average number of key comparisons is O(nlog2n); worst case number of key comparisons is O(n ) – Merge sort: number of key comparisons is O(nlog2n) C++ Programming: Program Design Including Data Structures, Seventh Edition 70 ... upper half of the list C++ Programming: Program Design Including Data Structures, Seventh Edition 11 Binary Search (cont’d.) C++ Programming: Program Design Including Data Structures, Seventh Edition... (cont’d.) C++ Programming: Program Design Including Data Structures, Seventh Edition 17 Asymptotic Notation: Big-O Notation (cont’d.) C++ Programming: Program Design Including Data Structures,... all n ≥n0 C++ Programming: Program Design Including Data Structures, Seventh Edition 20 Asymptotic Notation: Big-O Notation (cont’d.) C++ Programming: Program Design Including Data Structures,

Ngày đăng: 06/02/2018, 09:15