0% found this document useful (0 votes)
14 views

Lecture 3

Uploaded by

hsn.balorss
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lecture 3

Uploaded by

hsn.balorss
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 190

learn how to think algorithmically

n
n n/2
n n/2

log2 n
1. Stand up and think of the number 1.
1. Stand up and think of the number 1.
2. Pair off with someone standing, add their number to yours, and remember the sum.
1. Stand up and think of the number 1.
2. Pair off with someone standing, add their number to yours, and remember the sum.
3. One of you should then sit down.
1. Stand up and think of the number 1.
2. Pair off with someone standing, add their number to yours, and remember the sum.
3. One of you should then sit down.
4. If still standing, go back to step 2.
1 5 10 20 50 100 500
[0] [1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5] [6]
searching
input → → output
→ → output
→ → bool
algorithm
linear search
For each door from left to right
If 50 is behind door
Return true
Return false
For each door from left to right
If 50 is behind door
Return true
Else
Return false
For each door from left to right
If 50 is behind door
Return true
Return false
For i from 0 to n-1
If 50 is behind doors[i]
Return true
Return false
binary search
If 50 is behind middle door
Return true
Else if 50 < middle door
Search left half
Else if 50 > middle door
Search right half
If no doors left

If 50 is behind middle door


Return true
Else if 50 < middle door
Search left half
Else if 50 > middle door
Search right half
If no doors left
Return false
If 50 is behind middle door
Return true
Else if 50 < middle door
Search left half
Else if 50 > middle door
Search right half
If no doors left
Return false
If 50 is behind doors[middle]
Return true
Else if 50 < doors[middle]
Search doors[0] through doors[middle - 1]
Else if 50 > doors[middle]
Search doors[middle + 1] through doors[n - 1]
running time
time to solve

size of problem
O(n) O(n/2)

time to solve

O(log2n)

size of problem
O(n) O(n/2)

time to solve

O(log2n)

size of problem
O(n) O(n)

time to solve

O(log2n)

size of problem
O(n) O(n)

time to solve

O(log n)

size of problem
O(n)

time to solve

O(log n)

size of problem
O
O(n2)

O(n log n)

O(n)

O(log n)

O(1)
O(n2)

O(n log n)

O(n) linear search

O(log n)

O(1)
O(n2)

O(n log n)

O(n) linear search

O(log n) binary search

O(1)
Ω
Ω(n2)

Ω(n log n)

Ω(n)

Ω(log n)

Ω(1)
Ω(n2)

Ω(n log n)

Ω(n)

Ω(log n)

Ω(1) linear search


Ω(n2)

Ω(n log n)

Ω(n)

Ω(log n)

Ω(1) linear search, binary search


Θ
Θ(n2)

Θ(n log n)

Θ(n)

Θ(log n)

Θ(1)
linear search
string.h
manual.cs50.io/#string.h
strcmp
data structures
person people[]
string name;
string number;
typedef struct
{
string name;
string number;
}
person;
typedef struct
{
string name;
string number;
} person;
sorting
input → → output
unsorted → → output
unsorted → → sorted
7 2 5 4 1 6 0 3 → → sorted
7 2 5 4 1 6 0 3 → → 0 1 2 3 4 5 6 7
7 2 5 4 1 6 0 3 → → 0 1 2 3 4 5 6 7
7 2 5 4 1 6 0 3
selection sort
7 2 5 4 1 6 0 3
For i from 0 to n-1
Find smallest number between numbers[i] and numbers[n-1]
Swap smallest number with numbers[i]
[0] [1] [2] ... [n-3] [n-2] [n-1]
(n – 1)
(n – 1) + (n – 2)
(n – 1) + (n – 2) + (n – 3)
(n – 1) + (n – 2) + (n – 3) + ... + 1
(n – 1) + (n – 2) + (n – 3) + ... + 1

n(n – 1)/2
(n – 1) + (n – 2) + (n – 3) + ... + 1

n(n – 1)/2

(n2 – n)/2
(n – 1) + (n – 2) + (n – 3) + ... + 1

n(n – 1)/2

(n2 – n)/2

n2/2 – n/2
(n – 1) + (n – 2) + (n – 3) + ... + 1

n(n – 1)/2

(n2 – n)/2

n2/2 – n/2

O(n2)
O(n2)

O(n log n)

O(n)

O(log n)

O(1)
O(n2) selection sort

O(n log n)

O(n)

O(log n)

O(1)
For i from 0 to n-1
Find smallest number between numbers[i] and numbers[n-1]
Swap smallest number with numbers[i]
[0] [1] [2] ... [n-3] [n-2] [n-1]
Ω(n2)

Ω(n log n)

Ω(n)

Ω(log n)

Ω(1)
Ω(n2) selection sort

Ω(n log n)

Ω(n)

Ω(log n)

Ω(1)
Θ(n2)

Θ(n log n)

Θ(n)

Θ(log n)

Θ(1)
Θ(n2) selection sort

Θ(n log n)

Θ(n)

Θ(log n)

Θ(1)
bubble sort
7 2 5 4 1 6 0 3
Repeat n times
For i from 0 to n-2
If numbers[i] and numbers[i+1] out of order
Swap them
Repeat n-1 times
For i from 0 to n-2
If numbers[i] and numbers[i+1] out of order
Swap them
[0] [1] [2] ... [n-3] [n-2] [n-1]
(n – 1) × (n – 1)
(n – 1) × (n – 1)

n2 – 1n – 1n + 1
(n – 1) × (n – 1)

n2 – 1n – 1n + 1

n2 – 2n + 1
(n – 1) × (n – 1)

n2 – 1n – 1n + 1

n2 – 2n + 1

O(n2)
O(n2)

O(n log n)

O(n)

O(log n)

O(1)
O(n2) bubble sort

O(n log n)

O(n)

O(log n)

O(1)
Repeat n-1 times
For i from 0 to n-2
If numbers[i] and numbers[i+1] out of order
Swap them
Repeat n-1 times
For i from 0 to n-2
If numbers[i] and numbers[i+1] out of order
Swap them
If no swaps
Quit
Ω(n2)

Ω(n log n)

Ω(n)

Ω(log n)

Ω(1)
Ω(n2)

Ω(n log n)

Ω(n) bubble sort

Ω(log n)

Ω(1)
recursion
If no doors left
Return false
If number behind middle door
Return true
Else if number < middle door
Search left half
Else if number > middle door
Search right half
If no doors left
Return false
If number behind middle door
Return true
Else if number < middle door
Search left half
Else if number > middle door
Search right half
1 Pick up phone book
2 Open to middle of phone book
3 Look at page
4 If person is on page
5 Call person
6 Else if person is earlier in book
7 Open to middle of left half of book
8 Go back to line 3
9 Else if person is later in book
10 Open to middle of right half of book
11 Go back to line 3
12 Else
13 Quit
1 Pick up phone book
2 Open to middle of phone book
3 Look at page
4 If person is on page
5 Call person
6 Else if person is earlier in book
7 Open to middle of left half of book
8 Go back to line 3
9 Else if person is later in book
10 Open to middle of right half of book
11 Go back to line 3
12 Else
13 Quit
1 Pick up phone book
2 Open to middle of phone book
3 Look at page
4 If person is on page
5 Call person
6 Else if person is earlier in book
7 Open to middle of left half of book
8 Go back to line 3
9 Else if person is later in book
10 Open to middle of right half of book
11 Go back to line 3
12 Else
13 Quit
1 Pick up phone book
2 Open to middle of phone book
3 Look at page
4 If person is on page
5 Call person
6 Else if person is earlier in book
7 Search left half of book
8
9 Else if person is later in book
10 Search right half of book
11
12 Else
13 Quit
1 Pick up phone book
2 Open to middle of phone book
3 Look at page
4 If person is on page
5 Call person
6 Else if person is earlier in book
7 Search left half of book
8 Else if person is later in book
9 Search right half of book
10 Else
11 Quit
google.com/search?q=recursion
merge sort
Sort left half of numbers
Sort right half of numbers
Merge sorted halves
If only one number
Quit
Else
Sort left half of numbers
Sort right half of numbers
Merge sorted halves
If only one number
Quit
Else
Sort left half of numbers
Sort right half of numbers
Merge sorted halves
1 3 4 6 0 2 5 7
If only one number
Quit
Else
Sort left half of numbers
Sort right half of numbers
Merge sorted halves
6 3 4 1 5 2 7 0
63415270
63415270
5270
6341
5270
41

63
5270
41

6
5270
41

6 3
5270
41

6
5270
41

36
5270

36 41
5270

36 1

4
5270

36

4 1
5270

36 1

4
5270

36 14
5270
1

36 4
5270
13

6 4
5270
134

6
5270
1346
1346 5270
1346 70

52
1346 70

5
1346 70

5 2
1346 70

5
1346 70

25
1346

25 70
1346

25 0

7
1346

25

7 0
1346

25 0

7
1346

25 07
1346 0

25 7
1346 02

5 7
1346 025

7
1346 0257
0
1346 257
01
346 257
012
346 57
0123
46 57
01234
6 57
012345
6 7
0123456
7
01234567
01234567
O(n2)

O(n log n)

O(n)

O(log n)

O(1)
63415270
6341 5270

63 41 52 70

6 3 4 1 5 2 7 0
63415270
6341 5270

63 41 52 70

6 3 4 1 5 2 7 0
log2 n
log2 8
log2 23
3
63415270
6341 5270

63 41 52 70

6 3 4 1 5 2 7 0
n log2 n
n log n
O(n2)

O(n log n) merge sort

O(n)

O(log n)

O(1)
Ω(n2)

Ω(n log n) merge sort

Ω(n)

Ω(log n)

Ω(1)
Θ(n2)

Θ(n log n) merge sort

Θ(n)

Θ(log n)

Θ(1)
63415270
6341 5270

63 41 52 70

6 3 4 1 5 2 7 0

You might also like