Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4: Sorting Algorithms Prof Branestawm’s Sorting Challenge.

Similar presentations


Presentation on theme: "Lecture 4: Sorting Algorithms Prof Branestawm’s Sorting Challenge."— Presentation transcript:

1 Lecture 4: Sorting Algorithms John.Levine@cis.strath.ac.uk Prof Branestawm’s 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 we’re 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 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.

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?


Download ppt "Lecture 4: Sorting Algorithms Prof Branestawm’s Sorting Challenge."

Similar presentations


Ads by Google