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

Lecture 02 - Algorithm Design

Uploaded by

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

Lecture 02 - Algorithm Design

Uploaded by

manighasemi2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Algorithm Design

Fundamentals of Computer and Programming

Hossein Zeinali
Modified Slides from Dr. Bahador Bakhshi
CE Department, Amirkabir University of Technology
What We Will Learn
Sample algorithms to practice problem
solving steps
Input & Output analysis
Algorithm design
 Pseudo-code

2
‫ عدد‬۳ ‫محاسبه میانگین‬

Algorithm: Average

1- print “Please enter three integers”


2- read x1, x2, x3
3- sum  x1 + x2 + x3
4- average  sum / 3
5- print “Average = ” average

3
‫الگوريتم تشخیص زوج يا فرد بودن عدد‬
Algorithm: Odd-Even-1

1- print “Please enter an integer”


2- read n
3- y  n mod 2
4- if (y == 0)
print “Number is even”
else
print “Number is odd”

4
‫الگوريتم تشخیص زوج يا فرد بودن عدد‬
Algorithm: Odd-Even-2
1- print “Please enter an integer”
2- read n
3- if(n < 0)
n  -1 * n
4- while(n >= 2)
nn–2
5- if(n == 0)
print “even”
else
print “odd” Verify the
Algorithm

5
‫الگوريتم تشخیص زوج يا فرد بودن عدد‬
Algorithm: Odd-Even-3
1- print “Please enter an integer”
2- read n
3- while (n >= 2) or (n <= -1)
n  n - sign(n) * 2
4- if (n == 1)
print “odd”
else
print “even”

6
‫ تمام ميشود را ميگیرد و‬0 ‫الگوريتمي كه يك رشته عدد را كه با‬
‫تعداد اعداد زوج و فرد را چاپ ميكند‬
Algorithm: Count Odd-Even
odd_cnt  0
even_cnt  0
print “Please enter an integer”
read n
while (n != 0)
y  n mod 2
if (y == 0)
even_cnt  even_cnt + 1
else
odd_cnt  odd_cnt + 1
print “Please enter an integer”
read n

print “Odd = “ odd_cnt “Even = “ even_cnt

7
‫الگوريتمي كه يك عدد صحیح مثبت را بگیرد و مجموع ارقام آن را‬
‫چاپ كند‬

Algorithm: Digit-Sum
print “Please enter a positive integer”
read n
sum  0
mn
while (n != 0)
y  n mod 10
sum  sum + y
nn-y
n  n / 10
Verify the
print “sum of digits of” m “ = “ sum Algorithm

8
‫ چاپ‬8 ‫الگوريتمي كه يك عدد صحیح مثبت را بگیرد و آنرا در مبناي‬
‫كند‬
Algorithm: Base-8
print “Please enter a positive integer”
read n
i0
while (n != 0)
x[i]  n mod 8
n  floor (n / 8)
ii+1
ii-1
while (i >= 0)
print x[i]
ii-1
9
‫الگوريتمي كه يك عدد صحیح مثبت را بگیرد و فاكتوريل آنرا تولید كند‬
Algorithm: Factorial-1
print “Please enter a positive integer”
read n
i1
result  1
while (i <= n)
result  i * result
ii+1

return result

10
‫الگوريتمي كه يك عدد صحیح مثبت را بگیرد و فاكتوريل آنرا تولید كند‬
Algorithm: Factorial-2
print “Please enter a positive integer”
read n
result  1
while (n > 0)
result  result * n
nn-1

return result

11
‫الگوريتمي كه يك عدد صحیح مثبت را بگیرد و فاكتوريل آنرا تولید كند‬
Algorithm: Factorial-Recursive (n)

if (n <= 1)
return 1
else
return n * Factorial-Recursive (n - 1)

12
‫ و محل عضو‬start ‫الگوريتمي كه يك رشته عدد را كه محل عضو اول آن با‬
.‫ مشخص شده است را به صورت صعودي مرتب كند‬end ‫آخر آن با‬

Algorithm: sort (x, start, end)


while (start != end)
j  find index of minimum element from start to end
swap x[j] and x[start]
start  start + 1
==================
Algorithm find_min(x, start, end)
y  start
i  start + 1
while (i <= end)
if(x[i] < x[y])
yi
ii+1 Verify the
return y Algorithm

13
‫الگوريتمي كه يك رشته عدد را كه محل عضو اول آن با ‪ start‬و محل عضو‬
‫آخر آن با ‪ end‬مشخص شده است را به صورت صعودي مرتب كند‪.‬‬

‫)‪Algorithm swap(x, j, i‬‬

‫]‪temp  x[j‬‬
‫]‪x[j]  x[i‬‬
‫‪x[i]  temp‬‬

‫)‪Algorithm swap2(x, j, i‬‬

‫]‪x[j]  x[j] + x[i‬‬


‫]‪x[i]  x[j] – x[i‬‬
‫]‪x[j]  x[j] – x[i‬‬
‫‪14‬‬
‫الگوريتمي كه آرایه صعودی از اعداد صحیح را بگیرد و آنرا تبدیل به آرایه‬
.‫نزولی کند‬

Algorithm reverse(A, start, end)

if (start >= end)


return
else
swap(A, start, end)
reverse(A, start + 1, end - 1)

15
Tower of Hanoi
Algorithm hanoi(n, source, target, auxiliary):
if (n <= 0)
return

# Move n - 1 disks from source to auxiliary


hanoi(n - 1, source, auxiliary, target)

# Move the nth disk from source to target


append the source last disk to the target

# Move the n - 1 disks that we left on auxiliary onto target


hanoi(n - 1, auxiliary, target, source)

A = [3, 2, 1]
B = []
C = []
hanoi(3, A, C, B)
16
Eight Queens Puzzle

17
Summary
 There are more than one algorithm for a problem
 Efficiency, Complexity, Clarity, …

 Algorithm (Programming Language) building blocks


 Calculations (Lecture 4)
 Input / Output (Lecture 5)
 Decision Making (Lecture 6)
 Repeating (Lecture 7)
 Modular Programming (Lecture 8)
 Arrays + Memory Management (Lectures 9 + 10)
 Others (Files, …) (Lecture 11 + 12)

18

You might also like