본문 바로가기

Algorithms

[Algorithms] 1.Sequential, 2.Add Array, 알고리즘에 대해

반응형
기본적인 알고리즘 연산에 대해 공부해 봅시다 

알고리즘을 작성 할때는 pseudo code로 작성합니다. 왜냐하면 알고리즘이 특정 언어에만 국한된것이 아니기때문입니다.

이번 포스트에서는

1. Sequential Search 알고리즘
2. Add array 알고리즘
3. Exchange Sort 알고리즘에 대해 알아 봅시다


1. Sequential Search 알고리즘은
 찾고자 하는 keytype x를 배열 keytype S[]에서 찾고자 할때 사용합니다.
keytype 은 알맞는 자료형 int,char,...을 나타냅니다.
void search(int n, Const keytype S[],Keytype x,Index & location)
{
location = 1;
while(location<= n && S[location]!= x )
location ++;

if(location > n)
location = 0;
}


2.Add array Members
배열에 있는 값을 모두 더해 반환 할때 사용합니다.
int n개인 배열 s[]에서 배열 요소 값을 모두 더해( result += result +s[i]; ) 반환 합니다. ( return result ) ;
type sum( int n, const number s[])
{
index i;
type result;
result = 0 ;

for( i = 0;i<=n; i ++ )
result += result +s[i];

return result;
}