Download presentation
Presentation is loading. Please wait.
1
How can this be simplified?
void Map::destroyHelper(Node* p){ if(p != nullptr) { if(p->left != nullptr){ destroyHelper(p->left); } else if(p->right != nullptr){ destroyHelper(p->right); delete p->data.second; delete p;
2
How can this be simplified?
void Map::destroyHelper(Node* p){ if(p != nullptr) { destroyHelper(p->left); destroyHelper(p->right); delete p->data.second; delete p; }
3
A circle of friends Sue Omer Yusuf Joe Ann
4
Assignment 8 Program Circle class has-a Digraph class has-a
5
What are the responsibilities of
The Digraph class The circle class the program
6
What are the responsibilities of
The Digraph class Store the vertices (members of the circle of friends) Store the edges (designated friends of each member) Is information about who has designated a member as a friend needed? Within the graph class vertices are numbers Does no output to the screen The circle class add members (vertices) and friendships (edges) to a digraph Respond to queries about the circle of friends (does output to the screen) the program Create a circle of friends Present a menu of queries to the user (this is the only screen output done by the program)
7
Another question What is the return type of the methods:
Return the vertices of the digraph Return the successors of a vertex
8
sorting
9
Zybooks by Thursday’s class chapter 21 sections 4 and 5
10
quiz Given a nearly ordered list which of the following sorting algorithms will perform best? Insertion sort Selection sort Given a randomly ordered list which of the following sorting algorithms will require the fewest exchanges (swaps)?
11
quiz Given a nearly ordered list which of the following sorting algorithms will perform best? Insertion sort Selection sort Given a randomly ordered list which of the following sorting algorithms will require the fewest exchanges (swaps)?
12
The sorting problem given a linear collection of items:
x1, x2, x3, ....,xn arrange them so that they are in: ascending order: x1 <= x2 <= x <= xn or descending order: x1 >= x2 >= x >= xn most sorting algorithms do the job by comparing 2 items and when needed exchanging them What sorting algorithms do you know?
13
some sorting algorithms
Algorithms based on comparisons and exchanges N bubblesort, insertionsort, selectionsort N log2 n - quicksort, heapsort, mergesort Algorithms that do no comparisons Radixsort
14
n2 sorting algorithms All use same basic strategy – nested loops
start with an unsorted sequence of length n outer loop – does n-1 passes through the sequence inner loop does comparisons and (sometimes) exchanges resulting in 1 item being moved to where it belongs and the length of the unsorted sequence being reduced by 1 during each of the n – 1 passes comparisons and exchanges are done
15
bubble sort Each pass results in moving largest element in unsorted sequence into its correct place in the sorted sequence Compares neighbors and exchanges them if needed Largest element “bubbles” up unsorted sequence: after pass after pass after pass after pass after pass
16
analysis of performance
How many comparisons are done during each pass? pass 1: n-1 pass 2: n-2 pass 3: n-3 ……. pass n-1: Total number of comparisons is: n(n-1)/2 or (N2 – n) / 2 How many exchanges will be done during each pass? Could be same as number of comparisons Could be 0 Could be anything between 0 and number of comparisons
17
bubble sort performance
based on number of comparisons and exchanges done #comparisons #exchanges big O worst case: average case: best case:
18
bubble sort performance
based on number of comparisons and exchanges done #comparisons #exchanges big O worst case: (n2 - n)/ (n2 - n)/ O(n2) average case: (n2 - n)/ (n2 - n)/ O(n2) best case: (n2 - n)/ O(n2)
19
selection sort Each pass does comparisons to find the largest item in the unsorted sequence and then does an exchange to put it in its final location Unsorted sequence: after pass 1: after pass 2: after pass 3: after pass 4: after pass 5:
20
selection sort performance
based on number of comparisons and exchanges done #comparisons #exchanges big O worst case: average case: best case:
21
selection sort performance
based on number of comparisons and exchanges done #comparisons #exchanges big O worst case: (n2 - n)/ n O(n2) average case: (n2 - n)/ n O(n2) best case: (n2 - n)/ n O(n2)
22
insertion sort Each pass does comparisons to find out where to insert one item into the sorted sequence Unsorted sequence: after pass 1: after pass 2: after pass 3: after pass 4: after pass 5:
23
insertion sort performance
based on number of comparisons and exchanges done #comparisons #exchanges big O worst case: average case: best case:
24
insertion sort performance
based on number of comparisons and exchanges done #comparisons #exchanges big O worst case: (n2 - n)/ (n2 - n)/ O(n2) average case: (n2 - n)/ (n2 - n)/ O(n2) best case: n n O(n)
25
comparing sorting algorithms
number of comparisons done number of swaps done is it stable equal keys kept in same order is it in place requires O(1) extra space is it adaptive speeds up when sequence is sorted or nearly sorted Can it be used with a linked list
26
Algorithm Compares Swaps Stable? Extra space Adaptive? Bubble O(n2) Selection O(n) insertion
27
Algorithm Compares Swaps Stable? Extra space Adaptive? Bubble O(n2) yes O(1) O(n) when sorted Selection O(n) no insertion
28
how can we sort faster? reduce number of passes to log2 n
mergesort and quicksort do this reduce work done per pass to log2 n heapsort does this do no comparisons at all radix sort does this
29
quicksort basic strategy is recursive
qsort (list, low, high) if low >= high return //base case else //recursive case pivot_position = partition (list, low, high) qsort (list, low, pivot_postion - 1) qsort (list, pivot_postion + 1, high)
30
What does partition do? picks one element in the list it is sent (call it the pivot) Zybooks uses the element at position (low + High) / 2 There are Other possibilities does comparisons and exchanges resulting in rearranging list so that elements < pivot pivot element elements >= pivot Pivot element is where is belongs, don’t need to deal with again
31
What is the result of calling partition for the list {67, 33, 49, 21, 25, 94} ?
Which element is the pivot? How is the list rearranged? What value is returned?
32
What is the result of calling partition for the list {67, 33, 49, 21, 25, 94} ?
Which element is the pivot? 49 How is the list rearranged? {25, 33, 21, 49, 67, 94} What value is returned? 3
33
an example quicksorting the list {67, 33, 49, 21, 25, 94}
Pass 1: lo = 0, hi = 5, mid = 2, pivot = 49 result of partitioning, returns 3 Pass 2: lo = 0, hi = 2, mid = 1, pivot = 33 result of partitioning, returns 2 Pass 3: lo = 0, hi = 1, mid = 0, pivot = result of partitioning, returns 1 Pass 4: lo = 0, hi = 0, mid = 0, base case reached
34
Completing the job Pass 1: 67 33 49 21 25 94 25 33 21 49 67 94
Pass 2: upper partition needs to be sorted Pass 3: upper partition empty (base case) Pass 4:
35
original list {67, 33,49, 21, 25, 94} pass { } {67 94} pass {25 21} {} {} {94} pass {21} {} ~~ ~~ ~~ ~~ ~~ sorted list {21, 25, 33, 49, }
36
quicksort performance
how many comparisons per pass? how many exchanges per pass? how many passes?
37
quicksort performance
Number of comparisons done per pass O(n) Number of exchanges done per pass O(n) (best case is 0) number of passes made If partitions are even – log2 n Worst case – N - 1
38
quicksort variations pivot selection method Middle low median of 3
random do insertion sort for partitions of size < 20
39
quicksort Is it Stable? Is it Adaptive?
How much extra space does it require?
40
quicksort Is it Stable? Is it Adaptive?
No Is it Adaptive? no How much extra space does it require? Log2 n
41
Sorting and the standard template library
42
Sorting and the stl Sort function Stable_sort
Requires random_access iterators Is not stable O(N log2 N) Stable_sort Is stable Depending on extra memory available is O(N log22 N) List<t> has a sort method
43
Sorting-algorithms.com
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.