Lecture 2: Sorting and the Big O. Wednesday, 16 September 2009

Similar documents
CSC 273 Data Structures

Chapter 8 Algorithms 1

Outline. Computer Science 331. Three Classical Algorithms. The Sorting Problem. Classical Sorting Algorithms. Mike Jacobson. Description Analysis

Recursion: The Beginning

S1) It's another form of peak finder problem that we discussed in class, We exploit the idea used in binary search.

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

CS/COE 1501

Sorting Algorithms. CSE21 Winter 2017, Day 2 (B00), Day 1-2 (A00) January 11, 2017

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

More Counting Sort and Sorting-by-Key

CS/COE 1501 cs.pitt.edu/~bill/1501/ Introduction

8 Algorithms 8.1. Foundations of Computer Science Cengage Learning

Searching & Sorting in Java Bubble Sort

Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

1 a = [ 5, 1, 6, 2, 4, 3 ] 4 f o r j i n r a n g e ( i + 1, l e n ( a ) 1) : 3 min = i

Lecture 11: In-Place Sorting and Loop Invariants 10:00 AM, Feb 16, 2018

Recursion: The Beginning

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

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

Lecture Notes for Chapter 2: Getting Started

Lecture 3.4: Recursive Algorithms

Week 12: Running Time and Performance

Sorting Pearson Education, Inc. All rights reserved.

Sorting. Chapter 12. Objectives. Upon completion you will be able to:

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

Binary Search and Worst-Case Analysis

Scan and Quicksort. 1 Scan. 1.1 Contraction CSE341T 09/20/2017. Lecture 7

overview overview who practicalities introduction data structures and algorithms lecture 1 sorting insertion sort pseudo code merge sort

Binary Search and Worst-Case Analysis

RAM with Randomization and Quick Sort

CS 1110: Introduction to Computing Using Python Loop Invariants

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

Scan and its Uses. 1 Scan. 1.1 Contraction CSE341T/CSE549T 09/17/2014. Lecture 8

LING/C SC/PSYC 438/538. Lecture 18 Sandiway Fong

Lecture 2: Getting Started

Lecture 5 Sorting Arrays

EECS 203 Spring 2016 Lecture 8 Page 1 of 6

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103. Chapter 2. Sets

SORTING AND SEARCHING

What is an algorithm? CISC 1100/1400 Structures of Comp. Sci./Discrete Structures Chapter 8 Algorithms. Applications of algorithms

CISC 1100: Structures of Computer Science

Practical Questions CSCA48 Winter 2018 Week 11

ECE 2574: Data Structures and Algorithms - Big-O Notation. C. L. Wyatt

n 1 x i = xn 1 x 1. i= (2n 1) = n 2.

CSE 230 Computer Science II (Data Structure) Introduction

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

CS3110 Spring 2017 Lecture 18: Binary Search Trees

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

CSc 225 Algorithms and Data Structures I Case Studies

Searching & Sorting in Java Shell Sort

Dual Sorting Algorithm Based on Quick Sort

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

Lecture 15 : Review DRAFT

ECE 122. Engineering Problem Solving Using Java

Lecture 9: Sorting Algorithms

Sorting. Bubble Sort. Selection Sort

1 The sorting problem

Chapter Contents. An Introduction to Sorting. Selection Sort. Selection Sort. Selection Sort. Iterative Selection Sort. Chapter 9

Faster Sorting Methods

Chapter 3, Algorithms Algorithms

Computational Geometry: Lecture 5

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

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Data Structures and Algorithms CSE 465

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

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

CSE 143 Lecture 14. Sorting

CS Sorting Terms & Definitions. Comparing Sorting Algorithms. Bubble Sort. Bubble Sort: Graphical Trace

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Analysis of Sorting Algorithms. Imagine you have a few thousand dollars in your safe in all different denominations. The

(the bubble footer is automatically inserted into this space)

CS4311 Design and Analysis of Algorithms. Lecture 1: Getting Started

Sorting. Introduction. Classification

Algorithms (7A) Young Won Lim 4/18/17

Algorithms Overview (1A) Young Won Lim 3/29/18

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

Copyright 2009, Artur Czumaj 1

CS 157: Assignment 5

Algorithms. Notations/pseudo-codes vs programs Algorithms for simple problems Analysis of algorithms

CSC 273 Data Structures

Algorithm Analysis CISC4080 CIS, Fordham Univ. Instructor: X. Zhang

Lecture 1. Introduction

Discrete Mathematics and Probability Theory Fall 2009 Satish Rao,David Tse Note 8

CS126 Final Exam Review

CSE 20 DISCRETE MATH WINTER

CS2351 Data Structures. Lecture 1: Getting Started

Introduction to Programming: Lecture 6

Binary Search. Roland Backhouse February 5th, 2001

CS 137 Part 7. Big-Oh Notation, Linear Searching and Basic Sorting Algorithms. November 10th, 2017

Classic Data Structures Introduction UNIT I

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

Discrete Mathematics for CS Spring 2008 David Wagner Note 13. An Introduction to Graphs

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

CS 103 Unit 8b Slides

COSC242 Lecture 7 Mergesort and Quicksort

Lecture 15: Algorithms. AP Computer Science Principles

CITS3001. Algorithms, Agents and Artificial Intelligence. Semester 2, 2016

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

Data Structures and Algorithms (DSA) Course 13 Algorithms

Transcription:

Lecture 2: Sorting and the Big O CS204/209 : Algorithms (and Scientific Computing) Niall Madden Wednesday, 16 September 2009 CS204/209 Lecture 2: Sorting and the Big O 1/18

In today s lecture 1 Recall... Algorithms 2 Sorting Bubble Sort Insertion Sort 3 The correctness of algorithms Loop Invariants 4 Comparing Algorithms 5 The Big-O Most of the content is based on Chap. 2 of Cormen et al s Introduction to Algorithms, and Sections 2.1 and 2.2 of Rosen s Discrete Mathematics (511 ROS). CS204/209 Lecture 2: Sorting and the Big O 2/18

Last week s problem Problem 1 6 busybodies each have a piece of news. How many phone calls are required so that all of them know all the news? Any ideas? CS204/209 Lecture 2: Sorting and the Big O 3/18

Recall... Algorithms Definition An Algorithm is a finite set of precise instructions for performing a computation or for solving a problem. CS204/209 Lecture 2: Sorting and the Big O 4/18

Sorting The basic problem is Given a list of integers {a 1, a 2,..., a n } rearrange them into a list {b 1, b 2,..., b n } such that b 1 b 2 b n. Methods that we will look at include the Bubble, Insertion, Shaker, Merge, Quick and Tournament sort algorithms. CS204/209 Lecture 2: Sorting and the Big O 5/18

Sorting Bubble Sort Bubble sort is one of the simplest and slowest algorithms for sorting. The basic idea is go through the list {a 1, a 2,..., a n 1, a n } from left to right. If an adjacent pair of element are out of order, swap them. At the end of the 1st pass, the largest element should be at the end. Now repeat the process for the list {a 1, a 2,..., a n 1 }. and then for the list {a 1, a 2,..., a n 2 }. Continue until you have sorted the list {a 1, a 2 }. CS204/209 Lecture 2: Sorting and the Big O 6/18

Sorting Bubble Sort Here is a pseudo-code description of the algorithm: BubbleSort(a 1, a 2,..., a n ) FOR i 1, 2,..., n 1 FOR j 1, 2,..., n i IF a j > a j+1 swap(a j, a j+1 ) END END END Bubble Sort CS204/209 Lecture 2: Sorting and the Big O 7/18

Sorting Bubble Sort Example Use Bubble Sort to arrange the following integers in ascending order { 9, 12, 3, 2, 6, 8 } CS204/209 Lecture 2: Sorting and the Big O 8/18

Sorting Insertion Sort Insertion Sort This can be thought of as being like how one might sort a hand of cards. Start with the cards face down on the table. 1 Pick the first card: it forms the trivially sorted list {a 1 }. 2 Pick the next card from the table, and insert it into the correct place (i.e., before or after a 1 ), giving the sorted list {a 1, a 2 }. 3 Pick a third cards and insert it into the correct place in the list. Now we have sorted {a 1, a 2, a 3 }. 4 In general, at step j, insert a j into the correct location of {a 1, a 2,..., a j 1 }. CS204/209 Lecture 2: Sorting and the Big O 9/18

Sorting Insertion Sort Pseudocode for Insertion Sort 1: for j = 2 to n do 2: key a j 3: i j 1 4: while i > 0 and a i > key do 5: a i+1 a i 6: i i 1 7: end while 8: a i+1 key 9: end for CS204/209 Lecture 2: Sorting and the Big O 10/18

Sorting Insertion Sort Example Use Insertion Sort to arrange the following integers in ascending order { 9, 12, 3, 2, 6, 8 } CS204/209 Lecture 2: Sorting and the Big O 11/18

The correctness of algorithms The algorithm described above seems to work: after it has run we end up with a sorted list. But how can we prove this. The idea is to use a Loop Invariant this notion will repeat through-out the course. At the end of each pass j through the algorithm given above, the entries in the sublist {a 1, a 2,..., a j } are fist j entries of the original list, but in sorted order. But we need to state this a bit more formally... CS204/209 Lecture 2: Sorting and the Big O 12/18

The correctness of algorithms Loop Invariants To prove an algorithm works using a loop invariant, we use Initialisation: before the first iteration of the loop, the statement is true. Maintenance: if it is true at the start of an iteration of the loop, it is true at the end of it. Termination: conclude that when the loop finishes, the statement is true. Now lets use this idea to show that Insertion Sort works: CS204/209 Lecture 2: Sorting and the Big O 13/18

Comparing Algorithms We now have two algorithms for sorting a list. In general, how do we know which one to choose? One of the most important determinants is the amount of effort required for the method. That requires us to have some way of expressing the expense associated with a given procedure for solving a particular size. Exercise Suppose we have two algorithms for sorting a list of length n. One takes 100n 2 operations and the other takes n 3 + n. Which should we use? CS204/209 Lecture 2: Sorting and the Big O 14/18

The Big-O The above exercise is interesting, but very simplified/idealised. The time taken to run the same algorithm on the same problem is very likely to vary from one computer to another. But this difference probably won t depend on n the size of the problem. So we use the Big-O notation. CS204/209 Lecture 2: Sorting and the Big O 15/18

The Big-O Definition (Big-O) Given functions f : N R and g : N R, we say that f (x) is O ( g(x) ) if there are constants C and k such that for all x > k. f (x) C g(x) In English, we read this as f is big-oh of g. The Big-O notation was introduced by Paul Bachmann (Germany) in 1892. The symbol is sometimes called the Landau symbol in honour of Edmund Landau. It was introduced into computer science by (the legendary) Donald Knuth. CS204/209 Lecture 2: Sorting and the Big O 16/18

The Big-O Example Show that f (x) = x 2 + 2x + 1 is O(x 2 ). Note that x 2 is also O ( x 2 + 2x + 1 ). CS204/209 Lecture 2: Sorting and the Big O 17/18

The Big-O Example Show that f (x) = 7x 2 is O(x 3 ). Is it the case that x 3 is O(7x 2 )? CS204/209 Lecture 2: Sorting and the Big O 18/18