170 likes | 282 Vues
In this session, we dive into fundamental sorting algorithms, including Bubble Sort, Insertion Sort, Quicksort, and Mergesort. Learn how to sort lists efficiently in both ascending and descending order, along with the trade-offs of various techniques. Understand the mechanics behind these algorithms, their time complexities, and practical applications, especially using the STL sort with iterators. We’ll also touch upon indirect sorting methods and their benefits for handling large objects. Get ready to enhance your coding skills!
E N D
CS 240: Data Structures Thursday, July 12th Sorting – Bubble, Insertion, Quicksort, Mergesort, Analysis, STL
Sorting • Ok, so we have a list: X1, X2, X3, X4, X5, X6, … XN • We can sort this list either ascending or descending. • Protip: You only need operator <. • Not that I want you to code any less. I like it when you write operator > too.
Sorting • First, we describe some simple techniques. • These techniques are usually slow (O(n2)) • But, they are easier to understand. • You will have to understand the more complex schemes eventually. There is no escape. http://tanksoftware.com/tutes/uni/sorting.html
Insertion Sort • There are two kinds of insertion sort: • Insert in-order into a new list • Reposition into our existing array • What are the tradeoffs?
Bubble Sort • Has a good name. • Easy to understand. • Slow and crusty. • What is its efficiency?
Sorts • Ok, now that I’ve filled your head with the basics. Do you want to know the ultimate, mega-powered, best sort ever? • Me too. I like quicksort though. • Too many variables apply: • List contents • Pre-sortedness • Etc.
Mergesort • Mergesort takes two (or more) inputs and sorts them! • But how? • By insertion! • Uh oh… everyone has to get up now.
Quicksort • The “Bad Boy” of sorts. • This is recursive too. • Again, everyone needs to get up. • Hey…. These slides are pretty empty. But, you get to code this stuff!
Radix Sort • Sorting by digits… maybe characters… • Lets create some 3-digit numbers. • What if our numbers don’t have the same number of digits? • Lexigraphical order?
Sorts • Sort of a randomly generated list of 500 items • Times are based on 70’s hardware • (we have faster computers now) Booya!
STL sort • In order to use the STL sort, you need to use iterators! • We’ll use vector to do this! • Remind me to put this code online.
Sort Analysis • Well, first we need to explain more structurally how these work: • Mergesort: • Split up a list into smaller parts (half size) until the list is of size 1. • Put lists back together by “merging”: insertion sort • Quicksort: • Select a value and ensure that all values to the left are smaller – all values to the right are equal or larger • Repeat with left/right side until they are of size 1 or 2 (and sorted).
Indirect Sorting • Well, remember the difference between pass by value and pass by reference in terms of speed? • Sometimes you need to sort large objects! • You can use pointers! Fast access! Avoid copying data around!
Next week… • Presentations on Tuesday. • Second exam on Thursday! Yay! + 1
Searches! • Linear search • You know this, you know you do. • Binary search • Why is this a problem with linked list? • Next time: Hashes!
Project 2 • Recursion time!