Programming Abstractions in C阅读笔记:p293-p302

《Programming Abstractions in C》学习第73天,p293-p302总结,总计10页。

一、技术总结

1.时间复杂度

(1)quadratic time(二次时间)

p293, Algorithms like selection sort that exhibit O(N^2) performance are said to run in quadratic time。

2.线性查找(linear search)

p293, Because the for loop in the implementation executes n time, you expect the performance of LinearSearch——as its name implies——to be O(N)。时间复杂度为O(N)的查找称为线性查找。

3.归并排序(merge sort)

书中并为对归并排序做严格的定义。p298, The merge operation, combined with recursive decomposition, gives rise to a new sorting algorithm called merge sort, which you can implement in straightforward way. The basic idea of the algorithm can be outlined as follows:

  1. Check to see if the array is empty or has only one element. If so, it must alread be sorted, and the function can return without doing any work. This condition defines the simple case for the recursion.
  2. Divide the array into two new subarrays, each of which is half the size of the original.
  3. Sort each of the subarrasy recursively.
  4. Merge the two subarrays back into the original one.
/*
 * 伪代码
 */
static void Merge(int array[], int arr1[], int n1, int arr2[], int n2);
static int *CopySubArray(int array[], int start, int n);
void SortIntegerArray(int array[], int n);
/*
 * Function: SortIntegerArray
 * Usage: SortIntegerArray(array, n);
 * ----------------------------------
 * This function sorts the first n elements of array into
 * increasing numerical order using the merge sort algorithm,
 * which requires (1) dividing the array into two halves,
 * (2) sorting each half, and (3) merging the halves together.
 */
void SortIntegerArray(int array[], int n) {
    int n1, n2, *arr1, *arr2;

    if (n <= 1) {
        return;
    }
    n1 = n / 2;
    n2 = n - n1;

    arr1 = CopySubArray(array, 0, n1);
    arr2 = CopySubArray(array, 0, n2);
    SortIntegerArray(arr1, n1);
    SortIntegerArray(arr1, n2);
    Merge(array, arr1, n1, arr2, n2);
    FreeBlock(arr1);
    FreeBlock(arr2);
}

/*
 * Function: Merge
 * Usage: Merge(array, arr1, n1, arr2, n2);
 * ----------------------------------------
 * This function merges two sorted arrays (arr1 and arr2) into a
 * single output array. Because the input arrays are sorted, the
 * implementation can always select the first unused element in
 * one of the input arrays to fill the next position in array.
 */

static void Merge(int array[], int arr1[], int n1, int arr2[], int n2) {
    int p, p1, p2;

    p = p1 = p2 = 0;
    while (p1 < n1 && p2 < n2) {
        if (arr1[p1] < arr2[p2]) {
            array[p++] = arr1[p1++];
        } else {
            array[p++] = arr2[p2++];
        }
    }
    while (p1 < n1) {
        array[p++] = arr1[p1++];
    }
    while (p2 < n2) {
        array[p++] = arr2[p2++];
    }
}

/*
 * Function: CopySubArray
 * Usage: CopySubArray(array, arr1, n1, arr2, n2);
 * -----------------------------------------------
 * This function makes a copy of a subset of an integer array
 * and returns a pointer to a new dynamic array containing the
 * new elements. The array begins at the indicated start
 * position in the original array and continues for n elemnts.
 */

static int *CopySubArray(int array[], int start, int n) {
    int i, *result;

    result = NewArray(n, int);
    for (i = 0; i < n; i++) {
        result[i] = array[start + i];
    }
    return result;
}

二、英语总结

1.quadratic是什么意思?

答:In mathematics by 1660s;the algebraic quadratic euqation(二次方程, 1680s) are so called because they involve the square(x^2) and no higher power of x。这里要注意quarter是四分之一,但是quadratic是二次(x^2)的意思。

2.sophistication是什么意思?

答:

(1)sophist > sophiticate > sophistication。

(2)sophist: one who makes use of fallacious arguments(诡辩者)。

(3)sophistication: u. the quality of being sophisticated(老练, 复杂度)。

p293, The problem, howerver is that average-case analysis is much more difficult to carry out and typically requires considerable mathematical sophistication。这里比较重要的一点是虽然这里用的是名词sophistication,但是因为翻译的时候需要转一下,mathematical sophistication(复杂的数学知识)。

3.average用法总结

答:

(1)vt. to reach a particular amount as an average。主语可以是物也可以是人。翻译的时候需要灵活转变。

主语是物:Inquires to our office average 1000 calls a month.

主语是人:p294, From a practical point of view, it is often useful to consider how well an algorithm performs if you average its behavior over all possible sets of input data

三、其它

本书中作者讲解归并排序(merge sort)和其它书还是有些不同的,从选择排序算法展开,从而引出归并排序算法,可以和其它书进行对比阅读。

四、参考资料

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)

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,366评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,521评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,689评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,925评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,942评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,727评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,447评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,349评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,820评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,990评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,127评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,812评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,471评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,017评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,142评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,388评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,066评论 2 355

推荐阅读更多精彩内容