Programming abstractions in C閱讀筆記:p181-p183
《Programming Abstractions In C》學(xué)習(xí)第61天,p181-p183總結(jié)。
一、技術(shù)總結(jié)
1.linear search algorithm
2.lexicographic order(字典順序)
3.binary search algorithm(二分查找算法)
/*
* 1.二分查找也應(yīng)用了遞歸的思想。
* 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;
}
二、英語(yǔ)總結(jié)
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. 英語(yǔ)
(1)Etymology Dictionary:https://www.etymonline.com
(2) Cambridage Dictionary:https://dictionary.cambridge.org

歡迎搜索及關(guān)注:編程人(a_codists)
浙公網(wǎng)安備 33010602011771號(hào)