Você está na página 1de 1

APCSA

Unit 4—Searching and Sorting

Big-O
• Can be used to describe an algorithm's efficiency.
 Our focus is on time, but Big-O is used to describe storage efficiency, as well.
• Formally: "f(x) is O(g(x))" means that there exist positive x0 and positive k such that f ( x) ≤ kg ( x) for all

x ≥ x0 .

 example: x 2 + 3 x + 2 is O( x 2 ) , as x 2 + 3 x + 2 ≤ 3x 2 for all x ≥ 2 .

→ Here, my k is 3 and my x0 is 2.

∗ Can you find another k and x0 ?

→ Normally, our f (x) is not so nicely defined.

∗ Instead of " x 2 + 3 x + 2 ," our f (x) will be something like "selection sort".

∗ g (x) is determined by analyzing the implementation of the algorithm.

→ Is it true that x 2 + 3 x + 2 is O( x 3 ) , also?

∗ Yes (example: x 2 + 3 x + 2 ≤ 6x 3 for all x ≥ 1 )


♦ We will use Big-O to imply a tight upper bound, however.
• There are several orders that arise frequently:
 O(1)
→ constant time
 O(log n)
→ example: binary search, where n is the size of the array being searched
 O(n)
→ example: linear search, where n is the size of the array being searched
 O(n log n)
→ example: mergesort, where n is the size of the array being sorted
 O(n 2 )

→ example: selection sort, where n is the size of the array being sorted
• More than one variable may be used.
 example: The Needleman-Wunsch algorithm for aligning two character sequences of lengths m and n is
O(mn) .

Você também pode gostar