procedure
이름(인자: 타입)
ex) procedure maximum(L: list of integers)
변수 := 표현
begin
statements
endif condition
then statement
→ 프로그래밍 언어의 ifwhile condition
statement
→ 프로그래밍 언어의 whilefor var := initial to final
statement
→ 프로그래밍 언어의 for
{comment}
→ 주석, 설명을 덧붙이기 위해 사용
example
procedure
max( : intergers)
max := {largest element so far}
for i := 2 to n {go thru rest of elems}
if max < then max := {found bigger?}
{이 지점에서 max는 list의 원소 중 가장 큰 정수와 같다}
return max {max is the largest element}
example
procedure
sum( : intergers)
s := 0 {sum of elems so far}
for i := 1 to n {go thru all elems}
s := s + ai {add current item}
{이 지점에서 s는 모든 list 안의 값의 합}
return s
procedure
linear search(x : integer, : distinct integers)
i := 1
while (i ≤ n and x ≠ )
i := i+1
if i ≤ n then location := i
else location := i
return location {location is the subscript of the term that equal x, or is 0 if x is not found}
→ 시간 복잡도 : O(n)
procedure
binary search(x : integer, : increasing integers)
i := 1 {i is left endpoint of search interval}
j := n {j is right endpoint of search interval}
while i < j
m :=
if then i := m + 1
else j := m
if x = then location := i
else location := 0
return location {location is the subscript of the term equal to x, or 0 if x is not found
→ 시간 복잡도 : O(log n)
procedure
bubble sort( : real numbers with n ≥ 2)
for i := 1 to n-1
for j := 1 to n-i
if then interchange and
{ is in increasing order}
→ 시간 복잡도 : O(n2

procedure
insertion sort(: real numbers with n ≥ 2)
for j := 2 to n
i := 1
while
i := i + 1
m :=
for k := 0 to j - i - 1
:= m
{ is in increasing order}
→ 시간 복잡도 : O(n2

계산원 알고리즘 : 동전으로 거스름돈을 주는 방법을 계산하는 알고리즘
procedure
change(: values of denominations of coins, where , n : a positive integer
for i := 1 to r
:= 0 { counts the coins of denomination used}
while n ≥
:= + 1 {andd a coin of denomination
n := n -
{ is the number of coins of denomination in the change for i = 1, 2, …, r}
greedy 알고리즘이 모든 알고리즘에 대한 답인 것은 아니다.
→ 해결할 수 없다는 것 증명
1. 가정 : 만약 H(P, I)라는 절차가 존재한다고 가정
2. 만약 프로그램이 멈추면 "halt", 멈추지 않으면 "loops forever" 출력
3. 프로그램 K(P) 정의
K(K)가 "loops forever" 라면 K(K)는 멈춘다 → 모순
K(K)가 "halt" 라면 K(K)는 무한 루프에 빠진다 → 모순
따라서, Halting 문제를 해결할 수 있는 일반적인 알고리즘은 존재하지 않는다.
