Course navigation & on this pageLecture 06 · CS-702
Asymptotic analysis & growth rates
Read Big O, Big Omega and Big Theta precisely, and compare how algorithms scale.
Intermediate~55 min guideSource-based learning
What you’ll understand
Use the quantified definitions of O, Ω and Θ.
Analyze linear, nested and halving loops.
Distinguish growth rate from elapsed time.
Compare searching and sorting strategies.
Before you begin: Lecture 04: operation counts · Logarithms and inequalities
Expanded study guide
What Big O tells you
The supplied lecture emphasizes scaling: how the resource requirement changes when more data is supplied. Time complexity models work; space complexity models memory. Neither directly gives a wall-clock duration without additional assumptions.
Constants can matter for small inputs, while a larger growth rate eventually dominates any fixed constant. Therefore benchmark practical sizes and use asymptotic analysis to understand the trend.
Big O: an eventual upper bound
For nonnegative cost functions, f(n) is O(g(n)) if there exist constants c > 0 and n₀ such that f(n) ≤ c g(n) for every n ≥ n₀. The constants must be independent of n.
For f(n) = 3n + 2, take c = 5 and n₀ = 1 to prove O(n). This function is also O(n²), but the linear bound is tighter. Big O does not inherently mean worst case: specify which cost function you are bounding.
f(n)∈O(g(n))⟺∃c>0,n0:0≤f(n)≤cg(n)∀n≥n0
Big Omega and Big Theta
Ω(g(n)) is an eventual lower bound. Θ(g(n)) gives matching upper and lower bounds up to positive constant factors. A statement of Θ(n²) describes a tight growth class.
For 3n + 2 and n ≥ 1, the function lies between 3n and 5n. That proves Θ(n). An algorithm’s best-case time and a problem’s lower bound are different concepts; do not use the terms interchangeably.
A loop that increments i once from 0 to n−1 performs n iterations. If an inner loop also runs n times for each outer iteration, multiply to get n². If the inner bound is i, sum the varying inner counts to get a triangular number.
When a positive counter doubles each iteration, it reaches n after about log₂ n iterations. If the counter increases by consecutive integers, its value after k steps is k(k+1)/2, so reaching n takes Θ(√n) steps.
Do not infer complexity from the number of loops alone.
An inner loop may have a bound independent of n.
Early return can make best and worst cases different.
Separate preprocessing from per-query work.
1+2+⋯+k=2k(k+1)⇒k=Θ(n)
Logarithms count repeated division
The equation 2ᵏ = n is equivalent to k = log₂ n. A sorted region of 1024 items can be halved ten times to reach one item. A final inspection may still be needed, depending on the exact search convention.
Changing the logarithm base multiplies it by a constant, which does not affect Θ(log n). The supplied workbooks retain the original examples, formulas and cached values for studying powers and logarithms.
logbn=logablogan
Sorting and partitioning
The source references insertion, bubble and selection sorting and emphasizes partitioning. Selection sort repeatedly selects the next minimum and performs quadratic comparisons. Insertion sort inserts each next element into an already sorted prefix; it can be linear on already sorted input.
Merge sort divides the input, sorts each half and merges in linear work per level, giving Θ(n log n). Quicksort partitions around a pivot. Balanced partitions lead to Θ(n log n), while repeatedly extreme partitions can give Θ(n²). The supplied Quick implementation shuffles before partitioning, so its expected performance assumes that randomization works as intended.
Three bubble-sort variants in the source
Pages 29–45 develop bubble sort by comparing adjacent values and exchanging inverted pairs. The first pseudocode runs n−1 passes, each with n−1 comparisons, for (n−1)² comparisons even if the input is already sorted.
The first enhancement shortens the inner loop after each pass because the largest remaining value has reached its final position. Its comparison count becomes (n−1) + (n−2) + … + 1 = n(n−1)/2. This reduces the count but keeps Θ(n²) growth.
The second enhancement records whether any exchange occurs. A pass with no exchanges proves the array is sorted, permitting early termination. Already sorted input then takes Θ(n) time, while reverse order still takes Θ(n²). All three versions use Θ(1) auxiliary space. The exact original pseudocode remains on the source slides.
Trace [5, 3, 8, 1]: the first pass gives [3, 5, 1, 8].
The next pass gives [3, 1, 5, 8], and the third gives [1, 3, 5, 8].
A strict greater-than comparison preserves the relative order of equal keys, making the adjacent-swap version stable.
A smaller leading coefficient is a practical improvement even when the asymptotic class stays the same.
(n−1)2∈Θ(n2),i=1∑n−1i=2n(n−1)∈Θ(n2)
The source’s merge and partition traces
The merge-sort slides show eight elements passing through three merge levels: 8 × 3 = 24 append operations in that illustrative merge-only cost model. Since log₂ 8 = 3, this makes the n log₂ n pattern visible. Comparisons, allocation and copying can add work without changing the growth class.
The quicksort sequence begins with [5, 2, 7, 6, 1, 9, 4, 8] and illustrates partitioning around 5. Subsequent slides show an extreme partition pattern with 7 + 6 + 5 + 4 + 3 + 2 + 1 = 28 operations for eight items. The general triangular sum explains the quadratic worst case.
The source’s recursive QuickSort(Data, Left, Right) stops when Left is no longer less than Right, and recurses on intervals ending before and starting after PivotPosition. Excluding the pivot is what ensures progress. The preserved slide walkthrough and the Java Quick implementation use their own partition details; do not assume their exact swap sequences are identical.
Remember for revision
Define the function and the input model before writing a bound. O is an upper bound, Ω a lower bound and Θ a tight bound. Show the loop count, sum or recurrence that justifies the result.
Original implementations
Read the complete original code and its documentation. Core algorithm pages add a walkthrough, complexity discussion and a worked example.
Supplementary practice. Try each question before opening the answer.
Prove 4n² + 3n + 7 is Θ(n²).
For n ≥ 1, 4n² ≤ 4n² + 3n + 7 ≤ 14n². Constants c₁ = 4, c₂ = 14 and n₀ = 1 establish the claim.
What is the cost of repeatedly doubling i from 1 while i < n?
After k iterations i = 2ᵏ, so k is about log₂ n. With constant work per iteration, the time is Θ(log n).
Original course material
Complete lecture source
Every source page is preserved below. Open a page to read its text and inspect the original diagram, formula or example. Expanded explanations above are supplementary.
Page 01 · Analysis of AlgorthimsOriginal page 1 · Open the image for full detail.
Analysis of Algorthims
Big O Notation
Searchable transcription (OCR; verify formulas against the image)
Analysis of Algorthims
Big O Notation
Page 02 · Visual explanation / original slideOriginal page 2 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Big O
Big O describes how the time taken, or memory used, by a
program scales with the amount of data it has to work on
Big O describes the 'complexity' of a program
Common sense tells us that a program takes longer when
there is more data to work on.. But not necessarily
95
19
58
12
53
17
63
Target
63
Page 03 · Visual explanation / original slideOriginal page 3 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Big O Complexities
Linear search
Stack
Bubble sort
Binary search
Merge sort
Page 04 · Visual explanation / original slideOriginal page 4 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Linear Search
Sometimes referred to as a sequential search
An unordered list is searched for a particular value
Each value in the list is compared with the target value
Linear search implemented with a simple loop
Page 05 · Visual explanation / original slideOriginal page 5 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 06 · Visual explanation / original slideOriginal page 6 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Target
63
95
19
63
Target
95
19
63
Page 07 · Visual explanation / original slideOriginal page 7 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 08 · Visual explanation / original slideOriginal page 8 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
LinearSearch
5000
Data
Time
4500
10
20
4000
40
3500
80
3000
Time
16
160
2500
32
320
2000
64
640
1500
128
1280
1000
256
2560
500
512
5120
50
100
150
200
250
300
350
400
450
500
Amount of Data
Page 09 · Visual explanation / original slideOriginal page 9 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Linear Search Complexity
For n data items, the time taken is equal to some
constant multiplied by n
The Big O time complexity is Linear
o(n)
Page 10 · Visual explanation / original slideOriginal page 10 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Linear Time Complexity
5000
Data
Time
4500
10
4000
20
40
3500
0(n)
80
3000
Time
16
160
2500
32
320
2000
64
640
1500
128
1280
1000
256
2560
500
512
5120
50
100
150
200
250
300
350
400
450
500
AmountofData
Page 11 · Visual explanation / original slideOriginal page 11 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Stack
Items are pushed onto and popped off the top of a stack
Peek examines top item without removing it
Last in first out data structure (LIFO)
Implemented with an array and a pointer to the top item
Page 12 · Visual explanation / original slideOriginal page 12 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
ProcedurePush
IFTop =MaximumSize THEN
OUTPUT"Stack overflow"
ELSE
Top = Top + 1
ArrayStack(Top)=new item
ENDIF
ENDProcedure
Page 13 · Visual explanation / original slideOriginal page 13 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
ProcedurePush
ProcedurePop
IFTop=MaximumSizeTHEN
IFTop=OTHEN
OUTPUT"Stackoverflow"
OUTPUT"Stack is empty"
ELSE
ELSE
Top = Top + 1
copy item=ArrayStack(Top)
ArrayStack(Top)=newitem
Top = Top - 1
ENDIF
ENDIF
ENDProcedure
ENDProcedure
Page 14 · Visual explanation / original slideOriginal page 14 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
MaximumSize=5
Sally
Top = 2
Kevin
Page 15 · Visual explanation / original slideOriginal page 15 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Push
Beatrix
MaximumSize=5
Sally
Top = 2
Kevin
Page 16 · Visual explanation / original slideOriginal page 16 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Push
MaximumSize=5
Beatrix
Top = 3
Sally
Kevin
Page 17 · Visual explanation / original slideOriginal page 17 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Pop
MaximumSize=5
Beatrix
Top = 3
Sally
Kevin
Page 18 · Visual explanation / original slideOriginal page 18 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Pop
Beatrix
MaximumSize=5
Beatrix
Sally
Top = 2
Kevin
Page 19 · Visual explanation / original slideOriginal page 19 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Marylin
Marylin
David
David
Beatrix
Beatrix
Sally
Sally
Kevin
Kevin
Page 20 · Visual explanation / original slideOriginal page 20 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
John
John
Chloe
Chloe
Agnes
Agnes
Albert
Albert
Marylin
Marylin
Marylin
David
David
David
Beatrix
Beatrix
Beatrix
Sally
Sally
Sally
Kevin
Kevin
Kevin
Page 21 · Visual explanation / original slideOriginal page 21 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Marylin
John
Chloe
Agnes
Albert
Marylin
David
David
Beatrix
Beatrix
Sally
Sally
Kevin
Kevin
Page 22 · Visual explanation / original slideOriginal page 22 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
John
Chloe
Agnes
Albert
Marylin
David
David
Beatrix
Beatrix
Sally
Sally
Kevin
Kevin
Page 23 · Visual explanation / original slideOriginal page 23 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
StackPushor Pop
5000
Data
Time
4500
500
4000
500
3500
500
500
3000
Time
16
500
2500
32
500
2000
500
64
1500
128
500
1000
256
500
500
512
500
50
100
150
200
250
300
350
400
450
500
Amount of Data
Page 24 · Visual explanation / original slideOriginal page 24 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Stack Operations Complexity
Increasing the amount of data makes no difference
to the time taken by push or pop
The Big O time complexity is Constant
0(1)
Page 25 · Visual explanation / original slideOriginal page 25 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 26 · Visual explanation / original slideOriginal page 26 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
The Dominant Term
An algorithm working on a data structure of size n
might take 5n3 + n? + 4n + 3 steps
Page 27 · Visual explanation / original slideOriginal page 27 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
The Dominant Term
An algorithm working on a data structure of size n
might take 5n3 + n? + 4n + 3 steps
The larger n becomes, the less significant the smaller
terms become, so we ignore everything except 5n3
Page 28 · Visual explanation / original slideOriginal page 28 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
thank you sir
The Dominant Term
An algorithm working on a data structure of size n
might take 5n3 + n? + 4n + 3 steps
The larger n becomes, the less significant the smaller
terms become, so we ignore everything except 5n3
We can also ignore any constants, so the Big O time
complexity of this algorithm is O(n3)
Page 29 · Visual explanation / original slideOriginal page 29 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Bubble Sort
Page 30 · Visual explanation / original slideOriginal page 30 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Bubble Sort
Sorts a list of items into numeric or alphabetical order
Scans a list comparing pairs of values and swapping
their positions if necessary
For n data items, the list is scanned like this n-1 times
Various enhancements possible
Page 31 · Visual explanation / original slideOriginal page 31 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Pseudocode
FOR iPass = 1 to n - 1
FORi=O TO n - 2
IF ArrayToSort(i) > ArrayToSort(i + 1) THEN
Swap ArrayToSort(i) with ArrayToSort(i + 1)
ENDIF
NEXTi
NEXTiPass
Page 32 · Visual explanation / original slideOriginal page 32 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
10
10
10
10
10
Page 33 · Visual explanation / original slideOriginal page 33 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
10
10
10
10
10
Page 34 · Visual explanation / original slideOriginal page 34 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
10
10
10
10
Page 35 · Visual explanation / original slideOriginal page 35 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
10
10
10
10
Page 36 · Visual explanation / original slideOriginal page 36 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Bubble Sort
5000
Data
Time
4500
4000
3500
16
3000
Time
16
256
2500
32
1024
2000
64
4096
1500
128
16384
1000
256
65536
500
512
262144
50
200
250
300
100
150
350
400
450
500
Amountof Data
Page 37 · Visual explanation / original slideOriginal page 37 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Bubble Sort Complexity
For n data items, a simple implementation performs
(n - 1) * (n - 1) operations
This can be written n2 - 2n + 1, and the dominant
term is n2
The Big O time complexity is Quadratic
Page 38 · Visual explanation / original slideOriginal page 38 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Bubble Sort Complexity
For n data items, a simple implementation performs
(n - 1) * (n - 1) operations
This can be written n2 - 2n + 1, and the dominant
term is n2
The Big O time complexity is Quadratic
O(n2)
Page 39 · Visual explanation / original slideOriginal page 39 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 40 · Visual explanation / original slideOriginal page 40 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Enhanced Bubble Sort
10
Largest item is in the correct position after the first pass
Second largest item is in the correct place after the next pass
and so on...
The inner loop can run one less time with each pass of the
outer loop
Page 41 · Visual explanation / original slideOriginal page 41 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 42 · Visual explanation / original slideOriginal page 42 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Enhanced Algorithm Complexity
For n items of data, the enhanced bubble sort algorithm
performs (n - 1) + (n - 2) + (n - 3) + ... + 3 + 2 + 1 operations
This can be shown to be (n2 - n)/2
This is a 50% reduction in the time taken, but..
The dominant term is still n?
The complexity is still Quadratic
O(n2)
Page 43 · Visual explanation / original slideOriginal page 43 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Alternative Enhanced Bubble Sort
10
If the inner loop performs no swaps, the data must now be in
the correct order
Check for swaps with a Boolean variable
Force an early exit when there's no more work to do
Page 44 · Visual explanation / original slideOriginal page 44 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Pseudocode
REPEAT
Swapped = False
FOR i = O TO Length(ArrayToSort) - 2
IF ArrayToSort(i) > ArrayToSort(i + 1) THEN
Swap ArrayToSort(i) with ArrayToSort(i + 1)
Swapped = True
ENDIF
NEXTi
UNTILSwapped=False
Page 45 · Visual explanation / original slideOriginal page 45 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Best versus
Worst Case Scenario
Best case scenario
- Data is already sorted, the inner loop will run only once
Linear
0(n)
Worst case scenario
- Data is in reverse order, every item has to be moved
Quadratic
0(n2)
Page 46 · Visual explanation / original slideOriginal page 46 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Logarithms
The inverse of exponentiation
23 = 8
log28 = 3
104 = 10000
log1o10000 = 4
Generally...
x=
logxy = Z
Page 47 · Visual explanation / original slideOriginal page 47 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 48 · Visual explanation / original slideOriginal page 48 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 49 · Visual explanation / original slideOriginal page 49 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Binary Search
Page 50 · Visual explanation / original slideOriginal page 50 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Binary Search
Used to search an ordered list for a particular value
Divide and conquer approach
Target compared with middle value, then half of the list is
discarded, repeatedly, until the target is found
Very efficient for large sorted lists
Page 51 · Visual explanation / original slideOriginal page 51 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
iLow=LBound(DataArray)
iHigh = UBound(DataArray)
DoWhileiLow<=iHigh
iMiddle=(iLow+iHigh) / 2
If Target = DataArray(iMiddle) Then
bFound = True
Exit Do
ElselfTarget<DataArray(iMiddle)Then
iHigh = (iMiddle - 1)
Else
iLow=(iMiddle+1)
End If
Loop
Page 52 · Visual explanation / original slideOriginal page 52 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Target
63
27
31
63
85
93
24
41
53
58
62
95
Page 53 · Visual explanation / original slideOriginal page 53 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Target
63
24
27
53
58
62
63
31
41
85
93
95
Page 54 · Visual explanation / original slideOriginal page 54 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Target
63
62
63
85
93
95
24
27
41
53
31
58
Page 55 · Visual explanation / original slideOriginal page 55 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 56 · We choped the list 3 times. SometimesOriginal page 56 · Open the image for full detail.
We choped the list 3 times. Sometimes
also known as Binary Cop
Searchable transcription (OCR; verify formulas against the image)
Target
63
62
63
62
63
63
62
We choped the list 3 times. Sometimes
also known as Binary Cop
63
Page 57 · Visual explanation / original slideOriginal page 57 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 58 · Visual explanation / original slideOriginal page 58 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 59 · Visual explanation / original slideOriginal page 59 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Target
63
63
85
93
95
62
63
85
93
95
62
63
Page 60 · In 4 chops – we reach at the target.Original page 60 · Open the image for full detail.
In 4 chops – we reach at the target.
Searchable transcription (OCR; verify formulas against the image)
Target
63
63
62
In 4 chops - we reach at the target.
63
Page 61 · Visual explanation / original slideOriginal page 61 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
BinarySearch
10
Data
Time
Time
16
32
64
128
256
512
50
100
150
200
250
300
350
400
450
500
Amountof Data
Page 62 · Visual explanation / original slideOriginal page 62 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Binary Search Complexity
Double the data requires only one extra chop
This makes the binary search very efficient for very
large data sets, if the data is already sorted
The Big O time complexity is Logarithmic
o(log n)
Page 63 · Visual explanation / original slideOriginal page 63 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Logarithmic TimeComplexity
10
Data
Time
o(log n)
Time
16
32
64
128
256
512
50
100
150
200
250
300
350
400
450
500
AmountofData
Page 64 · Visual explanation / original slideOriginal page 64 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Merge Sort
Page 65 · Visual explanation / original slideOriginal page 65 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Merge Sort
Sorts the data in a list
Divide and conquer approach
Splits a list into several sub lists each of which contains
only one item, and is therefore by definition sorted
Pairs of sub lists merged together, sorting as it goes
Page 66 · Visual explanation / original slideOriginal page 66 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 67 · Visual explanation / original slideOriginal page 67 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 68 · Visual explanation / original slideOriginal page 68 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 69 · Visual explanation / original slideOriginal page 69 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 70 · Visual explanation / original slideOriginal page 70 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 71 · Visual explanation / original slideOriginal page 71 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 72 · Visual explanation / original slideOriginal page 72 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 73 · Visual explanation / original slideOriginal page 73 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Merge Sort Complexity
If n = 8, merge sort does n * 3 append operations
Page 74 · Visual explanation / original slideOriginal page 74 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Merge Sort Complexity
If n = 8, merge sort does n * 3 append operations
log2n = 3, so this is n * logzn append operations
Page 75 · Visual explanation / original slideOriginal page 75 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Merge Sort Complexity
If n = 8, merge sort does n * 3 append operations
log2n = 3, so this is n * log2n append operations
The Big O time complexity is 'Linearithmic'
Page 76 · Visual explanation / original slideOriginal page 76 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Merge Sort Complexity
If n = 8, merge sort does n * 3 append operations
log2n = 3, so this is n * log2n append operations
The Big O time complexity is ‘Linearithmic'
O(n log n)
Page 77 · Visual explanation / original slideOriginal page 77 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Merge Sort
5000
Data
Time
4500
4000
3500
24
3000
Time
16
64
2500
32
160
2000
64
384
1500
128
896
1000
256
2048
500
512
4608
50
100
150
200
250
300
350
400
450
500
Amount of Data
Page 78 · Visual explanation / original slideOriginal page 78 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 79 · Visual explanation / original slideOriginal page 79 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Big O
Big O describes how the time taken, or memory used, by a
program scales with the amount of data it has to work on
Page 80 · Visual explanation / original slideOriginal page 80 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Constant O(1)
Timetakenisindependentoftheamountofdata
Stackpush,popandpeek;Queueenqueueanddequeue;Insertanodeintoa linkedlist
Linear O(n)
Timetakenisdirectlyproportionaltotheamountofdata
Linear search; Count items in a list; Compare apair of strings
Quadratic O(n2)
Timetakenisproportionaltotheamountofdatasquared
Bubble sort; Selection sort; Insertion sort,Traverse a 2Darray
Polynomial o(nk)
Timetaken isproportionaltotheamountof dataraisedtothepowerofaconstant
Logarithmic o(log n)
Timetakenisproportionaltothelogarithmof theamountofdata
Binary search a sorted list; Searcha binarytree
Linearithmic O(n log n)
Timetakenisproportionaltothelogarithmoftheamountofdata,multipliedbytheamountofdata
Merge sort;Quicksort
Exponential o(k))
Timetakenisproportionaltoa constantraisedtothepoweroftheamount of data
n-Queensproblem;Travellingsalesman
Page 81 · Big O complexitiy is not about the real perforence of the program.Original page 81 · Open the image for full detail.
Big O complexitiy is not about the real perforence of the program.
Its about how well a program scale
Or How well a program mainatian its perforemcne when given more data to work with.
Page 82 · Visual explanation / original slideOriginal page 82 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Big O
Space Complexity
Page 83 · Time complexity of a program i.e., impact of more input data on the timeOriginal page 83 · Open the image for full detail.
Time complexity of a program i.e., impact of more input data on the time
it take to complete.
Space Complexity of a program i.e., How the memory requirments of program
differ with amount of the data?
Page 84 · Visual explanation / original slideOriginal page 84 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Linear Search
FORi=0TOn-1
Target
63
IFArray(i)=Target THEN
bFound=True
95
58
12
17
19
53
63
EXITFOR
ENDIF
NEXTi
Page 85 · Visual explanation / original slideOriginal page 85 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Linear Search
FORi=0TOn-1
Target
63
IF Array(i) = Target THEN
bFound=True
95
19
58
12
53
17
EXITFOR
ENDIF
NEXTi
Linear time complexity o(n)
Page 86 · Other Examples: Inseration Sort, Bubble Sort, Section SortOriginal page 86 · Open the image for full detail.
Other Examples: Inseration Sort, Bubble Sort, Section Sort
Searchable transcription (OCR; verify formulas against the image)
Linear Search
FORi=0TOn-1
Target
63
IF Array(i) = Target THEN
bFound=True
95
19
58
12
53
17
63
EXITFOR
END IF
NEXTi
Other Examples: Inseration Sort, Bubble Sort, Section Sort
Linear time complexity o(n)
Constant space complexity o(1)
Page 87 · Imp: Partionoing ProcessOriginal page 87 · Open the image for full detail.
Imp: Partionoing Process
Searchable transcription (OCR; verify formulas against the image)
Quicksort
Imp: Partionoing Process
Page 88 · Visual explanation / original slideOriginal page 88 · Open the image for full detail.Page 89 · Visual explanation / original slideOriginal page 89 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Quicksort
Repeatforeachpartitionwith morethanone item
Partitionthe list
Untilallpartitionscontainonlyoneitem
Page 90 · Visual explanation / original slideOriginal page 90 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 91 · Visual explanation / original slideOriginal page 91 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Quicksort
Repeat for each partition with morethan oneitem
Partitionthe list
Until all partitions contain only one item
Page 92 · Visual explanation / original slideOriginal page 92 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Quicksort
Repeat for eachpartitionwithmorethanone item
Partitionthelist
Untilallpartitionscontainonlyoneitem
Page 93 · Visual explanation / original slideOriginal page 93 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 94 · Visual explanation / original slideOriginal page 94 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 95 · Visual explanation / original slideOriginal page 95 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Quicksort
Repeat foreachpartitionwithmorethanone item
Partitionthe list
Until all partitionscontainonlyoneitem
Page 96 · Visual explanation / original slideOriginal page 96 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Quicksort
Repeat for each partition with more than one item
Partition the list
Until all partitions containonlyoneitem
Page 97 · Visual explanation / original slideOriginal page 97 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Quicksort
Repeat for each partition with morethan one item
Partitionthelist
Untilallpartitionscontainonlyoneitem
Page 98 · Visual explanation / original slideOriginal page 98 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 99 · 7+6+5+4+3+2+1 = 28Original page 99 · Open the image for full detail.
7+6+5+4+3+2+1 = 28
Searchable transcription (OCR; verify formulas against the image)
Quicksort
1234
Repeat for each partition with more than one item
Partitionthelist
Until all partitionscontainonlyoneitem
7+6+5+4+3+2+1=28
Page 100 · Visual explanation / original slideOriginal page 100 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 101 · Visual explanation / original slideOriginal page 101 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Page 102 · Visual explanation / original slideOriginal page 102 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Quicksort
Repeat for each partition with more than one item
Partitionthelist
Untilall partitionscontainonlyoneitem
(n2 - n) / 2
Page 103 · Visual explanation / original slideOriginal page 103 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Quicksort
Repeat for each partition with more than one item
Partitionthelist
Until all partitions containonly one item
Quadratic time complexity o(n2)
Page 104 · Visual explanation / original slideOriginal page 104 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Quicksort
Sub QuickSort(Data(),Left,Right)
IfLeft<RightThen
PivotPosition = Partition(Data, Left,Right)
Quicksort(Data,Left,PivotPosition-1)
Quicksort(Data,PivotPosition+l,Right)
End If
End Sub
Quadratic time complexity
0(n2)
Page 105 · Visual explanation / original slideOriginal page 105 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Quicksort
SubQuickSort(Data(), Left,Right)
If Left < Right Then
PivotPosition = Partition(Data, Left, Right)
QuickSort(Data,Left,PivotPosition-1)
Quicksort(Data,PivotPosition + l,Right)
End If
Left=0Right=7PivotPosition=0
EndSub
Quadratic time complexity
0(n2)
Page 106 · Visual explanation / original slideOriginal page 106 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Quicksort
123456
Left=6
Right =5
SubQuickSort(Data(),Left, Right)
Left=6
Right=7
PivotPosition=6
Right =7
Left =5
PivotPosition=5
IfLeft<Right Then
Left = 4
Right =7
PivotPosition=Partition(Data,Left,Right)
PivotPosition=4
Quicksort(Data,Left,PivotPosition -1)
Left = 3
Right =7
PivotPosition=3
Quicksort(Data,PivotPosition+l,Right)
Left =2
Right =7
PivotPosition=2
End If
Left=1
Right = 7
PivotPosition=1
Left = 0
Right =7
End Sub
PivotPosition=O
Quadratic time complexity o(n2)
Page 107 · Visual explanation / original slideOriginal page 107 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Quicksort
1234567
Left=7 Right=7
Sub
QuickSort(Data(),Left,Right)
Left =6
Right =7
PivotPosition=6
Left = 5
Right =7
PivotPosition=5
If Left<Right Then
Left = 4
Right = 7
PivotPosition= Partition(Data, Left,Right)
PivotPosition=4
QuickSort(Data,Left,PivotPosition-1)
Left = 3
Right = 7
PivotPosition=3
Quicksort(Data,PivotPosition+l,Right)
Left = 2
Right =7
PivotPosition=2
End If
Left = 1
Right =7
PivotPosition=1
Left =0
Right =7
EndSub
PivotPosition=O
Quadratictimecomplexity
0(n2)
Page 108 · Visual explanation / original slideOriginal page 108 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Quicksort
1234
SubQuickSort(Data(),Left,Right)
IfLeft<RightThen
PivotPosition= Partition(Data,Left,Right)
Quicksort(Data,Left,PivotPosition-1)
Left=3Right=7PivotPosition=3
Quicksort(Data, PivotPosition +l,Right)
Left=2Right=7PivotPosition=2
End If
Left=1
Right=7
PivotPosition=1
Left =0
Right = 7
End Sub
PivotPosition=O
Quadratic time complexity o(n2)
Page 109 · Visual explanation / original slideOriginal page 109 · Open the image for full detail.Searchable transcription (OCR; verify formulas against the image)
Quicksort
Sub QuickSort(Data(),Left,Right)
IfLeft<RightThen
PivotPosition = Partition(Data,Left,Right)
QuickSort(Data,Left,PivotPosition-1)
Quicksort(Data,PivotPosition+l,Right)
End If
Left=0 Right=7PivotPosition=0
End Sub
Quadratic time complexity o(n2)
Linear space complexity o(n)