Download presentation
Presentation is loading. Please wait.
1
CS 240: Data Structures Thursday, July 12 th Sorting – Bubble, Insertion, Quicksort, Mergesort, Analysis, STL
2
Sorting Ok, so we have a list: Ok, so we have a list: X1, X2, X3, X4, X5, X6, … XN We can sort this list either ascending or descending. We can sort this list either ascending or descending. Protip: You only need operator <. Protip: You only need operator <. Not that I want you to code any less. I like it when you write operator > too. 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. First, we describe some simple techniques. These techniques are usually slow (O(n 2 )) These techniques are usually slow (O(n 2 )) But, they are easier to understand. But, they are easier to understand. You will have to understand the more complex schemes eventually. There is no escape. 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: There are two kinds of insertion sort: Insert in-order into a new list Insert in-order into a new list Reposition into our existing array Reposition into our existing array What are the tradeoffs? What are the tradeoffs?
5
Insertion Sort
6
Bubble Sort Has a good name. Has a good name. Easy to understand. Easy to understand. Slow and crusty. Slow and crusty. What is its efficiency? 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? 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. Me too. I like quicksort though. Too many variables apply: Too many variables apply: List contents List contents Pre-sortedness Pre-sortedness Etc. Etc.
8
Mergesort Mergesort takes two (or more) inputs and sorts them! Mergesort takes two (or more) inputs and sorts them! But how? But how? By insertion! By insertion! Uh oh… everyone has to get up now. Uh oh… everyone has to get up now.
9
Quicksort The “Bad Boy” of sorts. The “Bad Boy” of sorts. This is recursive too. This is recursive too. Again, everyone needs to get up. Again, everyone needs to get up. Hey…. These slides are pretty empty. But, you get to code this stuff! Hey…. These slides are pretty empty. But, you get to code this stuff!
10
Radix Sort Sorting by digits… maybe characters… Sorting by digits… maybe characters… Lets create some 3-digit numbers. Lets create some 3-digit numbers. What if our numbers don’t have the same number of digits? What if our numbers don’t have the same number of digits? Lexigraphical order? 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! In order to use the STL sort, you need to use iterators! We’ll use vector to do this! We’ll use vector to do this! Remind me to put this code online. Remind me to put this code online.
13
Sort Analysis Well, first we need to explain more structurally how these work: Well, first we need to explain more structurally how these work: Mergesort: Mergesort: Split up a list into smaller parts (half size) until the list is of size 1. Split up a list into smaller parts (half size) until the list is of size 1. Put lists back together by “merging”: insertion sort Put lists back together by “merging”: insertion sort Quicksort: Quicksort: Select a value and ensure that all values to the left are smaller – all values to the right are equal or larger 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). 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? Well, remember the difference between pass by value and pass by reference in terms of speed? Sometimes you need to sort large objects! Sometimes you need to sort large objects! You can use pointers! Fast access! Avoid copying data around! You can use pointers! Fast access! Avoid copying data around!
15
Next week… Presentations on Tuesday. Presentations on Tuesday. Second exam on Thursday! Second exam on Thursday!
16
Searches! Linear search Linear search You know this, you know you do. You know this, you know you do. Binary search Binary search Why is this a problem with linked list? Why is this a problem with linked list? Next time: Hashes! Next time: Hashes!
17
Project 2 Recursion time! Recursion time!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.