Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

Lecture 12: Revision Lecture Dr John Levine Algorithms and Complexity March 27th 2006.
Introduction to Algorithms
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Quicksort CSE 331 Section 2 James Daly. Review: Merge Sort Basic idea: split the list into two parts, sort both parts, then merge the two lists
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Sorting Algorithms and Average Case Time Complexity
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
Heapsort. Heapsort is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat slower in practice on most machines.
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.
CSE 340: Review (at last!) Measuring The Complexity Complexity is a function of the size of the input O() Ω() Θ() Complexity Analysis “same order” Order.
Prof. U V THETE Dept. of Computer Science YMA
School of Computing Clemson University Fall, 2012
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Sorting.
May 17th – Comparison Sorts
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
CSE 373: Data Structure & Algorithms Comparison Sorting
Sorting Chapter 13 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort.
Data Structures Using C++
CSC 421: Algorithm Design & Analysis
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Winter 2015.
Divide-And-Conquer-And-Combine
Chapter 7 Sorting Spring 14
Week 12 - Wednesday CS221.
Teach A level Computing: Algorithms and Data Structures
CSE373: Data Structures & Algorithms Lecture 15: Introduction to Graphs Catie Baker Spring 2015.
CSE 143 Lecture 23: quick sort.
CSC 413/513: Intro to Algorithms
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
CSE373: Data Structures & Algorithms Lecture 16: Introduction to Graphs Linda Shapiro Spring 2016.
Chapter 4: Divide and Conquer
Data Structures and Algorithms
Quick Sort (11.2) CSE 2011 Winter November 2018.
Data Structures and Algorithms
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
Data Structures and Algorithms
B-Tree Insertions, Intro to Heaps
Lecture 5: Master Theorem, Maps, and Iterators
Data Structures and Algorithms
MSIS 655 Advanced Business Applications Programming
8/04/2009 Many thanks to David Sun for some of the included slides!
Divide-And-Conquer-And-Combine
Sub-Quadratic Sorting Algorithms
slides adapted from Marty Stepp
CSE373: Data Structure & Algorithms Lecture 21: Comparison Sorting
Sorting and recurrence analysis techniques
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
EE 312 Software Design and Implementation I
Analysis of Algorithms
CSC 421: Algorithm Design & Analysis
CSE 373 Data Structures and Algorithms
Searching/Sorting/Searching
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Data Structures and Algorithms
Lecture 15: Sorting Algorithms
Lecture 9: Intro to Trees
CSE 332: Sorting II Spring 2016.
CSCE 3110 Data Structures & Algorithm Analysis
CSC 421: Algorithm Design & Analysis
CS203 Lecture 15.
Lecture 20: Implementing Graphs
Lecture 19: Sorting Algorithms
Lecture 27: Array Disjoint Sets
Presentation transcript:

Data Structures and Algorithms Sorting Data Structures and Algorithms CSE 373 Su 18 – Ben jones

Warmup Discuss with your neighbors: What considerations do we think about when choosing a sorting algorithm? So far we have seen: selection sort, insertion sort, and heap sort. What is the “main idea” behind each one? What are their properties? In which contexts are they better or worse? CSE 373 Su 18 – Ben jones

Warmup Algorithm Main Idea Best Case Worst Case Average Case In Place? Stable? Selection Sort Repeatedly find the next smallest element and put in front. O(n^2) Yes Insertion Sort Pull the next unsorted element and insert into the proper position. O(n) Heap Sort Repeatedly pull the min element from a heap. O(n log n) Can Be Merge Sort Recursively sort then merge the left and right halves. O(n log n)* No ??? * there are O(n) best case variants of merge-sort used in practice CSE 373 Su 18 – Ben jones

Announcements Individual Homework Due Tonight Project 2 is assigned – it’s a one week project (so due on Friday) Also by Friday: sign up for partner for project 3! https://goo.gl/forms/KYVCv4QddVN5Rbyi1 - Remember to sign up for a partner – you won’t automatically be re-partnered with the same person - (for random partnering, we’ll assume your availability is the same as last time) Course format change: Smaller homeworks, more frequently - Should keep HW content closer to lecture content CSE 373 Su 18 – Ben jones

Review: Selection Sort and Insertion Sort https://visualgo.net/en/sorting CSE 373 Su 18 – Ben jones

Merge Sort https://www.youtube.com/watch?v=XaqR3G_NVoo Divide 1 2 3 4 1 2 3 4 5 6 7 8 9 91 22 57 10 1 2 3 4 8 91 22 57 5 6 7 8 9 1 10 4 8 Conquer 8 Combine 1 2 3 4 8 22 57 91 5 6 7 8 9 1 4 10 1 2 3 4 5 6 7 8 9 10 22 57 91 CSE 373 SP 18 - Kasey Champion

1 2 3 4 8 57 91 22 Merge Sort 1 8 2 1 2 57 91 22 mergeSort(input) { if (input.length == 1) return else smallerHalf = mergeSort(new [0, ..., mid]) largerHalf = mergeSort(new [mid + 1, ...]) return merge(smallerHalf, largerHalf) } 8 2 57 1 91 22 91 22 1 22 91 Worst case runtime? Best case runtime? Average runtime? Stable? In-place? 1 if n<= 1 2T(n/2) + n otherwise T(n) = 1 2 8 1 2 22 57 91 Yes 1 2 3 4 8 22 57 91 No CSE 373 SP 18 - Kasey Champion

Merge Sort Optimization Use just two arrays – swap between them Another Optimization: Switch to Insertion Sort for small arrays (e.g. n < 10) CSE 373 Su 18 – Ben jones

Merge Sort Benefits Useful for massive data sets that cannot fit on one machine Works well for linked-lists and other sequentially accessible data sets A O(n log n) stable sort! Easy to implement! mergeSort(input) { if (input.length == 1) return else smallerHalf = mergeSort(new [0, ..., mid]) largerHalf = mergeSort(new [mid + 1, ...]) return merge(smallerHalf, largerHalf) } Homework! CSE 373 Su 18 – Ben jones

Quick Sort Main Idea: Divide and Conquer – “smaller” “half” and “bigger” “half” “smaller” and “bigger” relative to some pivot element “half” doesn’t always mean half, but the closer it is to half, the better CSE 373 Su 18 – Ben jones

Quick Sort Divide Conquer Combine https://www.youtube.com/watch?v=ywWBy6J5gz8 Divide Conquer Combine 1 2 3 4 5 6 7 8 9 91 22 57 10 1 2 3 4 6 7 8 1 2 3 91 22 57 10 6 6 2 3 4 22 57 91 8 5 6 7 8 9 1 4 10 1 2 3 4 5 6 7 8 9 10 22 57 91 CSE 373 SP 18 - Kasey Champion

1 2 3 4 5 6 20 50 70 10 60 40 30 Quick Sort 10 1 2 3 4 50 70 60 40 30 quickSort(input) { if (input.length == 1) return else pivot = getPivot(input) smallerHalf = quickSort(getSmaller(pivot, input)) largerHalf = quickSort(getBigger(pivot, input)) return smallerHalf + pivot + largerHalf } 1 40 30 1 70 60 30 60 1 30 40 1 60 70 1 if n<= 1 n + T(n - 1) otherwise Worst case runtime? Best case runtime? Average runtime? Stable? In-place? T(n) = 1 2 3 4 30 40 50 60 70 1 if n<= 1 n + 2T(n/2) otherwise T(n) = 1 2 3 4 5 6 10 20 30 40 50 60 70 No No CSE 373 SP 18 - Kasey Champion

Can we do better? Pick a better pivot Pick a random number Pick the median of the first, middle and last element Sort elements by swapping around pivot in place CSE 373 SP 18 - Kasey Champion

Better Quick Sort 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 Low X < 6 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 Low X < 6 High X >= 6 1 2 3 4 5 6 7 8 9 Low X < 6 High X >= 6 CSE 373 SP 18 - Kasey Champion

Project 2: Invariants, Pre-conditions, and post-conditions CSE 373 Su 18 – Ben jones

Introduction to Graphs CSE 373 SP 18 - Kasey Champion

Inter-data Relationships Arrays Categorically associated Sometimes ordered Typically independent Elements only store pure data, no connection info Trees Directional Relationships Ordered for easy access Limited connections Elements store data and connection info Graphs Multiple relationship connections Relationships dictate structure Connection freedom! Both elements and connections can store data 1 2 A B C A B C B C A CSE 373 SP 18 - Kasey Champion

Graph: Formal Definition B C D E F G H A graph is defined by a pair of sets G = (V, E) where… V is a set of vertices A vertex or “node” is a data entity E is a set of edges An edge is a connection between two vertices V = { A, B, C, D, E, F, G, H } E = { (A, B), (A, C), (A, D), (A, H), (C, B), (B, D), (D, E), (D, F), (F, G), (G, H)} CSE 373 SP 18 - Kasey Champion

Applications Physical Maps Relationships Influence Related topics Airline maps Vertices are airports, edges are flight paths Traffic Vertices are addresses, edges are streets Relationships Social media graphs Vertices are accounts, edges are follower relationships Code bases Vertices are classes, edges are usage Influence Biology Vertices are cancer cell destinations, edges are migration paths Related topics Web Page Ranking Vertices are web pages, edges are hyperlinks Wikipedia Vertices are articles, edges are links SO MANY MORREEEE www.allthingsgraphed.com CSE 373 SP 18 - Kasey Champion

Graph Vocabulary Graph Direction Degree of a Vertex Undirected Graph: Undirected graph – edges have no direction and are two-way Directed graphs – edges have direction and are thus one-way Degree of a Vertex Degree – the number of edges containing that vertex A : 1, B : 1, C : 1 In-degree – the number of directed edges that point to a vertex A : 0, B : 2, C : 1 Out-degree – the number of directed edges that start at a vertex A B C V = { A, B, C } E = { (A, B), (B, C) } inferred (B, A) and (C,B) Undirected Graph: A B C V = { A, B, C } E = { (A, B), (B, C), (C, B) } CSE 373 SP 18 - Kasey Champion

Food for thought Is a graph valid if there exists a vertex with a degree of 0? Yes A B C A B C A B C A has an “in degree” of 0 B has an “out degree” of 0 C has both an “in degree” and an “out degree” of 0 Are these valid? Is this a valid graph? Yup A B C D A B C A Sure Yes! CSE 373 SP 18 - Kasey Champion

Graph Vocabulary Self loop – an edge that starts and ends at the same vertex Parallel edges – two edges with the same start and end vertices Simple graph – a graph with no self-loops and no parallel edges A A B CSE 373 SP 18 - Kasey Champion