Ch. 2: Getting Started.

Slides:



Advertisements
Similar presentations
Chapter 2. Getting Started. Outline Familiarize you with the to think about the design and analysis of algorithms Familiarize you with the framework to.
Advertisements

A Basic Study on the Algorithm Analysis Chapter 2. Getting Started 한양대학교 정보보호 및 알고리즘 연구실 이재준 담당교수님 : 박희진 교수님 1.
2. Getting started Hsu, Lih-Hsing. Computer Theory Lab. Chapter 2P Insertion sort Example: Sorting problem Input: A sequence of n numbers Output:
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS421 - Course Information Website Syllabus Schedule The Book:
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms - 1 Analysis.
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
Analysis of Algorithms CS 477/677
Introduction CIS 606 Spring The sorting problem Input: A sequence of n numbers 〈 a 1, a 2, …, a n 〉. Output: A permutation (reordering) 〈 a’ 1,
Lecture 2 MAS 714 Hartmut Klauck
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
BY Lecturer: Aisha Dawood.  an algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture three Dr. Hamdy M. Mousa.
Getting Started Introduction to Algorithms Jeff Chastine.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started.
Introduction to Complexity Analysis. Computer Science, Silpakorn University 2 Complexity of Algorithm algorithm คือ ขั้นตอนการคำนวณ ที่ถูกนิยามไว้อย่างชัดเจนโดยจะ.
Algorithms A well-defined computational procedure that takes some value as input and produces some value as output. (Also, a sequence of computational.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 4.
ECOE 456/556: Algorithms and Computational Complexity
Algorithms Sorting – Part 3.
Lecture 2 Algorithm Analysis
CMPT 438 Algorithms.
Analysis of Algorithms CS 477/677
Introduction to Algorithms Prof. Charles E. Leiserson
Unit 1. Sorting and Divide and Conquer
Introduction to Algorithms (2nd edition)
Algorithm Analysis CSE 2011 Winter September 2018.
CS 583 Fall 2006 Analysis of Algorithms
CS 3343: Analysis of Algorithms
Growth Functions Algorithms Lecture 8
CS 3343: Analysis of Algorithms
Algorithm Analysis (not included in any exams!)
Analysis of Algorithms CS 477/677
CS 3343: Analysis of Algorithms
Quick Sort (11.2) CSE 2011 Winter November 2018.
Bubble, Selection & Insertion sort
Quicksort analysis Bubble sort
Ch 7: Quicksort Ming-Te Chi
Data Structures Review Session
Ch 2: Getting Started Ming-Te Chi
CS200: Algorithms Analysis
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
CS200: Algorithms Analysis
CS 583 Analysis of Algorithms
CSE 2010: Algorithms and Data Structures Algorithms
Algorithms: the big picture
CSE 373 Data Structures and Algorithms
Algorithms: Design and Analysis
Chapter 9: Medians and Order Statistics
Math/CSE 1019N: Discrete Mathematics for Computer Science Winter 2007
Analysis of Algorithms
Algorithms Sorting.
The Selection Problem.
Algorithms CSCI 235, Spring 2019 Lecture 2 Introduction to Asymptotic Analysis Read Ch. 2 and 3 of Text 1.
nalysis of lgorithms A A Universidad Nacional de Colombia
Divide and Conquer Merge sort and quick sort Binary search
Algorithms and Data Structures Lecture II
Presentation transcript:

Ch. 2: Getting Started

About this lecture Study a few simple algorithms for sorting Insertion Sort Selection Sort (Exercise) Merge Sort Show why these algorithms are correct Try to analyze the efficiency of these algorithms (how fast they run)

The Sorting Problem Input: A list of n numbers Output: Arrange the numbers in increasing order Remark: Sorting has many applications. E.g., if the list is already sorted, we can search a number in the list faster

Insertion sort A good algorithm for sorting a small number of elements It works the way you might sort a hand of playing cards: Start with an empty left hand and the cards face down on the table Then remove one card at a time from the table, and insert it into the correct position in the left hand To find the correct position for a card, compare it with each of the cards already in the hand, from right to left Finally, the cards held in the left hand are sorted

Copyright © The McGraw-Hill Companies, Inc Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

…… Insertion Sort Operates in n rounds At the kth round, kth item Swap towards left side ; Stop until seeing an item with a smaller value. Question: Why is this algorithm correct?

Copyright © The McGraw-Hill Companies, Inc Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Copyright © The McGraw-Hill Companies, Inc Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Correctness of Insertion Sort Initialization: It is true prior to the first iteration of the loop Maintenance: If it is true before an iteration of the loop, it remains true before the next iteration Termination: When the loop terminates Loop invariant: At the start of each iteration of the “outer” for loop— the loop indexed by j— the subarray A[1..j-1] consists of the elements originally subarray A[1..j-1] but in sorted order

Divide and Conquer Divide a big problem into smaller problems  solve smaller problems separately  combine the results to solve original one This idea is called Divide-and-Conquer Smart idea to solve complex problems (why?) Can we apply this idea for sorting ?

Divide-and-Conquer for Sorting What is a smaller problem ?  E.g., sorting fewer numbers  Let’s divide the list to two shorter lists Next, solve smaller problems (how?) Finally, combine the results  “merging” two sorted lists into a single sorted list (how?)

Merge Sort The previous algorithm, using divide-and-conquer approach, is called Merge Sort The key steps are summarized as follows: Step 1. Divide list to two halves, A and B Step 2. Sort A using Merge Sort Step 3. Sort B using Merge Sort Step 4. Merge sorted lists of A and B Question: Why is this algorithm correct?

Copyright © The McGraw-Hill Companies, Inc Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Analyzing the Running Times Which of previous algorithms is the best? Compare their running time on a computer But there are many kinds of computers !!! Standard assumption: Our computer is a RAM (Random Access Machine), so that each arithmetic (such as , , , ), memory access, and control (such as conditional jump, subroutine call, return) takes constant amount of time

Analyzing the Running Times Suppose that our algorithms are now described in terms of RAM operations  we can count # of each operation used  we can measure the running time ! Running time is usually measured as a function of the input size E.g., n in our sorting problem

Insertion Sort (Running Time) The following is a pseudo-code for Insertion Sort. Each line requires constant RAM operations. Why ? tj = # of times key is compared at round j

Insertion Sort (Running Time) Let T(n) denote the running time of insertion sort, on an input of size n By combining terms, we have T(n) = c1n + (c2+c4+c8)(n-1) + c5S tj + (c6+c7) S (tj – 1) The values of tj are dependent on the input (not the input size)

Insertion Sort (Running Time) Best Case: The input list is sorted, so that all tj = 1 Then, T(n) = c1n + (c2+c4+c5+c8)(n-1) = Kn + c  linear function of n Worst Case: The input list is sorted in decreasing order, so that all tj = j-1 Then, T(n) = K1n2 + K2n + K3  quadratic function of n

Worst-Case Running Time In our course (and in most CS research), we concentrate on worst-case time Some reasons for this: 1. Gives an upper bound of running time 2. Worst case occurs fairly often Remark: Some people also study average-case running time (they assume input is drawn randomly)

Try this at home Revisit pseudo-code for Insertion Sort make sure you understand what’s going on Write pseudo-code for Selection Sort

Merge Sort (Running Time) The following is a partial pseudo-code for Merge Sort. The subroutine MERGE(A,p,q,r) is missing. Can you complete it? Hint: Create a temp array for merging

Merge Sort (Running Time) Let T(n) denote the running time of merge sort, on an input of size n Suppose we know that Merge( ) of two lists of total size n runs in c1n time Then, we can write T(n) as: T(n) = 2T(n/2) + c1n when n > 1 T(n) = c2 when n = 1 Solving the recurrence, we have T(n) = K1 n log n + K2 n

Copyright © The McGraw-Hill Companies, Inc Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Which Algorithm is Faster? Unfortunately, we still cannot tell since constants in running times are unknown But we do know that if n is VERY large, worst-case time of Merge Sort must be smaller than that of Insertion Sort Merge Sort is asymptotically faster than Insertion Sort

Homework Problem: 2-2 Exercises: 2.2-1, 2.3-3, 2.3-6