Winter 2018 CISC101 11/19/2018 CISC101 Reminders

Slides:



Advertisements
Similar presentations
Visual C++ Programming: Concepts and Projects
Advertisements

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.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
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.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
Spring 2006CISC101 - Prof. McLeod1 Last Time The File class. Back to methods –Passing parameters by value and by reference. –Review class attributes. –An.
CPS120: Introduction to Computer Science Sorting.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
COMPSA Exam Prep Session On: April 8th from 1:30-3:00 Location TBA Winter 2016CISC101 - Prof. McLeod1.
CMSC 104, Version 8/061L24Searching&Sorting.ppt Searching and Sorting Topics Sequential Search on an Unordered File Sequential Search on an Ordered File.
Searching and Sorting Searching algorithms with simple arrays
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
Chapter 9: Sorting and Searching Arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
Week 9 - Monday CS 113.
Introduction to Search Algorithms
Arrays 2.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Simple Sorting Algorithms
Chapter 7 Sorting Spring 14
Algorithm Analysis CSE 2011 Winter September 2018.
CISC/CMPE320 - Prof. McLeod
Design and Analysis of Algorithms
10.3 Bubble Sort Chapter 10 - Sorting.
CISC101 Reminders Quiz 2 this week.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Last Class We Covered Data representation Binary numbers ASCII values
Objectives At the end of the class, students are expected to be able to do the following: Understand the purpose of sorting technique as operations on.
Describing algorithms in pseudo code
2008/12/03: Lecture 20 CMSC 104, Section 0101 John Y. Park
Searching and Sorting Topics Sequential Search on an Unordered File
Quicksort analysis Bubble sort
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Searching and Sorting Topics Sequential Search on an Unordered File
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Sort Techniques.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
CISC101 Reminders Quiz 2 this week.
Standard Version of Starting Out with C++, 4th Edition
8/04/2009 Many thanks to David Sun for some of the included slides!
Sorting … and Insertion Sort.
UMBC CMSC 104 – Section 01, Fall 2016
Searching and Sorting Topics Sequential Search on an Unordered File
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
Winter 2019 CISC101 2/17/2019 CISC101 Reminders
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.
EE 312 Software Design and Implementation I
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CISC101 Reminders All assignments are now posted.
Analysis of Algorithms
Simple Sorting Algorithms
CISC101 Reminders Assignment 2 due today.
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
CISC101 Reminders Quiz 1 marking underway.
Simple Sorting Algorithms
CISC101 Reminders Assignment 3 due today.
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
EE 194/BIO 196: Modeling biological systems
Sorting.
Sorting and Complexity
10.3 Bubble Sort Chapter 10 - Sorting.
Presentation transcript:

Winter 2018 CISC101 11/19/2018 CISC101 Reminders Last quiz this week in lab. Topics in slides from last week. Last assignment due last day of class (April 6). Help page for viewing graded quizzes in onQ is posted in onQ under “Course Resources”. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

Today USATS. Algorithms – Sorting. Winter 2018 CISC101 - Prof. McLeod

Sorting – Overview We will look at three simple sorts: Insertion sort CISC101 Sorting – Overview We will look at three simple sorts: Insertion sort Selection sort Bubble sort Insertion and Selection can be very useful in certain situations. Bubble sort may be the *worst* sorting algorithm known! (See: https://www.youtube.com/watch?v=k4RRi_ntQc8) Any of these would be easy to code for smaller datasets. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

Before We Start… You need to learn these algorithms for the same reasons you needed to learn searching algorithms. The sort() in Python is way faster – it uses Quicksort, which uses recursion – both topics are outside the scope of this course (see CISC121!). Even if we coded Quicksort it would still be slower because of the interpreted vs. compiled issue. Winter 2018 CISC101 - Prof. McLeod

Sorting – Overview – Cont. The first step in sorting is to select the criteria used for the sort and the direction of the sort. It could be ascending numeric order, or alphabetic order by last name, etc. Winter 2018 CISC101 - Prof. McLeod

Sorting – Overview – Cont. How to sort (which algorithm to use)?: How large is the dataset? What will be critical: memory usage or execution time? Is it necessary that a new element be inserted in order or can it be added at the end and the sort deferred? How often will the algorithm be asked to sort a dataset that is already in order, except for a few newly added elements? Or, will it always have to re-sort a completely disordered dataset? Winter 2018 CISC101 - Prof. McLeod

Sorting – Overview – Cont. Sorting algorithms can be compared on the basis of: The number of comparisons for a dataset of size n, The number of data movements (“swaps”) necessary, and How these measures change with n (Analysis of Complexity…). Often need to consider these measures for best case (data almost in order), average case (random order), and worst case (reverse order). Some algorithms behave the same regardless of the state of the data, and others do better depending on how well the data is initially ordered. Winter 2018 CISC101 - Prof. McLeod

Sorting – Overview – Cont. If sorting simple values like integers or key values, then comparisons are easy to carry out and the comparison efficiency of the algorithm may not be as important as the number of data movements. However, if strings or objects are being compared then the number of comparisons would be best kept to a minimum. Finally, the only real measure of what algorithm is the best is an actual measure of elapsed time. The initial choice can be based on theory alone, but the final choice for a time-critical application must be by actual experimental measurement. Winter 2018 CISC101 - Prof. McLeod

Sorting – Overview – Cont. I will be presenting code samples that sort lists of integers into ascending order because this is easiest to understand. However the logic of the algorithm can be applied directly to lists of strings or other objects, provided the search criteria are specified. Descending order usually only requires that you change the comparison from “>” to “<“. Winter 2018 CISC101 - Prof. McLeod

CISC101 Insertion Sort Probably the most “instinctive” kind of sort: Find the location for an element and move all others up one, and insert the element. Pseudocode: Loop through list from i=1 to size-1, selecting element at position i = temp. Locate position for temp (position j, where j <= i), and move all elements above j up one location Put temp at position j. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

Insertion Sort, Cont. def insertionSort(numsList): for i in range(1, len(numsList)): temp = numsList[i] j = i while j > 0 and temp < numsList[j - 1]: numsList[j] = numsList[j - 1] j = j - 1 numsList[j] = temp Winter 2018 CISC101 - Prof. McLeod

27 12 3 18 11 7 12 27 3 18 11 7 3 12 27 18 11 7 3 12 18 27 11 7 3 11 12 18 27 7 3 7 11 12 18 27 Winter 2018 CISC101 - Prof. McLeod

Aside - Sorting “in situ” Our code is sorting the list in place. Saves the memory (and time) required to create a copy of the same list in memory. However, this means that once it is sorted, and since a mutable list is passed by reference, it stays sorted! Winter 2018 CISC101 - Prof. McLeod

CISC101 Selection Sort This one works by selecting the smallest element and then putting it in its proper location. Pseudocode: Loop through the list from i=0 to one element short of the end of the list. Select the smallest element in the list between i plus one to the end of the array. Swap this value with the value at position i. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

swap() Function First, a swap() function that will be used by this and other sorts, that works in Python: def swap(numsList, pos1, pos2) : numsList[pos1], numsList[pos2] = numsList[pos2], numsList[pos1] Winter 2018 CISC101 - Prof. McLeod

swap() Function, Again Another version that can be done in any language: def swap(numsList, pos1, pos2) : temp = numsList[pos1] numsList[pos1] = numsList[pos2] numsList[pos2] = temp Winter 2018 CISC101 - Prof. McLeod

Selection Sort, Cont. def selectionSort(numsList): i = 0 size = len(numsList) while i < size - 1: smallestPos = i j = i + 1 while j < size: if numsList[j] < numsList[smallestPos]: smallestPos = j j = j + 1 if smallestPos != i: swap(numsList, i, smallestPos) i = i + 1 Winter 2018 CISC101 - Prof. McLeod

27 12 3 18 11 7 3 12 27 18 11 7 3 7 27 18 11 12 3 7 11 18 27 12 3 7 11 12 27 18 3 7 11 12 18 27 Winter 2018 CISC101 - Prof. McLeod

Selection Sort, Cont. Selection sort is “swap efficient”, and insertion sort can be efficient for datasets that are mostly in order. Look at the timings in SortingTestBed.py Winter 2018 CISC101 - Prof. McLeod

CISC101 Bubble Sort Is best envisioned as a vertical column of numbers as bubbles. The larger bubbles gradually work their way to the top of the column, with the smaller ones pushed down to the bottom. Pseudocode: Loop through list from i=0 to the end: Loop down from the last element in the list to i. Swap adjacent elements if they are in the wrong order. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

Bubble Sort, Cont. def bubbleSort(numsList): size = len(numsList) for i in range(0, size): j = size - 1 while j > i : if numsList[j] < numsList[j - 1]: swap(numsList, j, j - 1) j = j - 1 Winter 2018 CISC101 - Prof. McLeod

27 12 3 18 11 7 3 27 12 7 18 11 3 7 27 12 11 18 3 7 11 27 12 18 3 7 11 12 27 18 3 7 11 12 18 27 Winter 2018 CISC101 - Prof. McLeod

Bubble Sort, Cont. Note that both the comparison and the swap are inside the inner loop (yuk!). Winter 2018 CISC101 - Prof. McLeod

Bubble Sort – A Slight Improvement def bitBetterBubbleSort(numsList): size = len(numsList) i = 0 isSorted = False while i < size and not isSorted: j = size - 1 isSorted = True while j > i : if numsList[j] < numsList[j - 1]: swap(numsList, j, j - 1) j = j - 1 i = i + 1 Winter 2018 CISC101 - Prof. McLeod

Bubble Sort, Cont. Possibly the simplest sorting algorithm to code. Also the slowest sorting algorithm! On average, bubble sort makes n (the size of the dataset) times more moves than selection or insertion sort. Winter 2018 CISC101 - Prof. McLeod

Sorting Animations Here are a few that are OK: CISC101 Sorting Animations Here are a few that are OK: http://sorting.at/ http://math.hws.edu/eck/js/sorting/xSortLab.html https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html And youtube has videos of many other animations. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

Demo Program See ModifiedSortingTestBed.py to experiment with the effects of the state of the data. How do our sorts compare to list.sort()? (Not very well. How is .sort() so fast?) Winter 2018 CISC101 - Prof. McLeod

Timings, Summary For 1000 data points between 1 and 1000, random order case. Sort Millisec Python sort() 0.18 Insertion 40 Selection 44 Bubble 102 Better?Bubble 101 Wow! Winter 2018 CISC101 - Prof. McLeod

Observations Insertion sort works remarkably well on data that is almost in order. Selection sort doesn’t care much about the state of the data. Bubble sort is always the worst sort, but it behaves slightly better when the data is more ordered. Winter 2018 CISC101 - Prof. McLeod