Sponsored Links
This presentation is the property of its rightful owner.
1 / 86

5.サーチ PowerPoint PPT Presentation


  • 92 Views
  • Uploaded on
  • Presentation posted in: General

5.サーチ. 5-1.線形探索 5-2.2分探索 5-3.ハッシュ. 入力:n個のデータ (ここで、入力サイズは、   とします。) さらに、キー 出力:        となる   があるときは、 その位置     キーが存在しないとき、-1. サーチ問題. 探索(サーチ). 2. 5. 3. 8. 1. 4. 13. 9. 入力:. 配列 A. A[0]. A[1]. A[i]. A[n-1]. 3. キー. K. 出力:. キーが存在:キーを保持している配列のインデックス(添え字).

Download Presentation

5.サーチ

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript




13

9

A

A[0]

A[1]

A[i]

A[n-1]

A[1]K


13

9

A

A[0]

A[1]

A[i]

A[n-1]




-



0

1

2

3

4

5

6

7

0

1

2

3

4

5

6

7

A

13

9

A

13

9

0

1

2

3

4

5

6

7

0

1

2

3

4

5

6

7

A

13

9

A

13

9

retun 3;


0

1

2

3

4

5

6

7

A

13

9

0

1

2

3

4

5

6

7

0

1

2

3

4

5

6

7

A

13

9

A

13

9

7

retun -1;


  • /*

  • */

  • int linear_search(double k)

  • {

  • int i; /* */

  • for(i=0;i<n-1;i++)

  • {

  • if(A[i]==k)

  • {

  • return i;/**/

  • }

  • }

  • return -1; /**/

  • }


LSlinear_seach

forp

[0]-A[p-1]k

LSlinear_seach





  • /*

  • */

  • int linear_search(double k)

  • {

  • int i=0; /* */

  • while(A[i]!=k){

  • i++;

  • }

  • return i; /* */

  • }


A[0]

7

13

9

A[7]

XXXX

yyyyy

zzzzz




0

1

2

3

4

5

6

7

0

1

2

3

4

5

6

7

8

8

A

13

9

A

13

9

0

1

2

3

4

5

6

7

8

0

1

2

3

4

5

6

7

8

A

13

9

A

13

9

if(i<n)retun i;


(

0

1

2

3

4

5

6

7

0

1

2

3

4

5

6

7

8

8

A

13

9

A

13

9

0

1

2

3

4

5

6

7

8

0

1

2

3

4

5

6

7

8

A

13

9

A

13

9

if(i==n)retun -1;


  • /* */

  • int banpei_search(double k)

  • {

  • int i=0; /* */

  • A[n]=k; /**/

  • while(A[i]!=k) {

  • i++;

  • }

  • }

  • if(i<n){

  • return i; /**/

  • }else {

  • return -1;/**/

  • }

  • }


BAN1banpei_seach

banpei_searh

A[0]-A[n]

5



-2



(

0

1

2

3

4

5

6

7

A

13

9

0

1

2

3

4

5

6

7

A

9

13

0

1

2

3

4

5

6

7

A

9

13

0

1

2

3

4

5

6

7

9

13

retun ;


(

0

1

2

3

4

5

6

7

A

9

13

0

1

2

3

4

5

6

7

A

9

13

0

1

2

3

4

5

6

7

A

9

13

A

9

13

retun -1;


right]

left]

mid]

left]

mid-1]

right]

mid+1]

retun mid;


2

A[mid]

A[left]

A[right]

key


11

15

0

1

2

3

4

5

6

7

8

9

10

12

13

14

A

9

13

14

17

19

20

21

25

26

28

29

30

(1)

key

(2)

key

10

(3)

key

20

(4)

key

23


2


  • /*

  • */

  • int loop_b_search(double k){

  • int left=0,right=N-1,mid; /* */

  • while(left<=right){

  • mid=(left+right)/2;

  • if(A[mid]==k){return mid;}/**/

  • if(k<A[mid]){right=mid-1;}/**/

  • if(A[mid]<k){left=mid+1;}/**/

  • }

  • return -1;/**/

  • }


LBSloop_b_search

A[left]A[right]

A[mid]<k

A[left]A[mid]k

K<A[mid]

A[mid]A[right]k


A[left]A[mid]


LBSloop_b_search

A[left]A[right]

A[mid]<k

kA[mid+1]A[right]

K<A[mid]

kA[left]A[mid-1]


LBSloop_b_search

loop_b_search

while

right-left


  • /* */

  • int loop_b_search(double k){

  • int left=0,right=N-1,mid; /* */

  • while(left<=right){

  • mid=(left+right)/2;

  • if(A[mid]==k)return mid;/**/

  • if(k<A[mid]){right=mid;}/**/

  • if(A[mid]<k){left=mid;}/**/

  • }

  • return -1;/**/

  • }


0

1

2

3

A

2

5

k

1

2

3

4

5


  • /* */

  • int rec_b_search(double k,int left,int right){

  • int mid;

  • if(left>right){/**/

  • return -1;/* */

  • }else{/**/

  • mid=(left+right)/2;

  • if(A[mid]==k){/**/

  • return mid;

  • }else if(k<A[mid]){/**/

  • return rec_b_search(k,left,mid-1);

  • }else if(A[mid]<k){/**/

  • return rec_b_search(k,mid+1,right);

  • }

  • }

  • }


rec_b_search

loop_b_search


i






-3




A

A[0]

A[1]

A[i]

A[M-1]

A[j]


suzuki,sato,kusakari,




suzuki,sato,kusakari,


M=


abe

B[0]

[0]

abe

B[1]

[1]

B[2]

[2]

B[3]

[3]

ito

B[4]

[4]

ito

B[5]

[5]

B[6]

[6]

B[7]

[7]









M=


abe

[0]

ou

[1]

[2]

[3]

ito

[4]

[5]

[6]

[7]



abe-> oku


abe

[0]

ou

ou

[1]

[2]

[3]

ito

[4]

[5]

[6]

[7]



abe

[0]

ou

[1]

abe

[2]

key

[3]

ito

[4]

[5]

[6]

[7]


(

abe

[0]

ou

ou

key

[1]

[2]

[3]

ito

[4]

[5]

[6]

[7]


(

  • /* */

  • void input()

  • {

  • int h; /**/

  • for(i=0;i<n;i++)

  • {

  • h=hash(x[i]);

  • A[h]=x[i];

  • }

  • }

  • return;

  • }


  • /* */

  • int search(double key)

  • {

  • int h; /**/

  • h=hash(key);

  • if(A[h]) {

  • return -1;/**/

  • } else if(A[h]==key) {/**/

  • return h;

  • }

  • }


(

  • /* */

  • void input()

  • {

  • int h=0; /**/

  • for(i=0;i<n;i++){

  • h=hash(x[i]);

  • while(A[h]){/**/

  • h=(h+1)%M;

  • }

  • A[h]=X[j];

  • }

  • return;

  • }


  • /* */

  • int search(double key)

  • {

  • int h; /**/

  • h=hash(key);

  • while(1){/**/

  • if(A[h]) {

  • return -1;/**/

  • }else if(A[h]!=key) {/**/

  • h=(h+1)%M; /**/

  • }else if(A[h]==key) {/**/

  • return h;

  • }

  • }

  • }



A[0]

A[1]

A[i]

A[M-1]

A[j]

k




A[0]

A[1]

A[i]

A[M-1]

A[j]

A[0]

A[1]

A[i]

A[M-1]

A[j]



A[0]

A[1]

A[i]

A[M-1]

A[j]






  • Login