Discrrete mathematics for computer science asymptotic warmup

4 70 0
Discrrete mathematics for computer science asymptotic warmup

Đang tải... (xem toàn văn)

Thông tin tài liệu

Order Notation Warmup Analyzing Bubble Sort Sort (a1,…,an): for i := to n-1 for j := to n-i if aj>aj+1 then interchange aj↔aj+1 How many comparison steps as a function of n? • Inner loop is executed n-1 times when i=1 n-2 times when i=2 … time when i=n-1 For a total of 1+2+3+…+(n-2)+(n-1) =((n-1)∙n)/2 The exact running time depends on details of the code (initializations, for example), the quality of the compiler, the speed of the computer, etc Let T(n) be the maximum running time of this program for any array of length n (Small variations due to whether swaps actually occur) • T(n) = a2n2 + a1n + a0 for some constants a0, a1, a2 • But the last two terms become increasing inconsequential as n increases since a1n + a0 = o(a2n2) • So the most useful thing to say is that T(n)=�(n2) (why both upper and lower bound?) ... i=n-1 For a total of 1+2+3+…+(n-2)+(n-1) =((n-1)∙n)/2 The exact running time depends on details of the code (initializations, for example), the quality of the compiler, the speed of the computer, ...Analyzing Bubble Sort Sort (a1,…,an): for i := to n-1 for j := to n-i if aj>aj+1 then interchange aj↔aj+1 How many comparison steps as a function... be the maximum running time of this program for any array of length n (Small variations due to whether swaps actually occur) • T(n) = a2n2 + a1n + a0 for some constants a0, a1, a2 • But the last

Ngày đăng: 22/03/2019, 10:57

Từ khóa liên quan

Mục lục

  • Slide 1

  • Analyzing Bubble Sort

  • Slide 3

  • Slide 4

Tài liệu cùng người dùng

Tài liệu liên quan