Presentation is loading. Please wait.

Presentation is loading. Please wait.

Aims: To learn about some simple sorting algorithms. To develop understanding of the importance of efficient algorithms. Objectives: All:Understand how.

Similar presentations


Presentation on theme: "Aims: To learn about some simple sorting algorithms. To develop understanding of the importance of efficient algorithms. Objectives: All:Understand how."— Presentation transcript:

1 Aims: To learn about some simple sorting algorithms. To develop understanding of the importance of efficient algorithms. Objectives: All:Understand how Bubble Sort works. Most:Understand how Insertion Sort works. Some:Understand why Insertion Sort is faster than Bubble Sort. Whatcha doin'?

2 Sorting Algorithms Computers spend a lot of time sorting data. We've already discussed the idea of an algorithm – a detailed description of how to do something. There are many different algorithms for sorting. Some are much more efficient than others (by which we mainly mean faster). We are going to look at two sorting algorithms and compare them. The first sorting algorithm I learned was: Bubble Sort.

3 First we need to know how to swap the contents of variables in Python:

4 Now we can swap the contents of variables and elements of lists, we can do Bubble Sort. We're going to start by comparing the first element to the next. If it is bigger, we swap them. We then repeat all the way along the array. This must be one less than the length of the list, or we'll get an error. Make sure you understand why. After the first pass, the '8' has moved up to just before the end If we repeat this, the list will be sorted.

5 Here I've put the elements in descending order which is the worst case for Bubble Sort, just to prove it works. We will be able to optimise this a bit, but basically that is the Bubble Sort algorithm in action!

6 Enter this function. Test it works and that you understand why. After the first pass, the largest number is in the right place, so we can reduce the number of iterations by one each time.

7 Bubble Sort is not a very fast sorting algorithm. We are now going to look at (slightly) more efficient algorithm. Imagine you have some books you want to put into alphabetical order. The natural way most people would do this is: Starting with the second book on the shelf: ● compare book (B) to the books(s) to the left. ● continue until you find one lower in the alphabet than B. ● insert B after that book and move the others up one. Repeat until you have placed all the books. Insertion Sort (personally I find magic is faster).

8 The loop runs 4 times: 1. Hardy is higher up the alphabet than Dickens, so we do nothing. 2. Bronte moves to the left and we move 2 books up. 3. Gaskell swaps with Hardy. 4. Conrad takes the second place and 3 books move up. 5. Austen moves to the first position and all the others move up. Done!

9 We store the value we are checking against in val. Then we set j to i-1 – so we are looking at the cell to the left of val. We go down the array until we find a number that is less than val. At each step, we move the data up one cell to the right. Then we set a[j+1] to be val. This is like slotting a book into its correct position on the shelf.

10 How much faster is Insertion Sort? This will test our two algorithms with lists of numbers containing 2000, 4000 and 8000 random numbers. This rounds the time to one decimal place. This is how to copy a list properly. Once we have imported time(), we can subtract the start time from the finish time of a sort to find out how long it took.

11 This is what happens if you are not careful when copying lists! You don't have two copies of the list, instead you have two names pointing to the same thing. That's more like it. Use the slice that represents the whole list and you have two independent copies.

12 As you can see, Bubble Sort is much slower than Insertion Sort. Each time we doubled the length of the array, the time taken to sort it roughly quadrupled. The test was run on a Toshiba Tecra 8200 with a Pentium III processor and 256 mb of RAM.

13 Tasks 1: Run the sorting test on your computer. Do you notice any significant differences from my results? If so, can you explain them? 2:Design some other tests, for example: -> numbers are already sorted -> numbers in reverse order -> numbers are almost sorted Try these and see if they differ from the random tests.


Download ppt "Aims: To learn about some simple sorting algorithms. To develop understanding of the importance of efficient algorithms. Objectives: All:Understand how."

Similar presentations


Ads by Google