1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.

Slides:



Advertisements
Similar presentations
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Advertisements

CSE 3101: Introduction to the Design and Analysis of Algorithms
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
Chapter 2. Getting Started. Outline Familiarize you with the to think about the design and analysis of algorithms Familiarize you with the framework to.
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.
Quicksort Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 2.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Introduction to Analysis of Algorithms
Sorting. Input: A sequence of n numbers a 1, …, a n Output: A reordering a 1 ’, …, a n ’, such that a 1 ’ < … < a n ’
Ch. 7 - QuickSort Quick but not Guaranteed. Ch.7 - QuickSort Another Divide-and-Conquer sorting algorithm… As it turns out, MERGESORT and HEAPSORT, although.
CS421 - Course Information Website Syllabus Schedule The Book:
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
Merge sort, Insertion sort
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms - 1 Analysis.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort or bubble sort 1. Find the minimum value in the list 2. Swap it with the value.
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
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
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,
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
Introduction to Algorithm design and analysis
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.
Algorithm Correctness A correct algorithm is one in which every valid input instance produces the correct output. The correctness must be proved mathematically.
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.
Jessie Zhao Course page: 1.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
ECOE 456/556: Algorithms and Computational Complexity Lecture 1 Serdar Taşıran.
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.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort (iterative, recursive?) * Bubble sort.
1Computer Sciences Department. Book: Introduction to Algorithms, by: Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest Clifford Stein Electronic:
September 17, 2001 Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
September 9, Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Algorithms IS 320 Spring 2015 Sorting. 2 The Sorting Problem Input: –A sequence of n numbers a 1, a 2,..., a n Output: –A permutation (reordering) a 1.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Sorting Algorithm Analysis. Sorting  Sorting is important!  Things that would be much more difficult without sorting: –finding a phone number in the.
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started.
Lecture 4 1 Advance Analysis of Algorithms. Selection Sort 2 Summary of Steps Find the smallest element in the array Exchange it with the element in the.
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 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
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.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
September 18, Algorithms and Data Structures Lecture II Simonas Šaltenis Aalborg University
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Lecture 2 Algorithm Analysis
CMPT 438 Algorithms.
Unit 1. Sorting and Divide and Conquer
Algorithm Analysis CSE 2011 Winter September 2018.
Analysis of Algorithms CS 477/677
Ch 7: Quicksort Ming-Te Chi
Ch 2: Getting Started Ming-Te Chi
CSE 373 Data Structures and Algorithms
Ch. 2: Getting Started.
Analysis of Algorithms
Presentation transcript:

1 Ch. 2: Getting Started

2 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)

3 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

4 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

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

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

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

8

9 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

10 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 ?

11 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?)

12 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?

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

14 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

15 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

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

17 Let T(n) denote the running time of insertion sort, on an input of size n By combining terms, we have T(n) = c 1 n + (c 2 +c 4 +c 8 )(n-1) + c 5  t j + (c 6 +c 7 )  (t j – 1) The values of t j are dependent on the input (not the input size) Insertion Sort (Running Time)

18 Best Case: The input list is sorted, so that all t j = 1 Then, T(n) = c 1 n + (c 2 +c 4 +c 5 +c 8 )(n-1) = Kn + c  linear function of n Worst Case: The input list is sorted in decreasing order, so that all t j = j-1 Then, T(n) = K 1 n 2 + K 2 n + K 3  quadratic function of n Insertion Sort (Running Time)

19 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) Worst-Case Running Time

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

21 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

22 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 c 1 n time Then, we can write T(n) as: T(n) = 2T(n/2) + c 1 n when n > 1 T(n) = c 2 when n = 1 Solving the recurrence, we have T(n) = K 1 n log n + K 2 n Merge Sort (Running Time)

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

24 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 Which Algorithm is Faster?

25 Exercises Problem 1-1*, 2-4 Exercises 2.2-2, 2.2-4, 2.3-7*