재귀호출 문제인거 같습니다만..

shs0917의 이미지

#include	<stdio.h>

#define	MAX 100
#define COMPARE(x,y) (((x)<(y))?-1:((x)>(y))?1:0)

int sort(int [], int);
int binsearch(int [], int, int, int);

int main(void){
	int index, left = 0, n, searchnum;
	int data[MAX];
	
	printf("Enter the n: ");
	scanf("%d", &n);
	
	if(n < 1 || n > MAX){
		fprintf(stderr, "Input error\n");
		exit(1);
	}

	for(index = 0; index < n; index++){
		data[index] = rand() % 100;
		printf("%d ", data[index]);
	}

	printf("Created data:\n");

	if(sort(data, n) != 0){
		fprintf(stderr, "Sort error!\n");
	}

	printf("Selection sorted data:\n");

	for(index = 0; index < n; index++){
		printf("%d ", data[index]);
	}
	
	printf("End\n");
	
	printf("Enter the search number : ");
	scanf("%d", &searchnum);
	
	index = binsearch(data, left, n, searchnum);
	if(index == -1){
		fprintf(stderr, "Sorry, not found searchnumber.\n");
	}
	else{
		printf("Search number is %d.\n", data[index]);
		printf("Search number index is %d.\n", index + 1);
	}
}

int sort(int sdata[], int sn){
	int swap(int *, int *);
	int index1, index2, min;
	for(index1 = 0; index1 < sn - 1; index1++){
		min = index1;
		for(index2 = index1 + 1; index2 < sn; index2++){
			if(sdata[min] > sdata[index2]){
				min = index2;
			}
		}
		if(swap(&sdata[min], &sdata[index1]) != 0){
			fprintf(stderr, "Swap error!\n");
		}
		
	}
	
	return 0;
}

int swap(int *x, int *y){
	int temp;
	temp = *x;
	*x = *y;
	*y = temp;

	return 0;
}
int binsearch(int sdata[], int left, int right, int searchnum){
	int middle;

	while(left <= right){
		middle = (left + right)/2;
		switch(COMPARE(sdata[middle], searchnum)){
			case -1: binsearch(sdata, middle + 1, right, searchnum);
			case 0: return middle;
			case 1: binsearch(sdata, left, middle - 1, searchnum);
		}
	}
	return -1;
}

이진탐색 알고리즘인데요..
처음 몇번 실행하면 되는데.. 다음부터는 서치부분에서 그냥.. 뻗어서 암짓도
안합니다. 재귀호출을 쓰면.. 스택이 넘칠경우 문제가 될 수 있다는 얘기를
어디서 들은듯 한데... 조언 부탁 드립니다.
그리고.. 스택영역 검사할 수 있는 방법 없을까요?
hackexpert의 이미지

자세하게 보지는 않았지만..
만약 일치 하는 값이 없으면 어떻게 되나요?;;
그냥 무한 재귀 될 것 같은데..

그리고 왠만한 경우 아니면 스택 잘 안넘칩니다.. ^^;
소스 한번 다시 봐야 겠네요..

litdream의 이미지

재귀호출이 아니고, binsearch() 입니다.
(0,2), (1,3), ... ( n, n+2 ) 의 인덱스에서 문제가 생기겠군요?
계속 같은 인덱스를 넘기면서 재귀호출 할겁니다.

한번 테스트해보세요.

삽질의 대마왕...

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.