[C] binary search

Course: Data Structure ( Chen Yue , Ho Ming Chin )


This problem required to achieve binary search algorithm.

Function interface definition:

Position BinarySearch( List L, ElementType X );

Wherein the Liststructure is defined as follows:

typedef int Position;
typedef struct LNode *List;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last; /* 保存线性表中最后一个元素的位置 */
};

LIs a linear incoming user table, which ElementTypeelements can be>, ==, <compare incoming data and to ensure that the subject is incremented ordered. Function BinarySearchto find Xthe Dataposition, i.e., the array subscripts (Note: the elements from the start storage subscript 1). Find subscript is returned, otherwise a special mark of failure NotFound.

Referee test program Example:

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 10
#define NotFound 0
typedef int ElementType;

typedef int Position;
typedef struct LNode *List;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last; /* 保存线性表中最后一个元素的位置 */
};

List ReadInput(); /* 裁判实现,细节不表。元素从下标1开始存储 */
Position BinarySearch( List L, ElementType X );

int main()
{
    List L;
    ElementType X;
    Position P;

    L = ReadInput();
    scanf("%d", &X);
    P = BinarySearch( L, X );
    printf("%d\n", P);

    return 0;
}

/* 你的代码将被嵌在这里 */

Sample Input 1:

5
12 31 55 89 101
31

Output Sample 1:

2

Sample Input 2:

3
26 78 233
31

Output Sample 2: 

0

Code:

Yourself achieving ReadInput () function is used to test the details in the final part of the code, not perfect but you can use.

Note that the former members of the body structure is initialized, a pointer to allocate memory structure, or will a memory error.

References: C language structure pointer initialization

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 10
#define NotFound 0
typedef int ElementType;

typedef int Position;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last; /* 保存线性表中最后一个元素的位置 */
};
typedef struct LNode *List;

List ReadInput(); /* 元素从下标1开始存储 */
Position BinarySearch( List L, ElementType X );

int main()
{
    List L;
    ElementType X;
    Position P;

    L = ReadInput();
    
    scanf("%d", &X);
    P = BinarySearch( L, X );
    printf("%d\n", P);

    return 0;
}

/* 你的代码将被嵌在这里 */
Position BinarySearch( List L, ElementType X ){
    Position minP = 1, maxP = L->Last, midP;
    Position retP = NotFound;
    while(minP <= maxP ){
        midP = (minP + maxP)/2;
        if(L->Data[midP] == X){
            retP = midP;
            break;
        }
        else if(L->Data[midP] > X){
            maxP = midP - 1;
            continue;
        }
        else{
            minP = midP + 1;
            continue;
        }
    }
    return retP;
}

/* 输入数据存入结构对象中,返回指针 */
List ReadInput(){
    List pList = (List)malloc(sizeof(struct LNode));
    pList->Data[0] = 0;
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; i++){
        scanf("%d",&(pList->Data[i]));
    }
    pList->Last = n;
    return pList;
}

 

Published 37 original articles · won praise 47 · Views 100,000 +

Guess you like

Origin blog.csdn.net/u013378642/article/details/105036929