How can this be simplified?

Slides:



Advertisements
Similar presentations
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Advertisements

Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Data Structures and Algorithms PLSD210 Sorting. Card players all know how to sort … First card is already sorted With all the rest, ¶Scan back from the.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
C++ Plus Data Structures
TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) Jan Maluszynski - HT Sorting: –Intro: aspects of sorting, different strategies –Insertion.
Cmpt-225 Sorting. Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort.
Sorting Chapter 10.
1 7.5 Heapsort Average number of comparison used to heapsort a random permutation of N items is 2N logN - O (N log log N).
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
Sorting Chapter 10. Chapter 10: Sorting2 Chapter Objectives To learn how to use the standard sorting methods in the Java API To learn how to implement.
Mergesort and Quicksort Chapter 8 Kruse and Ryba.
CSE 373 Data Structures Lecture 19
Sorting Text Read Shaffer, Chapter 7 Sorting O(N 2 ) sorting algorithms: – Insertion, Selection, Bubble O(N log N) sorting algorithms – HeapSort, MergeSort,
Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Sorting Chapter 10. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
1 Sorting Algorithms Sections 7.1 to Comparison-Based Sorting Input – 2,3,1,15,11,23,1 Output – 1,1,2,3,11,15,23 Class ‘Animals’ – Sort Objects.
Sort Algorithms.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Sorting. Sorting Terminology Sort Key –each element to be sorted must be associated with a sort key which can be compared with other keys e.g. for any.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
Prof. U V THETE Dept. of Computer Science YMA
Sorting and Searching Algorithms
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
Sorting.
Sorting Algorithms Sections 7.1 to 7.4.
CSCI 104 Sorting Algorithms
Algorithm Efficiency and Sorting
Sorting Chapter 13 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort.
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
Design and Analysis of Algorithms
Teach A level Computing: Algorithms and Data Structures
Data Structures and Algorithms
Description Given a linear collection of items x1, x2, x3,….,xn
Advanced Sorting Methods: Shellsort
Unit IV : Searching and sorting Techniques
Sorting … and Insertion Sort.
C++ Plus Data Structures
Sorting Chapter 8 CS 225.
Sub-Quadratic Sorting Algorithms
Sorting Chapter 13 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort.
CSE 326: Data Structures Sorting
Sorting Chapter 10.
Chapter 10 Sorting Algorithms
Algorithm Efficiency and Sorting
CSE 332: Sorting II Spring 2016.
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Advanced Sorting Methods: Shellsort
Presentation transcript:

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;

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; }

A circle of friends Sue Omer Yusuf Joe Ann

Assignment 8 Program Circle class has-a Digraph class has-a

What are the responsibilities of The Digraph class The circle class the program

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)

Another question What is the return type of the methods: Return the vertices of the digraph Return the successors of a vertex

sorting

Zybooks by Thursday’s class chapter 21 sections 4 and 5

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)?

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)?

The sorting problem given a linear collection of items: x1, x2, x3, ....,xn arrange them so that they are in: ascending order: x1 <= x2 <= x3 .... <= xn or descending order: x1 >= x2 >= x3 .... >= xn most sorting algorithms do the job by comparing 2 items and when needed exchanging them What sorting algorithms do you know?

some sorting algorithms Algorithms based on comparisons and exchanges N2 - bubblesort, insertionsort, selectionsort N log2 n - quicksort, heapsort, mergesort Algorithms that do no comparisons Radixsort

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

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: 67 33 25 21 94 49 after pass 1 33 25 21 67 49 94 after pass 2 25 21 33 49 67 94 after pass 3 21 25 33 49 67 94 after pass 4 21 25 33 49 67 94 after pass 5 21 25 33 49 67 94

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: 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

bubble sort performance based on number of comparisons and exchanges done #comparisons #exchanges big O worst case: average case: best case:

bubble sort performance based on number of comparisons and exchanges done #comparisons #exchanges big O worst case: (n2 - n)/2 (n2 - n)/2 O(n2) average case: (n2 - n)/2 (n2 - n)/4 O(n2) best case: (n2 - n)/2 0 O(n2)

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: 67 33 25 21 94 49 after pass 1: 67 33 25 21 49 94 after pass 2: 49 33 25 21 67 94 after pass 3: 21 33 25 49 67 94 after pass 4: 21 25 33 49 67 94 after pass 5: 21 25 33 49 67 94

selection sort performance based on number of comparisons and exchanges done #comparisons #exchanges big O worst case: average case: best case:

selection sort performance based on number of comparisons and exchanges done #comparisons #exchanges big O worst case: (n2 - n)/2 n - 1 O(n2) average case: (n2 - n)/2 n - 1 O(n2) best case: (n2 - n)/2 n - 1 O(n2)

insertion sort Each pass does comparisons to find out where to insert one item into the sorted sequence Unsorted sequence: 67 33 25 21 94 49 after pass 1: 33 67 25 21 94 49 after pass 2: 25 33 67 21 94 49 after pass 3: 21 25 33 67 94 49 after pass 4: 21 25 33 67 94 49 after pass 5: 21 25 33 49 67 94

insertion sort performance based on number of comparisons and exchanges done #comparisons #exchanges big O worst case: average case: best case:

insertion sort performance based on number of comparisons and exchanges done #comparisons #exchanges big O worst case: (n2 - n)/2 (n2 - n)/2 O(n2) average case: (n2 - n)/4 (n2 - n)/4 O(n2) best case: n-1 n-1 O(n)

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

Algorithm Compares Swaps Stable? Extra space Adaptive? Bubble O(n2) Selection O(n) insertion

Algorithm Compares Swaps Stable? Extra space Adaptive? Bubble O(n2) yes O(1) O(n) when sorted Selection O(n) no insertion

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

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)

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

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?

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

an example quicksorting the list {67, 33, 49, 21, 25, 94} Pass 1: 67 33 49 21 25 94 lo = 0, hi = 5, mid = 2, pivot = 49 25 33 21 49 67 94 result of partitioning, returns 3 Pass 2: 25 33 21 lo = 0, hi = 2, mid = 1, pivot = 33 25 21 33 result of partitioning, returns 2 Pass 3: 25 21 lo = 0, hi = 1, mid = 0, pivot = 25 21 25 result of partitioning, returns 1 Pass 4: 21 lo = 0, hi = 0, mid = 0, base case reached

Completing the job Pass 1: 67 33 49 21 25 94 25 33 21 49 67 94 25 33 21 49 67 94 Pass 2: 25 33 21 upper partition needs to be sorted 25 21 33 Pass 3: 25 21 upper partition empty (base case) 21 25 Pass 4: 21

original list {67, 33,49, 21, 25, 94} pass 1 {25 33 21} 49 {67 94} pass 2 {25 21} 33 {} {} 67 {94} pass 3 {21} 25 {} ~~ ~~ ~~ ~~ ~~ sorted list {21, 25, 33, 49, 67 94}

quicksort performance how many comparisons per pass? how many exchanges per pass? how many passes?

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

quicksort variations pivot selection method Middle low median of 3 random do insertion sort for partitions of size < 20

quicksort Is it Stable? Is it Adaptive? How much extra space does it require?

quicksort Is it Stable? Is it Adaptive? No Is it Adaptive? no How much extra space does it require? Log2 n

Sorting and the standard template library

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

Sorting-algorithms.com