0% found this document useful (0 votes)
38 views17 pages

CS 240: Data Structures

This document provides an overview of different sorting algorithms that will be covered in CS 240: Data Structures, including bubble sort, insertion sort, quicksort, mergesort, and STL sort. It discusses the time complexity of each algorithm and compares their performance on sample datasets. It also briefly introduces searching algorithms and previews that hashes will be covered in future lectures, alongside presentations and an exam the following week.

Uploaded by

rksinghrajput1
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views17 pages

CS 240: Data Structures

This document provides an overview of different sorting algorithms that will be covered in CS 240: Data Structures, including bubble sort, insertion sort, quicksort, mergesort, and STL sort. It discusses the time complexity of each algorithm and compares their performance on sample datasets. It also briefly introduces searching algorithms and previews that hashes will be covered in future lectures, alongside presentations and an exam the following week.

Uploaded by

rksinghrajput1
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

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?
Insertion Sort
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)
A lg o r it h m T y p e o f S o rt T im e ( s e c )
A lg o r it h m T y p e o f S o rt T im e ( s e c )
• B u b b le s o r t ( 2 0 0 7 ) E xc h a n g e <1
• S im p le s e le c t io n S e le c t io n 69
•H e a p s o rt S e le c t io n 18
• B u b b le s o r t E xc h a n g e 165
• 2 w a y b u b b le s o r t
• Q u ic k s o r t
Booya!
E xc h a n g e
E xc h a n g e
141
6
• L in e a r in s e r t io n I n s e r t io n 66
• B in a r y in s e r t io n I n s e r t io n 37
• S h e ll s o r t I n s e r t io n 11
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!
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!

You might also like