From 9108cea7954093975074ccec4796fddad0554b79 Mon Sep 17 00:00:00 2001 From: Louie S Date: Wed, 8 Apr 2020 11:39:07 -0700 Subject: Post-class 04/08 --- 04-08.md | 182 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 04-08.md (limited to '04-08.md') diff --git a/04-08.md b/04-08.md new file mode 100644 index 0000000..e4c4111 --- /dev/null +++ b/04-08.md @@ -0,0 +1,182 @@ +## Outline +- Concept + - Data Structure + - Code Efficiency +- Big-O + +## What is Data Structure? + +- How data is organized in memory (or on disk) + +|Data|Structure| +|----|---------| +|Atomic or composite data|A set of rules that holds the data together| + +- Examples: + - Linked List + - Array + +## Why do we care about Data Structure? + +- Efficiency can refer to either + - Space (importance depending on architecture) + - or Time +- We will focus on **time**, So, we want our operations efficient in terms of time + +## How to measure efficiency? + +- Wall-clock time? + - Not so great + - Not that accurate + - can't compare across computer or programmers + - dependent on circumstances and what's running + +- Count the number of operations! + +### What kind of Operations? + +- insert (add) +- delete (remove) +- access/retrieval +- min/max +- union/merge (less common) + +## A Simple Example + +- We assume each basic operation takes one step +- Example: Initialize an array (a loop setting `a[i]=0`) +- Count the steps ... how many times is ... + +Using a for loop: +``` +for(i = 0; i `5n^2` -> `n^2` + - Would be represented as O(n^2) + +- Example: `600 + 30n` -> `30n` -> `n` + - Would be represented as O(n) + +- Example: `15n + 12nlog(n) + 3` -> `12nlog(n)` -> `nlog(n)` + - Would be represented as O(nlog(n)) + +- Example: `14n^2 + 0.05n^3 + 18000000` -> `0.05n^3` -> `n^3` + - Would be represented as O(n^3) + +## Apply Big-O on Code Analysis + +- Let's analyze some loops and compute the big-O runtimes + - `for(i=0; i `n` -> O(n) + - `for(i=n; i>0; i--) x++;` + - O(n) + - `for(i=0; i<(n/2); i++) x++;` + - cut the threshold by half + - O(n/2) is **not** the finalized runtime + - O(n) is the finalized runtime + - `for(i=0; i O(n) + +- `for(i = 1; i < n; i=i*2) x++` + - Break it down: + - iteration 1: i=1 + - iteration 2: i=2 + - iteration 3: i=4 + - iteration 4: i=8 + - iteration 5: i=16 + - and so on, until i O(log2(n)) -> O(log(n)) + - log base value doesn't matter + +- Example: `for(i=1; i O(log(n)) +- Example: `for(i=1; i x = ((n-1)\*n)/2 +- O(x) = O(((n-1)\*n)/2) -> O(n^2) -- cgit