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

Slides:



Advertisements
Similar presentations
The Efficiency of Algorithms
Advertisements

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Sorting CMSC 201. Sorting In computer science, there is often more than one way to do something. Sorting is a good example of this!
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
CMPS1371 Introduction to Computing for Engineers SORTING.
1 CSE1301 Computer Programming: Lecture 28 List Sorting.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Simple Sorting Algorithms
Searching and Sorting SLA Computer Science 4/16/08 Allison Mishkin.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Algorithm Efficiency and Sorting
Searching/Sorting Introduction to Computing Science and Programming I.
Analysis of Algorithms CS 477/677
Selection Sort, Insertion Sort, Bubble, & Shellsort
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
Searching and Sorting Chapter Sorting Arrays.
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
Computer Science 101 Introduction to Sorting. Sorting One of the most common activities of a computer is sorting data Arrange data into numerical or alphabetical.
Sorting. Algorithms Sorting reorders the elements in an array or list in either ascending or descending order. Sorting reorders the elements in an array.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
Sorts Tonga Institute of Higher Education. Introduction - 1 Sorting – The act of ordering data Often, we need to order data.  Example: Order a list of.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
By Jonathan Villanueva. Bubble Sorting: the way to sort an array by switching two values that are right next to each other if the first number bigger.
3 – SIMPLE SORTING ALGORITHMS
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Intro To Algorithms Searching and Sorting. Searching A common task for a computer is to find a block of data A common task for a computer is to find a.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Lecture 4 1 Advance Analysis of Algorithms. Selection Sort 2 Summary of Steps Find the smallest element in the array Exchange it with the element in the.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Introduction to Algorithms. Algorithms Algorithms are ways of solving problems. There is a technical definition that basically says an algorithm is a.
Searching/Sorting. Searching Searching is the problem of Looking up a specific item within a collection of items. Searching is the problem of Looking.
Chapter 4, Part I Sorting Algorithms. 2 Chapter Outline Insertion sort Bubble sort Shellsort Radix sort Heapsort Merge sort Quicksort External polyphase.
OCR A Level F453: Data structures and data manipulation Data structures and data manipulation a. explain how static data structures may be.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
3.3 Fundamentals of data representation
Teach A level Computing: Algorithms and Data Structures
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Last Class We Covered Data representation Binary numbers ASCII values
Bubble, Selection & Insertion sort
Chapter 8 Arrays Objectives
Bubble Sort Key Revision Points.
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Chapter 8 Arrays Objectives
Simple Sorting Algorithms
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Simple Sorting Algorithms
Insertion Sort and Shell Sort
CSE 373 Sorting 2: Selection, Insertion, Shell Sort
Sorting.
Presentation transcript:

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'?

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.

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

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.

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!

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.

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

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!

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.

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.

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.

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.

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.