3 Sequential Search 8 Sequential search int seqsearchint

  • Slides: 36
Download presentation

3

3

������ Sequential Search 8

������ Sequential Search 8

Sequential search int seqsearch(int data[], int n, int key) { int pos; if(key<=data[n-1]) for(pos

Sequential search int seqsearch(int data[], int n, int key) { int pos; if(key<=data[n-1]) for(pos = 0; pos<n; pos++) if(data[pos] == key) return pos; return -1; /* no data found */ } 9

#include <stdio. h< #define N 10 int table[N] = {1, 2, 3, 5, 6,

#include <stdio. h< #define N 10 int table[N] = {1, 2, 3, 5, 6, 7, 10, 22, 23, 31; { int seqsearch(int data[], int n, int key; ( void printlist(int data[], int n; ( Ex 3 void main(void( } int search, result; printlist(table, N( printf("Please enter a data to search ; (“ : scanf("%d", &search; ( result = seqsearch(table, N, search; ( if(result >= 0( printf("Data found at location %dn", result; ( else printf("No such data in the tablen; (" { 10

int seqsearch)int data[], int n, int key( } int pos; if(key<=data[n-1]) for)pos=0; pos<n; pos(++

int seqsearch)int data[], int n, int key( } int pos; if(key<=data[n-1]) for)pos=0; pos<n; pos(++ if(data[pos] == key ( return pos; return -1; /* no data found/* { void printlist(int data[], int n( } int pos; for(pos=0; pos<n; pos(++ printf("%2 d ", data[pos; ([ printf("n; (" { 11

Ex 4 จงเขยนโปรแกรมเพอกรอกขอมลอายของนกศก ษา 10 คน (แบบเรยงลำดบ )แลวทำการคนหาวาอายทต องการอยในลำดบทเทาไรโดยใช Sequential Search Input Output Age

Ex 4 จงเขยนโปรแกรมเพอกรอกขอมลอายของนกศก ษา 10 คน (แบบเรยงลำดบ )แลวทำการคนหาวาอายทต องการอยในลำดบทเทาไรโดยใช Sequential Search Input Output Age 1: 14 Age 2: 15 Age 3: 16 Age 4: 18 Age 5: 19 Age 6: 21 Age 7: 22 Age 8: 23 Age 9: 24 Age 10: 26 Enter Search Age : 18 Result = 4 Enter Search Age : 24 Result = 9 Enter Search Age : 20 Result = Not Found 12

#include <stdio. h> int input)int data([] } Solution Ex 4 int seqsearch(int data[], int

#include <stdio. h> int input)int data([] } Solution Ex 4 int seqsearch(int data[], int n, int key( } 13

void main() { 14

void main() { 14

15

15

Ex 6 กำหนดใหคา search = 7 [0] [1] [2] [3] [4] [5] [6] X

Ex 6 กำหนดใหคา search = 7 [0] [1] [2] [3] [4] [5] [6] X 1 6 9 10 12 17 23 24 Last First รอบท [7] 1 21

Ex 6 search = 7 (ตอ ( 22

Ex 6 search = 7 (ตอ ( 22

Ex 6 search = 7 (ตอ ( 23

Ex 6 search = 7 (ตอ ( 23

int binarysearch(int data[], int n, int key) { int first, last, mid; first=0; last=n-1;

int binarysearch(int data[], int n, int key) { int first, last, mid; first=0; last=n-1; while(first<=last) { mid =(last+first)/2; /*find the center */ if(key < data[mid]) /*move the range down */ last = mid-1; else if(key > data[mid]) /*move range up*/ first = mid+1; else return mid; } return -1; /* no data found */ } 25

#include<stdio. h< #define N 10 Ex 7 int table[N]={1, 2, 3, 5, 6, 7,

#include<stdio. h< #define N 10 Ex 7 int table[N]={1, 2, 3, 5, 6, 7, 10, 22, 23, 31; { int binarysearch(int data[], int n, int key; ( void printlist(int data[], int n; ( void main(void( } int search, result; printf(“Data in TABLEn; (": printlist(table, N; ( printf")Please enter a data to search; (" : scanf("%d", &search; ( result = binarysearch(table, N, search; ( if(result>=0( printf("Data found at position %dn", result; ( else printf("No such data in the tablen; (" { 26

int binarysearch(int data[], int n, int key) { int first, last, mid; first=0; last=n-1;

int binarysearch(int data[], int n, int key) { int first, last, mid; first=0; last=n-1; while(first<=last) { mid =(last+first)/2; /*find the center */ if(key < data[mid]) /*move the range down */ last = mid-1; else if(key > data[mid]) /*move range up*/ first = mid+1; else return mid; } return -1; /* no data found */ } 27

void printlist)int data[], int n( } int pos; for(pos=0; pos<n; pos(++ printf("%2 d ",

void printlist)int data[], int n( } int pos; for(pos=0; pos<n; pos(++ printf("%2 d ", data[pos; ([ printf("n; (" { 28

Ex 8 จงเขยนโปรแกรมเพอรบจำนวนนกศกษา และกรอกคะแนนของนกศกษาตามจำนวนทร บ (แบบเรยงลำดบ ) แลวทำการคนหาวาคะแนนทตองการอย ในลำดบทเทาไหรโดยใช Output Binary Search Input Enter

Ex 8 จงเขยนโปรแกรมเพอรบจำนวนนกศกษา และกรอกคะแนนของนกศกษาตามจำนวนทร บ (แบบเรยงลำดบ ) แลวทำการคนหาวาคะแนนทตองการอย ในลำดบทเทาไหรโดยใช Output Binary Search Input Enter num of student : 6 Student 1: 15. 6 Student 2: 16 Student 3: 18. 75 Student 4: 19. 32 Student 5: 21. 56 Student 6: 28 Enter Search Score : 21. 56 Student No. = 5 Enter Search Score : 15. 6 Student No. = 1 Enter Search Score : 17. 75 Student No. = Not Found 29

#include <stdio. h> int input)float data([] Solution Ex 8 30

#include <stdio. h> int input)float data([] Solution Ex 8 30

int binarysearch(float data[], int n, float key) { 31

int binarysearch(float data[], int n, float key) { 31

void output)int r( } 32

void output)int r( } 32

void main() } 33

void main() } 33

Question? 34

Question? 34

Input Weight 1: 35. 5 Weight 2: 46 Weight 3: 58. 75 Weight 4:

Input Weight 1: 35. 5 Weight 2: 46 Weight 3: 58. 75 Weight 4: 49. 32 Weight 5: 61. 56 Weight 6: 40. 2 Weight 7: 48. 75 Weight 8: 59. 35 Weight 9: 57. 56 Weight 10: 48 Output Before Sorting : 35. 5 46 58. 75 49. 32 61. 56 40. 2 48. 75 59. 35 57. 56 48 After Sorting : 35. 5 40. 2 46 48 48. 75 49. 32 57. 56 58. 75 59. 35 61. 56 Enter Key Weight : 59. 35 Position : 9 ************ Bye ************* 36