1 / 9

11 월 12 일 셈틀 알고리즘 스터디

11 월 12 일 셈틀 알고리즘 스터디. 팀장 노현묵. 재귀함수. 1 .  ' 내가 하려는 작업이 이미 있다 .'  라고 생각 2. 문제를 작게 자른다 . 3. 더 이상 자를 수 없는 부분 (base) 만 처리 출처 : SICP (MIT 컴퓨터 프로그래밍 입문 교재 ). #1. Factorial. #include < stdio.h > int factorial( int n); int main(void) { int i,result ;

dolan
Download Presentation

11 월 12 일 셈틀 알고리즘 스터디

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 11월 12일 셈틀 알고리즘 스터디 팀장 노현묵

  2. 재귀함수 • 1. '내가 하려는 작업이 이미 있다.'  라고 생각 • 2. 문제를 작게 자른다. • 3. 더 이상 자를 수 없는 부분(base)만 처리 • 출처 : SICP (MIT 컴퓨터 프로그래밍 입문 교재)

  3. #1. Factorial #include <stdio.h> int factorial(int n); int main(void) { inti,result;      for(i=0;i<=10;i++){         result = factorial(i); printf("%d! = %d\n",i,result);     }      return 0; } int factorial(int n) {     if(n <= 1)         return 1;     else         return n * factorial(n-1); } • 5! = 5 * 4 * 3 * 2 * 1 • = 5 * 4! 4! = 4 * 3 * 2 * 1 = 4 * 3! 3! = 3 * 2 * 1 = 3 * 2! 2! = 2 * 1

  4. #2. Fibonacci #include <stdio.h> int factorial(int n); int main(void) { inti,result;      for(i=0;i<=10;i++){         result = factorial(i); printf("%d! = %d\n",i,result);     }      return 0; } int factorial(int n) {     if(n <= 1)         return 1;     else         return n * factorial(n-1); } • N1 = 1 • N2 = 1 • N3 = N2 + N1 • N4 = N3 + N2 .... • Nn = Nn-1 + Nn-2

  5. #3. GCD • 유클리드호제법을 이용한 최대공약수 구하기 • N1 = 38515, N2 = 5863 • 38515 / 5863 = 6 … 3337 • 5863 / 3337 = 1 … 2526 • 3337 / 2526 = 1 … 811 • 2526 / 811 = 3 … 93 • 811 / 93 = 8 … 67 • 93 / 67 = 1 … 26 • 67 / 52 = 1 … 15 • 52 / 15 = 3 … 7 • 15 / 7 = 2 … 1 • 7 / 1 = 7 … 0 • 최대공약수 = 1 int GCD(int a, int b) { int mod, tmp;     if(a<b){ tmp = a; a = b; b = tmp; } mod = a%b; if(mod!=0) return GCD(b,mod); else return b; }

  6. 재귀함수의 오버플로우를막는법 int Sum(int num){ if(num==0)return 0; num--;printf("%d ",num); return num;  } Void main(){int a=100000;  while(1) {   a=Add(a);  if(a==0)break;  } } int Show(int num){ if(num==0)return 0; num--;printf("%d ",num); Add(num); } Void main(){ Show(10000);   return 0;} 100,000 이상 입력시오버플로우!

  7. 정렬(sort)의 정의 • 데이타집합을 정해진 순서로 재배열 하는 것 • 오름차순 ( Ascending Order ), 내림차순 ( Descending Order ), 키순 ( Key Order) 등 • 정렬은 비교와 교환의 미학 • 정렬의 기본 Operation은 Comparison과 Exchange • 비교와 교환의 조합이 정렬 알고리즘 • 많은 연구가 된 부분으로 알고리즘의 기초 • 수많은 알고리즘이 존재함 

  8. 정렬알고리즘의 분류 • 복잡성 ( Complexity ) • 단순 : 선택, 삽입, 버블, 쉘정렬 • 난이도 ↓, 성능 ↓, 자원 ↓ ( 보통 O(N2) ) • 복잡 : 퀵, 기수, 힙, 병합정렬 • 난이도 ↑, 성능 ↑, 자원 ↑ ( NlogN ) • 장소에 따라 • 내부 정렬 : 컴퓨터 메모리 내에서 작동( Random Access ) • 외부 정렬 : 테이프 ( Sequential ), 하드디스크 • 교환 방식에 따라 ( DataBase ) • 직접 정렬 : 레코드 전체를 이동하여 정렬 • 간접 정렬 : 별도 존재하는 키 값의 인덱스만 정렬 • Stability ( 안정성 ) • 같은 키 값을 가지는 여러 레코드가 정렬 후에도 그 상대적 순서를 유지 하는가? ( A1, A2가 있을 경우 )

  9. Bubble sort • 오름차순 / 내림차순 • 첫번째와두번째 비교 • Swap • 두번째와세번째 비교 • Swap • 세번째와네번째 비교 • Swap ….쭉쭉쭉

More Related