순차적이지 않은 데이터를 검색하는 방법에 대해서..

n4u9h7의 이미지

a[10] 이라는 배열이 있습니다
0, 1, 2, 3, 4, 5, 6, 7, 8, 9

라는 배열안에 데이터가
10, 11, 12, 3, 4, 5, 6, 7, 8, 9

이런 순서로 데이터가 들어가 있을경우

(위 데이터는 하나의 배열을 전부 다 써서 가득 찼을 경우 다시 0번지부터 새로운 데이터를 입력하게되어서 나온 결과입니다..)

원하는 데이터를 검색하기 위해선 어떠한 알고리즘을 써야할까요..?

순차적인경우 바이너리 서치를 이용하여 검색하면 되긴 하는데

저런경우에는 어떻게 검색을 하면 좋을까요?

qiiiiiiiip의 이미지

보통 먼저 sort를 하죠..
--

혹시나 싶어 찾아보니, 질문하신 내용도 이미 웹에서 찾을 수 있네요.. ^^;;

http://stackoverflow.com/questions/2834652/seaching-for-an-element-in-a-circular-sorted-array

역시 구글링..

snowall의 이미지

정렬 후 검색을 하거나, 정렬도 하고 인덱싱도 하고 검색을 하거나, 해싱을 해놓고 쓰거나...

뭐 속도가 상관 없으면 그냥 처음부터 끝까지 다 뒤져보면 됩니다.

피할 수 있을때 즐겨라! http://melotopia.net/b

n4u9h7의 이미지

[%
#include
#define BUFFER_SIZE 20
#define MAX_SECTOR 10

void main()
{
int buffer[BUFFER_SIZE] = {10, 11, 12, 13, 4, 5, 6, 7, 8, 9};
int front, rear, index, count;
int input = 0;
int MAX_Flag = 1;
int front1, rear1;
int i = 0;

count = 0;
front = 4;
rear = 3;
front1 = front;
rear1 = rear;


printf("input data : ");
scanf("%d", &input);

while(count >= 0)
{
if(front1 <= rear1)
{
index = (front1 + rear1) / 2;
}
else if(front1 >= rear1)
{
index = (((front1 + (rear1 + MAX_SECTOR)) / 2) % MAX_SECTOR);
}

if(buffer

    < input)
    {
    front1 = index + 1;
    count++;
    }
    else if(buffer
      > input)
      {
      rear1 = index - 1;
      count++;
      }
      else
      {
      printf("find data buffer[%d] => %d\n", index, buffer
        );
        break;
        }
        if(count > BUFFER_SIZE)
        {
        printf("not find data!!\n");
        break;
        }
        }
        }
        %]

        위 소스의 front와 rear 값에대해서 이야기 하자면..

        처음 초기에 front는 0이며 rear은 1씩 증가하면서 데이터를 입력하는 구조를 가정한 것입니다.

        환형큐에서 rear값이 가득 찼을 경우 rear값은 다시 처음주소로 돌아오는데

        이때 front 값이 1씩 증가하면서 데이터를 삭제해 나갑니다.

        rear 값이 front 값 보다 작아질 경우 바이너리서치를 이용할 수 없게

        위 배열과 같은 형태로 데이터가 저장 되는데 이 부분을 해결하고자 고민끝에 나온게 저 소스코드입니다...

        따라서 front가 4이고 rear가 3인 이유는 이미 배열의 끝까지 데이터 입력을 끝내고 다시 처음부터 데이터를

        입력한다는 가정을 세운 것입니다.

        혹시나 참고 하실분은 참고하시길 바라며...

        댓글 달기

        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
        이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.