C++ Chapter 18 Quiz
Total Number of Comparisons for Bubble Sort Algorithm
(1/2)n^2 - (1/2)n = O(n^2)
Number of key comparisons for selection sort:
(n(n-1))/2 = O(n^2)
•Average number of comparisons for sequential search:
(n+1)/2
Steps of insertion Sort:
1. Figure out where the first unordered element is 2. Compare the first unordered element to all the elements that are sorted 3. Insert the unsorted element into the sorted list section where it needs to be 4. Repeat
Max number of comparisons for binary search algorithm
2log2(n+2)
How does merge sort differ from quick sort?
Merge Sort Divides list into two sublists of nearly equal size
Number of comparisons for selection sort:
O(n^2)
Worst case number of comparisons for Quick Sort
O(n^2)
Average number of comparisons for Quick Sort
O(nlog2(n)
Worst Case number of comparisons in Merge Sort
O(nlog2(n))
Binary Tree:
a comparison tree where each comparison has two outcomes
•Comparison tree:
graph used to trace the execution of a comparison-based algorithm
•Branch (referring to binary tree):
line that connects two nodes
•Selection sort algorithm:
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
•Node (referring to binary trees)
represents a comparison
•Leaf (referring to binary tree):
represents final ordering of the nodes
•Path (referring to binary tree):
sequence of branches from one node to another
•Insertion sort algorithm:
sorts the list by moving each element to its proper place in the sorted portion of the list
Asymptotic
the study of the function f as n becomes larger and larger without bound
•Root (referring to binary tree):
the top node
•Quick sort Algorithm:
uses the divide-and-conquer technique •The list is partitioned into two sublists •Each sublist is then sorted •Sorted sublists are combined into one list in such a way that the combined list is sorted •All of the sorting work occurs during the partitioning of the list
Binary Search Steps
•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 restrict the search to the upper half of the list
•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
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
Sequential search (linear search)
•Starts at first element and examines each element until a match is found
•Merge sort:
•Uses the divide-and-conquer technique -Partitions the list into two sublists -Sorts the sublists -Combines the sublists into one sorted list
Pivot Element (Quick sort)
•is chosen to divide the list into: lowerSublist and upperSublist •The elements in lowerSublist are < pivot •The elements in upperSublist are ≥ pivot •Ideally, the pivot divides the list into two sublists of nearly- equal size