ET 2006 : Data Structures & Algorithms

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Analysis of Algorithms
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
the fourth iteration of this loop is shown here
CSE 373: Data Structures and Algorithms
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms - 1 Analysis.
Algorithm Efficiency and Sorting
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.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting CS 202 – Fundamental Structures of Computer Science II Bilkent.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a lgorithms.
Week 2 CS 361: Advanced Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
Asymptotic Notation (O, Ω, )
Data Structure Introduction.
Growth of Functions. 2 Analysis of Bubble Sort 3 Time Analysis – Best Case The array is already sorted – no swap operations are required.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Time Complexity. Solving a computational program Describing the general steps of the solution –Algorithm’s course Use abstract data types and pseudo code.
Algorithms A well-defined computational procedure that takes some value as input and produces some value as output. (Also, a sequence of computational.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8a. Sorting(1): Elementary Algorithms.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Sort Algorithm.
CMPT 438 Algorithms.
CSCE 210 Data Structures and Algorithms
Chapter 2 Algorithm Analysis
Design and Analysis of Algorithms
Introduction to Algorithms: Asymptotic Notation
Analysis of Algorithms
Introduction to Search Algorithms
COMP108 Algorithmic Foundations Algorithm efficiency
Introduction to Algorithms
Big-O notation.
Introduction to Algorithms
Algorithm Analysis CSE 2011 Winter September 2018.
CS 583 Fall 2006 Analysis of Algorithms
Enough Mathematical Appetizers!
CS 3343: Analysis of Algorithms
Computation.
Algorithm Analysis (not included in any exams!)
Analysis of Algorithms CS 477/677
Objectives At the end of the class, students are expected to be able to do the following: Understand the purpose of sorting technique as operations on.
Asymptotic Notations Algorithms Lecture 9.
Describing algorithms in pseudo code
Bubble, Selection & Insertion sort
Foundations II: Data Structures and Algorithms
CS 3343: Analysis of Algorithms
CSC 413/513: Intro to Algorithms
Introduction to Algorithms Analysis
CS 3343: Analysis of Algorithms
Applied Discrete Mathematics Week 6: Computation
Big-O, Ω, ϴ Trevor Brown CSC263 Tutorial 1 Big-O, Ω, ϴ Trevor Brown
CS200: Algorithms Analysis
Searching, Sorting, and Asymptotic Complexity
Asst. Dr.Surasak Mungsing
Introduction to Algorithm and its Complexity Lecture 1: 18 slides
Ch. 2: Getting Started.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
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.
Estimating Algorithm Performance
CSE 373 Sorting 2: Selection, Insertion, Shell Sort
Algorithms Presented By:- Mr. Anup Ashok Shinde BBA (C.A) Dept.
Algorithms CSCI 235, Spring 2019 Lecture 13 Elementary Sorts II
CS 2210 Discrete Structures Algorithms and Complexity
Presentation transcript:

ET 2006 : Data Structures & Algorithms Lecture 4: Sorting & Complexity Analysis Malaka Walpola

Outline Simple Sorting Algorithms Analyzing Algorithms Insertion Sort Bubble Sort Selection Sort Analyzing Algorithms Analysis of Insertion Sort Asymptotic Notation Analysis of Bubble Sort Analysis of Selection Sort

Learning Outcomes After successfully studying contents covered in this lecture, students should be able to, explain the insertion sort, bubble sort & selection sort algorithms explain the major factors considered for analyzing algorithms explain the use of asymptotic analysis to examine the growth of functions express the time & space complexity of simple (non recursive) algorithms using asymptotic notation

Simple Sorting Algorithms

The Sorting Problem Input: Output: A More General Version A sequence of n numbers Output: A permutation (ordering) of the input sequence such that A More General Version *Source [1]

Insertion Sort Suitable for Sorting Small Number of Elements The Idea Remove an element from input and insert it in proper location Image taken from [1]

Insertion Sort The Idea Image taken from [3]

The Algorithm INSERTION-SORT(A) for j = 1 to A.length-1 key = A[j] //Insert A[j] into the sorted sequence A[0, 1, ……, j-1]. i = j-1 while i > -1 and A[i] > key A[i+1] = A[i] i = i-1 A[i+1] = key

Bubble Sort The Idea: Bubble up the largest(smallest) element to the bottom(top) of the list in each iteration Repeatedly step through the list Compare each pair of adjacent items Swap them if they are in the wrong order Repeated until no swaps are needed, which indicates that the list is sorted

The Algorithm BUBBLE-SORT(A) do swapped = false for i = 1 to A.length-1 if A[i-1] > A[i] temp = A[i] A[i] = A[i-1] A[i-1] = temp swapped = true while swapped

Selection Sort The Idea: Progression Repeatedly step through the list First select the smallest element in the list and swap it with L[0] Next select the second-smallest element and swap it with L[1] Continue…; finally select the second-largest element, swap it with L[n-2], leaving the largest element at L[n-1] List L[ ] is now sorted in ascending order Repeatedly step through the list Select (i+1)th smallest element and swap it with item in index i

The Algorithm SELECTION-SORT(A) for i = 0 to A.length-1 //Select (i+1)th smallest element int minJ = i; for j = i+1 to A.length-1 //if this element is less, then it’s the new min if (A[j] < A[minJ]) minJ = j; //swap (i+1)th smallest element with element in index i if(minJ != i) int temp = A[minJ]) A[minJ] = a[i]) a[i] = temp

Analyzing Algorithms

Analyzing Algorithms What are the factors that affect the running time of a program? The processing CPU speed Memory Size of input data set Nature of input data set

Analyzing Algorithms Factors to Consider Speed/Amount of work done/Efficiency Space efficiency/ Amount of memory used Simplicity We want an analysis that does not depend on CPU speed Memory

Analyzing Algorithms Why? Thus, we use Asymptotic Analysis We want to have a hardware independent analysis Thus, we use Asymptotic Analysis Ignore machine dependant constants Look at the growth of the running time Running time of an algorithm as a function of input size n for large n Written using Asymptotic Notation

Analyzing Algorithms Kinds of Analysis Worst-case: (usually) Average-case: (sometimes) Need assumption of statistical distribution of inputs. Best-case: (bogus?)

Analysis of Insertion Sort Code Cost Times INSERTION-SORT(A) 1. for j = 1 to A.length-1 2. key = A[j] 3. //………… 4. i = j-1 5. while i > -1 and A[i] > key 6. A[i+1] = A[i] 7. i = i-1 8. A[i+1] = key n = A.length tj = the number of times the while loop test in line 5 is executed

Analysis of Insertion Sort Code Cost Times INSERTION-SORT(A) ---- --- 1. for j = 1 to A.length-1 C1 n 2. key = A[j] C2 n-1 3. //………… 4. i = j-1 C4 5. while i > -1 and A[i] > key C5 6. A[i+1] = A[i] C6 7. i = i-1 C7 8. A[i+1] = key C8 n = A.length tj = the number of times the while loop test in line 5 is executed

Analysis of Insertion Sort Best Case – Sorted Array tj = 1 for j=2, 3, ……, n

Analysis of Insertion Sort Worst Case – Reverse Sorted Array tj = j for j=2, 3, ……, n

Asymptotic Notation Θ-Notation Asymptotically tight bound Θ(g(n)) = {f(n) : there exist positive constants c1, c2, and n0 such that 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0}

Asymptotic Notation O-Notation A General Guideline Asymptotic upper bound O(g(n)) = {f(n) : there exist positive constants c, and n0 such that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0} A General Guideline Ignore lower order terms Ignore leading constants

The O Notation - Examples f(x) = 3x g(x) = x C = 3, N =0 f(x) ≤ 3*g(x) f(x) = 4+7x g(x) = x C = 8, N =4 f(x) ≤ 8*g(x) 8*g(x) f(x) = 4+7x f(x) = 3x = 3g(x) g(x) = x g(x) = x N 3x = O(x) 4+7x = O(x)

The O Notation - Exercises f(x) = x+100x2 g(x) = x2 Show that f(x) = O(g(x)) f(x) = 20+x+5x2+8x3 g(x) = x3 Show that f(x) = O(g(x))

The θ Notation - Examples f(x) = 3x g(x) = x C1 = 1, N =0, C2 = 3 Show g(x) ≤ f(x) ≤ 3*g(x) g(x) = x < f(x) f(x) = 3x = 3g(x) Therefore f(x) = 3x = θ(x) = θ(g(x)) f(x) = 4+7x g(x) = x C1 = 7, N =4, C2 = 8 Show 7*g(x) ≤ f(x) ≤ 8*g(x) 7*g(x) = 7x < f(x) f(x) = 7x+4 ≤ 8x (when x≥4) Therefore f(x) = 7x+4 = θ(x) = θ(g(x))

The θ Notation - Exercises f(x) = x+100x2 g(x) = x2 Show that f(x) = θ(g(x)) f(x) = 20+x+5x2+8x3 g(x) = x3 Show that f(x) = θ(g(x))

Asymptotic Notation Example Macro Substitution Convention: A set in a formula represents an anonymous function in the set f(n) = n3 + O(n2) f(n) = n3 + h(n) for some h(n) ∈ O(n2)

Asymptotic Notation O-Notation Other Notations (Self-Study) May or may not be asymptotically tight Other Notations (Self-Study) Ω-notation Asymptotic lower bound o-notation Asymptotic upper bound (Not asymptotically tight) ω-notation Asymptotic lower bound (Not asymptotically tight)

Analysis of Bubble Sort Code Cost Times BUBBLE-SORT(A) do 2. swapped ← false 3. for i = 1 to A.length 4. if A[i-1] > A[i] 5. temp ← A[i] 6. A[i] ← A[i-1] 7. A[i-1] ← temp 8. swapped ← true 9. while swapped

Analysis of Selection Sort Code Cost Times SELECTION-SORT(A) for i = 0 to A.length-1 2. //Select (i+1)th smallest element 3. int minJ = i; 4. for j = i+1 to A.length-1 5. //if this element is less, then it’s the new min 6. if (A[j] < A[minJ]) 7. minJ = j

Analysis of Selection Sort Code Cost Times SELECTION-SORT(A) 8. //swap (i+1)th smallest element with element in index I 9. if(minJ != i) 10. int temp = A[minJ]) 11. A[minJ] = A[i]) 12. A[i] = temp

Homework Study the Ω, o and ω asymptotic notations Reading Assignment What are the relationships between the Θ, O, Ω, o and ω notations? Reading Assignment IA –Sections 3.1 (Asymptotic Notation)

References [1] T.H. Cormen, C.E. Leiserson, R.L. Rivest and C. Stein, Introduction to Algorithms, 3rd Ed. Cambridge, MA, MIT Press, 2009. [2] S. Baase and Allen Van Gelder, Computer Algorithms: Introduction to Design and Analysis, 3rd Ed. Delhi, India, Pearson Education, 2000. [3] Lecture slides from Prof. Erik Demaine of MIT, available at http://dspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall-2004/NR/rdonlyres/Electrical-Engineering-and-Computer-Science/6-046JFall-2004/B3727FC3-625D-4FE3-A422-56F7F07E9787/0/lecture_01.pdf