We will stamp HW Block day:

Similar documents
We can use a max-heap to sort data.

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Sorting Pearson Education, Inc. All rights reserved.

CS1 Lecture 30 Apr. 2, 2018

Sorting. Task Description. Selection Sort. Should we worry about speed?

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Mergesort again. 1. Split the list into two equal parts

Better sorting algorithms (Weiss chapter )

Introduction to Computers and Programming. Today

Searching and Sorting

Cpt S 122 Data Structures. Sorting

Quicksort (Weiss chapter 8.6)

Searching in General

Data structures. More sorting. Dr. Alex Gerdes DIT961 - VT 2018

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Chapter 10. Sorting and Searching Algorithms. Fall 2017 CISC2200 Yanjun Li 1. Sorting. Given a set (container) of n elements

Sorting is ordering a list of objects. Here are some sorting algorithms

Sorting. Data Structures and Algorithms CSE 373 SU 18 BEN JONES 1

Sorting Algorithms. + Analysis of the Sorting Algorithms

Lecture 19 Sorting Goodrich, Tamassia

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang

Sorting Algorithms Day 2 4/5/17

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Acknowledgement. Outline

CS61B, Spring 2003 Discussion #15 Amir Kamil UC Berkeley 4/28/03

Sorting. Data Structures and Algorithms CSE 373 SU 18 BEN JONES 1

Quick-Sort fi fi fi 7 9. Quick-Sort Goodrich, Tamassia

Search,Sort,Recursion

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F

Key question: how do we pick a good pivot (and what makes a good pivot in the first place)?

About this exam review

Sorting and Selection

CS 171: Introduction to Computer Science II. Quicksort

CS61BL. Lecture 5: Graphs Sorting

Quick-Sort. Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm:

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

Overview of Sorting Algorithms

Chapter 8 Algorithms 1

What did we talk about last time? Finished hunters and prey Class variables Constants Class constants Started Big Oh notation

Mergesort again. 1. Split the list into two equal parts

CSE 373: Data Structures & Algorithms More Sor9ng and Beyond Comparison Sor9ng

CP222 Computer Science II. Searching and Sorting

Sorting. Bringing Order to the World

106B Final Review Session. Slides by Sierra Kaplan-Nelson and Kensen Shi Livestream managed by Jeffrey Barratt

Introduction. two of the most fundamental concepts in computer science are, given an array of values:

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

Sorting. Hsuan-Tien Lin. June 9, Dept. of CSIE, NTU. H.-T. Lin (NTU CSIE) Sorting 06/09, / 13

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

Divide and Conquer Algorithms: Advanced Sorting

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

UNIT 7. SEARCH, SORT AND MERGE

Sorting is a problem for which we can prove a non-trivial lower bound.

CSCI 136 Data Structures & Advanced Programming. Lecture 14 Spring 2018 Profs Bill & Jon

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

DATA STRUCTURES AND ALGORITHMS

Sorting Goodrich, Tamassia Sorting 1

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

Faster Sorting Methods

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

Quick Sort. CSE Data Structures May 15, 2002

Sorting. Order in the court! sorting 1

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1

ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms. C. L. Wyatt

Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order.

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

Sorting & Searching (and a Tower)

COMP Data Structures

Programming in OOP/C++

Announcements. Lab 11 is due tomorrow. Quiz 6 is on Monday. Ninja session tonight, 7-9pm. The final is in two weeks!

2/14/13. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Lecture 8: Mergesort / Quicksort Steven Skiena

Programming II (CS300)

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

Sorting. Order in the court! sorting 1

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

KF5008 Algorithm Efficiency; Sorting and Searching Algorithms;

ECE 122. Engineering Problem Solving Using Java

ITEC2620 Introduction to Data Structures

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to:

Copyright 2009, Artur Czumaj 1

CSE 373: Data Structures and Algorithms

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

Today. CISC101 Reminders & Notes. Searching in Python - Cont. Searching in Python. From last time

Quick-Sort. Quick-Sort 1

Hiroki Yasuga, Elisabeth Kolp, Andreas Lang. 25th September 2014, Scientific Programming

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

Searching, Sorting. part 1

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015

Do you remember any iterative sorting methods? Can we produce a good sorting method by. We try to divide and conquer: break into subproblems

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

Bubble sort is so named because the numbers are said to bubble into their correct positions! Bubble Sort

Sorting. Weiss chapter , 8.6

Transcription:

Sorting Videos!

We will stamp HW Block day: #10 Recursion worksheet #3 #11 12 Recursion-1 Coding Bats #12 Code Step By Step (see canvas) Today we Dance! No Homework tonight :)

Guest Speaker Masters in Computer Science, Bachelor s in Cognitive Science Author of the O Reilly book Designing Voice User Interfaces Former VP of User Experience at Sensly Health App Head of Conversation Design Outreach at Google My sister :)

This week is our final topic! Searching and Sorting Lists of Data

How can we sort like items?

Lego Bubble Sort! 1) 2) 3) 4) 5) Start at the beginning Look at the next element If it is smaller, switch positions The biggest element will float to the top Repeat! Run Time - n^2

Bubble Sort Dance - Go Bears!

Your job: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. Get into a group of 8-10 Choose a videographer Each other group member chooses a unique number Write that number BIG on a piece of paper and tape it to yourself Go outside in the courtyard Line up in a random order Perform a bubble sort to sort yourselves Practice When you are ready, film it Send the video to : cdonaldson@seq.org

On the green piece of paper Try to write the code that would sort an array called numbers of type int public int[] bubblesort(int[] numbers) { //8 lines of code }

Bubble Sort: run-time O(n2) 1) Start at the beginning (or end) 2) The first element looks at the second element. If they are out of order, they switch. 3) The second element looks at the third element. If they are out of order, switch. 4) The biggest element will float to the top (or bottom) and be done 5) Start the process over again at the second element. 6) Keep going

Warm-Up! Coding Bat Recursion-1 : All Star

Warm-Up: CopyEvens

Bubble Sort Code Try to write the code that would sort an array called numbers of type int public int[] bubblesort(int[] numbers) { for (int i = 0; i < numbers.length-1; i++) for (int j = 0; j < numbers.length-1; j++) if (numbers[j] > numbers[j+1]) { int temp = numbers[j]; numbers[j] = numbers[j+1]; numbers[j+1] = temp; } return numbers; }

TODAY: Sort/Search Run-Time The amount of memory it will take to sort or search an array of n elements. It is denoted as O(n), the average sort time. OK: O(n2) BEST: O(n log n)

SEARCH!!!! Sequential: Start at 0 and check each element until you find the element you need. Pros: Works on ANY list, easy to code Con: O(n) Binary: Take a sorted list, look in the middle. If the value you want is less, look halfway down. If it s more, look halfway up. Keep halving until you find your value. DIVIDE AND CONQUER Pro: Fast! O(log n) Con: harder to code

Linear Search vs Binary Search

Linear Search vs Binary Search Linear Binary Rules Can be used Can only be on any list used on sorted lists Big-O n^2 log n

Sequential vs. Binary Search

Warm-Up: AP Sort Question Given this sorted list; 1, 3, 6, 7, 10, 12, 19 a) b) How many comparisons will it take to find the value 10 in a linear search? How many comparisons will it take to find the value 10 in a binary search?

Warm-Up: AP Sort Question Given this sorted list; 1, 3, 6, 7, 10, 12, 19 a) b) How many comparisons will it take to find the value 10 in a linear search? 5 How many comparisons will it take to find the value 10 in a binary search? 3

Warm-Up: AP Sort Question Given this unsorted list; 1, 3, 18, 7, 10, 6, 19 What number will never be found?

Warm-Up: AP Sort Question Given this unsorted list; 1, 3, 18, 7, 10, 6, 19 What number will never be found? 18

Pause: Sort/Search Worksheet # 1

Back to Sorting so we can search

The less efficient but easy to understand sorts O(n2): Bubble Insertion Selection These make n passes through an array of length n to compare two elements at a time. They have long names which makes it easier to remember they are long sorts.

Bubble Sort: run-time O(n2) 1) Start at the beginning (or end) 2) The first element looks at the second element. If they are out of order, they switch. 3) The second element looks at the third element. If they are out of order, switch. 4) The biggest element will float to the top (or bottom) and be done 5) Start the process over again at the second element. 6) Keep going

Bubble Sort - Reverse order (Descending) n^2

Insertion Sort: run-time O(n2) 1) 2) 3) 4) Put elements 0 and 1 in the right order Take element 2. Put it in the right order with 0 and 1. Take element 3. Put it in the right order with 0,1, & 2. Keep going through element n-1

Insertion Sort

Selection Sort: run-time O(n2) 1) Start at the very beginning of n elements 2) Find the smallest element between 0 and n-1. Put it at index 0. 3) Find the smallest element between 1 and n-1. Put it at index 1. 4) Find the smallest element between 2 and n-1. Put it at index 2. 5) Keep going through n passes

Selection Sort

More efficient but harder to understand sorts O(n log n): Merge Sort, Quick Sort These are DIVIDE AND CONQUER sorts that RECURSIVELY divide the array into smaller and smaller arrays to be sorted.

Merge Sort

Quick Sort: run-time O(n log n) 1) Very similar to merge sort, except the array size is not necessarily half. (There are different ways to do this) 2) Instead, choose a PIVOT point that divides the array 3) Recursively sort the array to the left of the pivot point 4) Recursively sort the array to the right of the pivot point 5) Keep going until you hit the base cases (you get to a sorted array) 6) Merge the sorted arrays

Quick Sort

DIVIDE AND CONQUER BINARY SEARCH MERGE & QUICK SORTS (short names, fast search. 5 letters)

Cool Comparison Website https://www.toptal.com/developers/sorting-algorithms

HW: Sort/Search Worksheet # 2,3,4

Slow Searches - Average efficiency n2 Dance How it works Bubble Little Mermaid Items 0 and 1 compare, then swap Items 1 and 2 compare, then swap.. Items n-1 and n compare then swap Repeat n times (The biggest item bubbles to the top) Selection Itik Ititk Find smallest item between 0 and n, put at 0 Find the smallest item between 1 and n, put at 1 Find the smallest item between 2 and n, put at 2 Repeat up to finding smallest between n-1, n Insertion Navy Put items 0 and 1 in order INSERT item 2 in the correct spot between 0 and 1 INSERT item 3 in the correct spot between 0 and 2 Keep inserting items into the sorted part of the list Repeat until you INSERT item n

Fast Searches - Average efficiency n (log n) DIVIDE AND CONQUER BY USING RECURSION!! Dance Merge Quick Buffalo NY, Benny Hill How it works Pick the middle index Split the list into half at that index Split each half into half Keep splitting until you get to the base case of 2 items Sort the base cases by comparing and swapping MERGE the lists of length 2 MERGE the lists of length 4 Repeat until you merge n/2 and n/2 Pick the a pivot index that makes sense Split the list at that index Split each half into half Keep splitting until you get to the base case of 2 items Sort the base cases by comparing and swapping MERGE the lists of length 2 MERGE the lists of length 4 Repeat until you merge n/2 and n/2

Quiz is Coming! Matrix/Recursion/Sort on Friday