Lecture 4: Sorting Algorithms - PowerPoint PPT Presentation

About This Presentation
Title:

Lecture 4: Sorting Algorithms

Description:

How can we get them back into order on the bookshelves as efficiently as possible? ... Go through the pile and find the book with the title which comes first. ... – PowerPoint PPT presentation

Number of Views:127
Avg rating:3.0/5.0
Slides: 9
Provided by: johnl193
Category:

less

Transcript and Presenter's Notes

Title: Lecture 4: Sorting Algorithms


1
Lecture 4 Sorting Algorithms
  • John.Levine_at_cis.strath.ac.uk
  • Prof Branestawms
  • Sorting Challenge

2
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 were not smart
  • More on graph searching later

3
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

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

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

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

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

8
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?
Write a Comment
User Comments (0)
About PowerShow.com