1 / 17

CS 240: Data Structures

CS 240: Data Structures. Thursday, July 12 th 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 < .

vivien
Download Presentation

CS 240: Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 240: Data Structures Thursday, July 12th Sorting – Bubble, Insertion, Quicksort, Mergesort, Analysis, STL

  2. 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.

  3. 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

  4. 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?

  5. Insertion Sort

  6. Bubble Sort • Has a good name. • Easy to understand. • Slow and crusty. • What is its efficiency?

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

  8. Mergesort • Mergesort takes two (or more) inputs and sorts them! • But how? • By insertion! • Uh oh… everyone has to get up now.

  9. 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!

  10. 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?

  11. Sorts • Sort of a randomly generated list of 500 items • Times are based on 70’s hardware • (we have faster computers now) Booya!

  12. 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.

  13. 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).

  14. 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!

  15. Next week… • Presentations on Tuesday. • Second exam on Thursday! Yay! + 1

  16. Searches! • Linear search • You know this, you know you do. • Binary search • Why is this a problem with linked list? • Next time: Hashes!

  17. Project 2 • Recursion time!

More Related