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.

Slides:



Advertisements
Similar presentations
Understanding the Need for Sorting Records
Advertisements

Fundamentals of Python: From First Programs Through Data Structures
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Selection and Insertion Sort Mrs. C. Furman October 1, 2008.
Sorting. Sorting Considerations We consider sorting a list of records, either into ascending or descending order, based upon the value of some field of.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
CPS120: Introduction to Computer Science Searching and Sorting.
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.
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Searching and Sorting Topics  Sequential Search on an Unordered File  Sequential Search on an Ordered File  Binary Search  Bubble Sort  Insertion.
Data Structures Advanced Sorts Part 2: Quicksort Phil Tayco Slide version 1.0 Mar. 22, 2015.
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
Simple Sorting Algorithms
Algorithm Efficiency and Sorting
Programming Logic and Design Fourth Edition, Comprehensive
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.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
CSC220 Data Structure Winter
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Searching and Sorting Topics Sequential Search on an Unordered File
Sorting HKOI Training Team (Advanced)
Recursion, Complexity, and Sorting By Andrew Zeng.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
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.
3 – SIMPLE SORTING ALGORITHMS
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Quicksort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting 1. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step.
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.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Searching Topics Sequential Search Binary Search.
Spring 2006CISC101 - Prof. McLeod1 Last Time The File class. Back to methods –Passing parameters by value and by reference. –Review class attributes. –An.
Spring 2006CISC101 - Prof. McLeod1 Announcements Assn 4 is posted. Note that due date is the 12 th (Monday) at 7pm. (Last assignment!) Final Exam on June.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
CPS120: Introduction to Computer Science Sorting.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
Lists and Sorting Algorithms
Searching and Sorting Searching algorithms with simple arrays
Chapter 9: Sorting and Searching Arrays
Simple Sorting Algorithms
10.3 Bubble Sort Chapter 10 - Sorting.
CISC101 Reminders Quiz 2 this week.
Last Class We Covered Data representation Binary numbers ASCII values
2008/12/03: Lecture 20 CMSC 104, Section 0101 John Y. Park
Winter 2018 CISC101 11/19/2018 CISC101 Reminders
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Sorting … and Insertion Sort.
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
Analysis of Algorithms
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
Presentation transcript:

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 preparation/topics page is posted. Assn 4 and Quiz 4 sample solutions are posted.

Today… Algorithms, Cont.: –Compare search timings. –Start Sorting: Insertion Selection Bubble Winter 2016CISC101 - Prof. McLeod2

3 Algorithms, Cont. - Searching We will look at two algorithms: Sequential Search –Brute force! –Works on a dataset in any order. Binary Search –Fast! –Only works on sorted datasets. Winter 2016

CISC101 - Prof. McLeod4 Binary Search – Timings See TimingBothSearchesDemo.py Does index() work any faster with a sorted() list? Can index() assume the list is sorted? Compare comparison counts between sequential and binary. Winter 2016

Summary You sort datasets just so you can use binary search – it is so much faster! The search functionality built into Python cannot assume that the dataset is sorted, so it will always take longer than our coded binary search. CISC101 - Prof. McLeod5Winter 2016

CISC101 - Prof. McLeod6 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: Any of these would be easy to code for smaller datasets. Winter 2016

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. CISC101 - Prof. McLeod7Winter 2016

CISC101 - Prof. McLeod8 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 2016

CISC101 - Prof. McLeod9 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 2016

CISC101 - Prof. McLeod10 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 2016

CISC101 - Prof. McLeod11 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 2016

CISC101 - Prof. McLeod12 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 2016

CISC101 - Prof. McLeod13 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 2016

CISC101 - Prof. McLeod14 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 2016

CISC101 - Prof. McLeod

CISC101 - Prof. McLeod16 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 it is passed by reference, it stays sorted! Winter 2016

CISC101 - Prof. McLeod17 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 2016

CISC101 - Prof. McLeod18 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 2016

CISC101 - Prof. McLeod19 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 2016

CISC101 - Prof. McLeod20 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 2016

CISC101 - Prof. McLeod

CISC101 - Prof. McLeod22 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 2016