实验5作业

实验结论


Part 1 使用二分查找,在一组有序元素中查找数据项

1. 形参是数组,实参是数组名

ex1_1.cpp
#include  <stdio.h>
const int N=10;
int binarySearch(int x[], int n, int item);
int main() {
    int a[N]={1,3,9,16,21,22,25,28,30,39};
    int i,index, key;
    
    printf("数组a中的数据:\n");
    for(i=0;i<N;i++)
       printf("%d ",a[i]);
    printf("\n");
    
    printf("输入待查找的数据项: ");
    scanf("%d", &key);
    
    // 调用函数binarySearch()在数组a中查找指定数据项item,并返回查找结果给index 
    index = binarySearch(a,N,key);// 补足代码① 
    // ××× 
    
    if(index>=0) 
        printf("%d在数组中,下标为%d\n", key, index);
    else
        printf("%d不在数组中\n", key); 
   
   return 0;
}

//函数功能描述:
//使用二分查找算法在数组x中查找特定值item,数组x大小为n 
// 如果找到,返回其下标 
// 如果没找到,返回-1 
int binarySearch(int x[], int n, int item) {
    int low, high, mid;
    
    low = 0;
    high = n-1;
    
    while(low <= high) {
        mid = (low+high)/2;
        
        if (item == x[mid])
            return mid;
        else if(item < x[mid]/*补足代码②*/)
            high = mid - 1;
        else
            low = mid + 1;
    }
    
    return -1;
}

运行结果:
1626197-20190528005049126-306404022.png

2. 形参是指针变量,实参是数组名

ex1_2.cpp
#include  <stdio.h>
const int N=10;
int binarySearch(int *x, int n, int item);
int main() {
    int a[N]={1,3,9,16,21,22,23,24,25,26};
    int i,index, key;
    
    printf("数组a中的数据:\n");
    for(i=0;i<N;i++)
       printf("%d ",a[i]);
    printf("\n");
    
    printf("输入待查找的数据项: ");
    scanf("%d", &key);
    
    // 调用函数binarySearch()在数组a中查找指定数据项item,并返回查找结果
    index = binarySearch(a,N,key);// 补足代码①
    // ××× 
    
    if(index>=0) 
        printf("%d在数组中,下标为%d\n", key, index);
    else
        printf("%d不在数组中\n", key); 
   
   return 0;
}

//函数功能描述:
//使用二分查找算法在x指向的数据项开始的n个数据中,查找item
// 如果找到,返回其位置
// 如果没找到,返回-1 
int binarySearch(int *x, int n, int item) {
    int low, high, mid;
    
    low = 0;
    high = n-1;
    
    while(low <= high) {
        mid = (low+high)/2;
        
        if (item == *(x+mid))
            return mid;
        else if(item < *(x+mid)/*补足代码②*/)
            high = mid - 1;
        else
            low = mid + 1;
    }
    
    return -1;
}

运行结果:
1626197-20190528005452712-1343909352.png

Part 2. 选择法排序

ex2_2.cpp

// 练习:使用选择法对字符串按字典序排序
#include <stdio.h>
#include <string.h>
void selectSort(char str[][20], int n ); // 函数声明,形参str是二维数组名 
int main() {
    char name[][20] = {"John", "Alex", "Joseph", "Candy", "Geoge"};
    int i;
    
    printf("输出初始名单:\n");
    for(i=0; i<5; i++)
        printf("%s\n", name[i]);
        
    selectSort(name, 5);  // 调用选择法对name数组中的字符串排序
    
    printf("按字典序输出名单:\n");
    for(i=0; i<5; i++)
        printf("%s\n", name[i]);
     
    return 0;
} 

// 函数定义
// 函数功能描述:使用选择法对二维数组str中的n个字符串按字典序排序 
void selectSort(char str[][20], int n) {
    // 补足代码
    // ××× 
    int i,j,k;
    char t[20];
    
    for(i=0;i<n-1;i++){
        k=i;
        for(j=i+1;j<n;j++){
            if(strcmp(str[j],str[k])<0)
            k=j;
        }
        if(k!=i){
            strcpy(t,str[i]);
            strcpy(str[i],str[k]);
            strcpy(str[k],t);
        }
    }
}

运行结果:
1626197-20190528183444047-971035270.png

Part 3. 用指针处理字符串

实验总结与体会


1.二分查找:使用二分查找,必须是在一组有序元素中查找数据项。数组元素个数奇数偶数都可以。
2.选择法排序:不理解教材例5.29为什么在main函数中要定义一个名为temp的数组。
3.使用指针变量对字符串进行处理:待更新。

转载于:https://www.cnblogs.com/zsuzifan/p/10934733.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值