Docsity

Quicker Sorting - Lecture Slides - Computing: Explorations in Language | CS 1120, Study notes of Computer Science

Toggle sidebar
bg1
Greg Humphreys
http://www.cs.virginia.edu/~humper
CS150: Computer Science
University of Virginia
Computer Science
Lecture 10: Quicker Sorting
CS150 Fall 2005: Lecture 10: Measuring Work 2
Menu
• compose and n-times
• Measuring Work:
What ! really means
• Quicker Sorting
CS150 Fall 2005: Lecture 10: Measuring Work 3
What does ! really mean?
•O(x) – it is no more than x work
(upper bound)
•"(x) – work scales as x (tight bound)
•#(x) – it is at least x work
(lower bound)
If O(x) and #(x) are true,
then "(x) is true.
CS150 Fall 2005: Lecture 10: Measuring Work 4
Meaning of O (“big Oh”)
means:
There is a positive constant c
such that
for all
f(x) = O(g(x))
x > x0
CS150 Fall 2005: Lecture 10: Measuring Work
Huh?
5
g(x)
is an
upper bound
on
f(x)
cf(x)
f(x)
x0
g(x)
CS150 Fall 2005: Lecture 10: Measuring Work 6
O Examples
x is O (x2)? Yes, c = 1 works fine.
10x is O (x)? Yes, c = .09 works fine.
x2 is O (x)? No, no matter what c we pick,
cx2 > x for big enough x
pf3
pf4

Partial preview of the text

Download Quicker Sorting - Lecture Slides - Computing: Explorations in Language | CS 1120 and more Study notes Computer Science in PDF only on Docsity!

Greg Humphreys http://www.cs.virginia.edu/~humper CS150: Computer Science University of Virginia Computer Science

Lecture 10: Quicker Sorting

CS150 Fall 2005: Lecture 10: Measuring Work 2

Menu

  • compose and n-times
  • Measuring Work:

What! really means

  • Quicker Sorting CS150 Fall 2005: Lecture 10: Measuring Work (^3)

What does! really mean?

  • O( x ) – it is no more than x work

(upper bound)

  • "( x )^ –^ work^ scales^ as^ x^ (tight^ bound)
  • #( x )^ –^ it^ is^ at least^ x^ work

(lower bound)

If O( x ) and #( x ) are true,

then "( x ) is true.

CS150 Fall 2005: Lecture 10: Measuring Work (^4)

Meaning of O (“big Oh”)

means:

There is a positive constant c

such that

for all

f (x) = O(g(x))

cf (x) < g(x)

x > x 0

Huh?

g(x) is an

upper bound

on f^ (x)

cf (x)

f (x)

x 0

g(x)

O Examples

x is O ( x^2 )? Yes,^ c = 1^ works^ fine.

10 x is O ( x )? Yes,^ c = .09^ works^ fine.

x^2 is O ( x )?

No, no matter what c we pick, cx^2 > x for big enough x

CS150 Fall 2005: Lecture 10: Measuring Work 7

Lower Bound: # (Omega)

means:

There is a positive constant c

such that

for all x > x 0

f (x) = Ω(g(x))

cf (x) > g(x)

Difference from O – this was < CS150 Fall 2005: Lecture 10: Measuring Work 8

Examples

• x is # ( x )

  • Yes, pick c = 2

• 10 x is # ( x )

  • Yes,^ pick^ c^ =^1

• Is x^2 # ( x )?

  • Yes!

• x is O( x )

  • Yes,^ pick^ c^ =^.

• 10 x is O ( x )

  • Yes, pick c =.

• x^2 is not O ( x )

CS150 Fall 2005: Lecture 10: Measuring Work (^9)

Tight Bound:! (Theta)

f (x) = Θ(g(x))

f (x) = O(g(x))

f (x) = Ω(g(x))

means

and

CS150 Fall 2005: Lecture 10: Measuring Work (^10)

! Examples

• 10 x is! ( x )

– Yes, since 10 x is # ( x ) and 10 x is O( x )

  • Doesn’t matter that you choose different c
values for each part; they are independent

• x^2 is/is not! ( x )?

  • (^) No, since x^2 is not O ( x )

• x is/is not! ( x^2 )?

  • (^) No, since x^2 is not # ( x )

Sorting

(define (simple-sort cf lst) (if (null? lst) lst (let ((best (find-best cf lst))) (cons best (simple-sort cf (delete lst most)))))) (define (find-best cf lst) (insertl (lambda (c1 c2) (if (cf c1 c2) c1 c2)) lst (car lst)))

simple-sort is "( n^2 )

If we double the length of the list, we amount of work sort does approximately quadruples.

Divide and Conquer sorting?

  • simple-sort: find the lowest in the list, add it to the front of the result of sorting the list after deleting the lowest
  • Insertion sort: insert the first element of the list in the right place in the sorted rest of the list

Can we do better?

(insertone < 88 (list 1 2 3 5 6 23 63 77 89 90)) Suppose we had procedures

(first-half lst)
(second-half lst)

that quickly divided the list in two halves?

Charge

  • Read Tyson’s essay (before Friday)
    • How^ does^ it^ relate^ to^!^ ( n^2 )
    • How does it relate to grade inflation
    • Don’t misinterpret it as telling you to run out
and get tattoos and piercings!