lecture 4: sorting algorithms [email protected] prof branestawm’s sorting challenge

8
Lecture 4: Sorting Algorithms [email protected] .uk Prof Branestawm’s Sorting Challenge

Upload: bennett-phillips

Post on 02-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lecture 4: Sorting Algorithms John.Levine@cis.strath.ac.uk Prof Branestawm’s Sorting Challenge

Lecture 4: Sorting Algorithms

[email protected]

Prof Branestawm’s

Sorting Challenge

Page 2: Lecture 4: Sorting Algorithms John.Levine@cis.strath.ac.uk Prof Branestawm’s Sorting Challenge

Recap of Searching

• Linear search through unordered data is O(n)

• Binary search in ordered data is O(log n)

• Parallel search is even faster

• Search through different combinations is exponential if we’re not smart

• More on graph searching later…

Page 3: Lecture 4: Sorting Algorithms John.Levine@cis.strath.ac.uk Prof Branestawm’s Sorting Challenge

The Problem

• Prof Branestawm has 1,024 books, which he usually keeps in alphabetical order

• Bubbles the experimental monkey has gone loopy and thrown them all around the room

• How can we get them back into order on the bookshelves as efficiently as possible?

• Comparison of titles is the slow step

Page 4: Lecture 4: Sorting Algorithms John.Levine@cis.strath.ac.uk Prof Branestawm’s Sorting Challenge

Random Sort

1. Put the books back on the shelves in a random order.

2. Check to see if they are in alphabetical order.

3. If they are, stop.

4. Else throw them on the floor and repeat from Step 1.

Page 5: Lecture 4: Sorting Algorithms John.Levine@cis.strath.ac.uk Prof Branestawm’s Sorting Challenge

Naïve Sort

1. Go through the pile and find the book with the title which comes first.

2. Put it on the shelf in first place.

3. Find the second book and put it in second place on the shelf.

4. Repeat until all the books are done.

Page 6: Lecture 4: Sorting Algorithms John.Levine@cis.strath.ac.uk Prof Branestawm’s Sorting Challenge

Bubblesort

1. Put the books on the shelves in a random order.

2. Go through the books comparing the book at position i with the book at position i+1. If they are in the wrong order swap them.

3. Repeat step 2 until a pass is made over the books where no swapping occurs.

Page 7: Lecture 4: Sorting Algorithms John.Levine@cis.strath.ac.uk Prof Branestawm’s Sorting Challenge

Quicksort

1. Partition the books into two piles: one pile for titles that start with A-M, one for N-Z.

2. Partition each pile into a further two piles, trying to keep the piles of equal size.

3. Repeat until the all the piles are of size 1, then put all the books onto the shelves in order.

Page 8: Lecture 4: Sorting Algorithms John.Levine@cis.strath.ac.uk Prof Branestawm’s Sorting Challenge

The Challenge

• What is the complexity of each of these sorting algorithms?

• How long will each take on average for 1024 books if the comparison step takes 1 second? How about for 2048 books?

• Can you think of an algorithm that could do better than any of these?