Uploaded by Mustafa Cihan Önal

Furkan Karaca 150721065 Discrete Mathematics Homework 1

advertisement
Furkan Karaca 150721065
1)Describe an algorithm that finds the location of the letter “F” in Turkish
alphetics which are listed in alphetical order.
Solution :
1. *Initialize* an ordered list of Turkish letters. You can use the standard Turkish
alphabet, which consists of 29 letters, including "F".
2. *Iterate* through the list of letters.
3. *Check* each letter to see if it matches the letter "F".
4. If the letter is "F", *record* its position (index) in the list.
5. *Continue* iterating through the remaining letters.
6. Once the iteration is complete, *return* the recorded position of "F".
----------------------------------------------------------------------------------------------------------------Pseudocode with Binary Search
procedure binary search (x:f ; a,b,c,ç,d,e,f,g,ğ,h,ı,i,j,k,l,m,n,o,ö,p,r,s,ş,t,u,ü,v,y,z)
i := 1
j := 29
while i < j
m := ⌊(i + j)∕2⌋
if x > am then i := m + 1
else j := m
if x = ai then location := i
else location := 0
return location
2) List all the steps used to search for 9 in the sequence 1, 3, 4, 5, 6, 8, 9, 11
using
a) a linear search b) a binary search
a)
procedure linear search(x: 9; 1,3,4,5,6,8,9,11: distinct integers)
i := 1
while (i ≤ 8 and x ≠ ai)
i := i + 1
if i ≤ n then location := i
else location := 0
return location
b)
procedure binary search (x: 9; 1,3,4,5,6,8,9,11: increasing integers)
i := 1 {i is left endpoint of search interval}
j := 8 {j is right endpoint of search interval}
while i < j
m := ⌊(i + j)∕2⌋
if x > am then i := m + 1
else j := m
if x = ai then location := i
else location := 0
return location
3) Determine whether each of these functions is O(x^2).
a) f(x) = 17x + 11
b) f(x) = x^2 + 1000
c) f(x) = x log x
d) f(x) = (x^4)∕2
e) f(x) = 2^x
f ) f(x) = ⌊x⌋ ⋅ ⌈x⌉
a)
f(x) = 17x+11
|f(x)| =< c|x^2|
17x+11 =< 17x^2 + 11x^2
17x+11 =< 28x^2
f(x) = O(x^2)
b)
f(x) = x^2+1000
|f(x)| =< c|x^2|
x^2+1000 =< x^2 + 1000x^2
x^2+1000 =< 1001x^2
f(x) = O(x^2)
c)
f(x) = x * log(x)
|f(x)| =< c|x^2|
x log(x) =< x^2 * log(x^2)
x log(x) =< 2x^2 * log(x)
f(x) = O(x^2)
d)
f(x) = (x^4)/2
|f(x)| =< c|x^2|
(x^4)/2 =< (x^2)/2
f(x) != O(x^2)
e)
f(x) = 2^x
|f(x)| =< c|x^2|
2^x =< 2^(x^2)
f(x) = O(x^2)
f)
f(x) = ⌊x⌋ ⋅ ⌈x⌉
|f(x)| =< c|x^2|
⌊x⌋ ⋅ ⌈x⌉ =< ⌊x^2⌋ ⋅ ⌈x^2⌉
f(x) = O(x^2)
Download