首页 > 其他分享 >Programming abstractions in C阅读笔记:p181-p183

Programming abstractions in C阅读笔记:p181-p183

时间:2023-10-23 09:12:42浏览次数:36  
标签:p181 string p183 abstractions int BinarySearch key return array

《Programming Abstractions In C》学习第61天,p181-p183总结。

一、技术总结

1.linear search algorithm

2.lexicographic order(字典顺序)

3.binary search algorithm(二分查找算法)

/*
 * 1.二分查找也应用了递归的思想。
 * 2.这里的代码只是demo
 */
#include <stdio.h>
#include "strlib.h"

int FindStringInSortedArray(string key, string array[], int n);

static int BinarySearch(string key, string array[], int low, int high);

/*
 * Function: FindStringInSortedArray
 * Usage: index = FindStringInSortedArray(key, array, n);
 * ------------------------------------------------------
 * This function searches the array looking for the specified
 * key. The argument n specifies the effective size of the
 * array, which must be sorted according to the lexicographic
 * order imposed by StringCompare. If the key is found, the
 * function returns the index in the array at which that key
 * appears. (If the key appears more that once in the array,
 * any of the matching indices may be return). If the key
 * does not exist in the array, the function returns -1. In
 * this implementation, FindStringInSortedArray is simply a
 * wrapper; all the work is done by the recursive function
 * BinarySearch.
 */
int FindStringInSortedArray(string key, string array[], int n) {
    return BinarySearch(key, array, 0, n - 1);
}

/*
 * Function: BinarySearch
 * Usage: index = BinarySearch(key, array, low, high);
 * ---------------------------------------------------
 * This function does the work for FindStringInSortedArray.
 * The only difference is that BinarySearch takes both the
 * upper and lower limit of the search.
 */
static int BinarySearch(string key, string array[], int low, int high) {
    int mid, cmp;

    if (low > high) {
        return -1;
    }
    mid = (low + high) / 2;
    cmp = StringCompare(key, array[mid]);
    if (cmp == 0) {
        return mid;
    }
    if (cmp < 0) {
        return BinarySearch(key, array, low, mid - 1);
    } else {
        return BinarySearch(key, array, mid + 1, high);
    }
}

int main() {
    int index;
    char *arr[] = {"Programming Abstractions in C", "Hello World", "C"};
    index = FindStringInSortedArray("C", arr, 3);
    printf("index is: %d", index);
    return 0;
}

二、英语总结

1.lecicographic是什么意思?

答:

(1)lexicographic < lexicography: adj. of or relating lexicography(字典的)。

(2)lexicography: lexico-("wordbook",字典) + -graphy("to write")

2.adhere是什么意思?

答:p182,Although most of the recursive functions you encounter are likely to adhere to this style, the definition of the recursion is actually somewhat broader。ad-("to") + haerere("to stick")。vi. to stick firmely(附着,遵循)。后面常接介词to。

三、参考资料

1. 编程

(1)Eric S.Roberts,《Programming Abstractions in C》:https://book.douban.com/subject/2003414

2. 英语

(1)Etymology Dictionary:https://www.etymonline.com

(2) Cambridage Dictionary:https://dictionary.cambridge.org

欢迎搜索及关注:编程人(a_codists)

标签:p181,string,p183,abstractions,int,BinarySearch,key,return,array
From: https://www.cnblogs.com/codists/p/17781617.html

相关文章

  • Programming abstractions in C阅读笔记:p179-p180
    《ProgrammingAbstractionsInC》学习第60天,p179-p180总结。一、技术总结1.palindrome(回文)(1)包含单个字符的字符串(如"a"),或者空字符串(如"")也是回文。(2)示例:“level”、"noon"。2.predicatefunction(1)predicate的意思pre-("forth")+*deik-("show"),“t......
  • Programming abstractions in C阅读笔记:p176-p178
    《ProgrammingAbstractionsInC》学习第59天,p176-p178总结。一、技术总结1.addtivesequencestn=tn-1+tn-2序列:3,7,10,17,27,44,71,115,186,301,487,788,1275,...p177,Asageneralclass,thesequencesthatfollowthispatternarecalledadditivesequen......
  • Programming abstractions in C阅读笔记:p176-p178
    《ProgrammingAbstractionsInC》学习第59天,p176-p178总结。一、技术总结1.addtivesequencestn=tn-1+tn-2序列:3,7,10,17,27,44,71,115,186,301,487,788,1275,...p177,Asageneralclass,thesequencesthatfollowthispatternarecalledadditive......
  • Programming abstractions in C阅读笔记:p166-p175
    《ProgrammingAbstractionsInC》学习第58天,p166-p175总结。一、技术总结1.斐波那契数列(FibonacciSequenc)(1)斐波那契数列来源斐波那契数列来自于《LiberAbaci》一书里兔子繁殖问题,相关资料很多,这里不赘述。(2)关于《LiberAbaci》一书《LiberAbaci》——Liber:abook......
  • Programming abstractions in C阅读笔记:p132-p137
    《ProgrammingAbstractionsInC》学习第53天,p132-p137,3.2小节“strings”总结如下:一、技术总结3.2小节介绍了字符串的用法:1.C语言是没有字符串(string)这种数据类型的,但是实际的场景中又很需要这种数据类型,那怎么表示字符串呢?有两种方法:1.用字符数组表示。2.用字符指针表示。......
  • Programming abstractions in C阅读笔记:p130-p131
    《ProgrammingAbstractionsInC》学习第52天,p130-p131,总结如下:一、技术总结1.piglatingame通过piglatingame掌握字符复制,指针遍历等操作。/**输入:字符串,这里采用书中坐着自定义的getline函数*/#include<stdio.h>#include<string.h>#include"simpio.h"#def......
  • Programming abstractions in C阅读笔记:p127-p129
    《ProgrammingAbstractionsInC》学习第51天,p127-p129,总结如下:一、技术总结1.stringlibrary掌握常用函数如strlen,strcpy用法。2.bufferoverflow(缓冲区溢出)(1)什么是buffer?p129,Arraysthatarepreallocatedandlateruseasarepositoryfordatacalledbuffers......
  • P1830题解
    思路:利用桶存储轰炸区域,双重循环。在存储轰炸区域时将次数刷新,也就是pos[j][k]=i;。下面是核心代码:for(inti=1;i<=x;i++){ intx1,x2,y1,y2; cin>>x1>>y1>>x2>>y2; for(intj=x1;j<=x2;j++) { for(intk=y1;k<=y2;k++) { vis[j][k]++; pos[j][k]=i; } }......
  • Programming abstractions in C阅读笔记:p123-p126
    《ProgrammingAbstractionsInC》学习第50天,p123-p126,总结如下:一、技术总结1.notaion这也是一个在计算机相关书籍中出现的词,但有时却不是那么好理解,因为它可以指代很多对象,这里做一个记录。示例:p124。InC,youcanuseanycharacterarraytoholdstringdata.charstr[6]={......
  • Programming abstractions in C阅读笔记: p118-p122
    《ProgrammingAbstractionsInC》学习第49天,p118-p122,总结如下:一、技术总结1.随机数(1)seedp119,"Theinitialvalue--thevaluethatisusedtogettheentireprocessstart--iscallaseedfortherandomgenerator."二、数学总结1.均匀分布(uniformdistribution)......