【笔记】第3章 列表

A. 循位置访问

  1. 列表可以通过循秩访问,但复杂度为O(n)
  2. 实现
    在这里插入图片描述

B. 接口与实现

一、从静态到动态

  • 数据结构
    - 静态:仅读取,数据结构的内容及组成一般不变
    - 动态:需写入,数据结构的局部或整体将改变
  • 存储与组成方式
    1. 静态(eg.向量):
      - 数据空间整体创建或销毁
      - 数据元素的物理存储次序与逻辑次序一一对应
      - 支持高效的静态操作
    2. 动态:(eg.列表):
      - 各数据元素动态地分配和回收物理空间
      - 相邻元素记录彼此的物理地址
      - 支持高效的动态操作

二、从向量到列表

  1. 列表(list):采用动态存储策略的典型结构
    · 其中元素称作节点(node),通过指针或引用彼此联接
    · 在逻辑上构成一个线性序列: L = {a0, a1, … an-1}
  2. 相邻节点彼此互称前驱(predecessor)和后继(successor)

三、从秩到位置

  1. 向量:循秩访问
  2. 列表:循位置访问(call-by-position),利用节点之间的相互引用,找到特定的节点(循秩成本过高,与秩r为位置相关)

四、实现

  1. 列表节点:ADT接口
    在这里插入图片描述
  2. 列表:ADT接口
    在这里插入图片描述
  3. 模板类
    - private:size,(头)哨兵元素,(尾)哨兵元素
    - protected:内部函数
    - public:构造函数、析构函数、只读接口、可写接口、遍历接口
    在这里插入图片描述
  4. 头、首、末、尾 节点的秩分别为:-1,0,n-1,n

五、作业

  1. Which of the following options about rank of list node is incorrect下列关于列表的秩的说法中不正确的是:The physical address of a list node can be determined by its rank列表节点的秩与物理地址有明确的对应关系
  2. Which of the following options about defined interface is incorrect?下列关于我们定义的接口的描述中,哪一条是错误的?If the length of the ‘visible list’ part is n, the ranks of the ‘head’, first, last, and ‘trail’ nodes are -1, 0, n, n + 1 respectively.如果一个列表的 visible list 部分长度为 n,则头、首、末、尾节点的秩分别为 -1, 0, n, n + 1

C. 无序列表

一、插入与删除

  1. 插入
    在这里插入图片描述
  2. 删除
    在这里插入图片描述

二、构造与析构

  1. 构造
    在这里插入图片描述
  2. 析构
    在这里插入图片描述

三、查找与去重

  1. 查找
    在这里插入图片描述
  2. 去重
    在这里插入图片描述

四、作业

  1. If you change insertAsPred() to the following function, the result is:若将insertAsPred()改为以下函数,其结果是:Cannot insert node, the structure of the original list is destroyed.不能插入节点,原列表的结构被破坏【pred -> succ】
    在这里插入图片描述
  2. We can consider speeding up call-by-rank by: If r>n/2, then we can continue to access pred() from the tail and eventually move backwards to find the node with rank r.我们可以考虑通过如下方式加快循秩访问的速度:如果 r>n/2,则我们可以从尾部哨兵开始不断访问pred(),最终从后向前地找到秩为r的节点。What kind of argument is wrong about this optimization?关于这种优化,哪种说法是错误的?Through this optimization, we can make the call-by-rank time complexity better than O(n).通过这样的优化,我们可以使循秩访问时间复杂度优于O(n)。【仍为O(n)】

D. 有序列表

一、唯一化

  1. 思路:只保留每个相等元素区间的第一个元素
  2. 实现:uniquify()
    在这里插入图片描述

二、查找

  1. 从后向前比较,返回不大于该元素的最后一个元素
  2. 复杂度:最好O(1), 最差O(n)
  3. 循位置访问
    在这里插入图片描述

三、作业

  1. The process of ordered list unique algorithm is:有序列表唯一化算法的过程是:Keep only the first element of each interval of equal elements.只保留每个相等元素区间的第一个元素
  2. Can a binary search be used in an ordered list to reduce the time complexity to O(log2n)?能否在有序列表中用二分查找使得时间复杂度降为O(log2n)?No, because the list cannot be efficiently accessed by rank不能,因为列表不能高效地循秩访问

E. 选择排序

一、思路

  1. 策略:反复选择其中最大的元素,进行排序
  2. 实现:在O(n)次计较确定M之后,仅交换1次

二、实现算法

  1. selectionSort
    在这里插入图片描述
  2. selectMax
    在这里插入图片描述
  3. 稳定性:有多个重复元素同时命中时,遵循“靠后者优先返回”,即 !lt() 或 ge()
  4. 性能分析
    · selectMax()为Θ(n-k),swap()为O(1),总体复杂度为Θ(n2)
    · 优化点:元素的比较可优化为O(logn)(chapter10)

三、作业

  1. V={11, 5, 7, 13, 2, 3}, sorting V by selection sort, the largest element selected as the unsorted subvector isV={11, 5, 7, 13, 2, 3},对V进行选择排序,被选为未排序子向量中最大的元素依次为:13,11,7,5,3
  2. In order to ensure the stability of the selectSort() algorithm, the measures we take are为了保证selectSort()算法的稳定性,我们采取的措施是:In selectMax(), among the multiple equal largest elements, the selection position is the last oneselectMax()中对于多个相等的最大元素,选取其中位置最靠后者
  3. For a vector or list of size n, the worst-case time complexity for selecting sorting and bubble sorting is.对于规模为n的向量或列表,选择排序和冒泡排序的最坏时间复杂度为:O(n2) O(n2)

F. 循环节*

  1. 任何一个序列都可以分解为若干个循环节,都应对应于一个有序序列
  2. 每个循环节,长度均不超过n;循环节之间,互不相交

G. 插入排序

一、减而治之

  1. 初始化:|S| = r = 0
  2. 迭代:关注并处理e=L[r]
    · 在S中确定适当位置——有序序列查找
    · 插入e,得到S的L[0,r]——有序序列插入
  3. 不变性:随着r的递增,L[0,r)始终有序,直到r=n,L即整体有序

二、比较

  1. 选择排序:U + S (U ≥ S)
  2. 插入排序:S + U

三、实现

  1. 算法
    在这里插入图片描述
  2. 性能分析
    · 就地算法in-place
    · 在线算法online
    · 输入敏感性input sensitivity
    · 复杂度:最好O(n);最坏O(n2)
  3. 平均性能:后向分析(backward analysis)
    · 数学期望的线性率:一组随机变量总和的期望等于它们各自期望的总和
    · O(n2)

三、逆序对Inversion

  1. 逆序对—输入敏感
  2. 将逆序对统一记到后者的账上

四、作业

  1. The average and worst time complexity of insertionSort() isinsertionSort()的平均、最坏时间复杂度分别为: O(n2) O(n2)
  2. The maximum number of reversed pairs contained in a sequence of n elements isn个元素的序列所含逆序对的个数最大是:(n(n-1))/2
    在这里插入图片描述
  3. For a Ordered subsequence in the insertion process order (suppose its length to k)对于插入过程排序中的已排序子序列(设其长度为k):The elements are the k elements at the front of the original sequence其中的元素是原序列中位于前方的k个元素
  4. After a step of insertion sorting, the following sub-sequence V={2,7,13,5,3} is obtained, and the 3 elements are sorted . After another iteration, the result is:在插入排序的某一步后得到如下子序列,此时已排序部分有3个元素。经过又一轮迭代后的结果是:{2,5,7,13,3}

J. 游标*

  1. 动机:某些语言不支持指针类型,即便支持频繁的动态空间分配也影响总体效率
  2. 构思:利用线性数组,以游标方式模拟列表
    · elem[]:对外可见的数据项
    · link[]:数据项之间的引用

XA. Java序列*

  1. interface不能直接实例化为对象,都需要具体地实现其中的接口方法
  2. 序列接口及其实现
    在这里插入图片描述

XB. python列表*

list属于内置的标准数据类型

测验

  1. About vector and the following list, which is wrong:下列关于向量和列表的说法错误的是:向量归并排序的时间复杂度是O(nlog2(n)),而列表为Ω(n2)
  2. In order to insert a new node in the list as a direct precursor to p, there are four related statements为了在列表中插入一个新节点node作为p的直接前驱,有四个相关的语句
    ①p->pred->succ = node
    ②node->pred = p->pred
    ③node->succ = p
    ④ p->pred = node
    The correct execution order of the above statement is: 上述语句执行顺序正确的是:③②①④ 【Correct execution of ① and ② needs to be able to locate p’s original direct precursor p->pred, and ④ will break p’s link to it, so ① and ② must be performed before ④. The process can be understood by drawing①和②的正确执行需要能够定位p原先的直接前驱p->pred,而④会破坏p到其的链接,故①和②必须在④之前执行。可以通过画图的方式理解该过程】
  3. For a bi-directional and unidirectional list of n nodes, the worst-case complexity of locating a node p’s direct precursor is:对于n个节点的双向、单向列表,定位某节点p直接前驱的最坏时间复杂度分别为:O(1),O(n) 【原因:For a two-way list, direct access to p->pred can locate its immediate precursor with only O(1) time. For a one-way list, locating its immediate predecessor requires visiting each node one by one from the first node. In the worst case, it needs to traverse the entire list, so it takes O(n) time.对于双向列表,直接访问p->pred即可定位其直接前驱,只需O(1)的时间。对于单向列表,定位其直接前驱需要从首节点开始逐一访问各节点,最坏情况下需要遍历整个列表,故花费O(n)的时间】
  4. The time complexity of finding an element in an ordered list is在有序列表中查找一个元素的时间复杂度是:Ω(n)
  5. Selecting the list {11, 5, 7, 13, 2, 3}. Each time the elements are selected as the largest element in the unsorted sublist in selectMax()对列表{11, 5, 7, 13, 2, 3}进行选择排序,每一次selectMax()被选为未排序子列表中最大者的元素依次为:13, 11, 7, 5, 3
  6. Which implementation of the selectSort() algorithm is stableselectSort()算法的哪种实现是稳定的:Each time moves the smallest element to the front. For multiple equal minimum elements, select the one with the smallest position.每一趟将最小元素移到前方,对于多个相等的最小元素,选取其中位置最靠前者。
  7. For ordered subsequences (set their length to k) during insertion sorting.对于插入排序过程中的已排序子序列(设其长度为k):The elements are the k elements at the front of the original sequence.其中的元素是原序列中位于前方的k个元素
  8. The sequence {2, 7, 13, 5, 3, 19, 17} is obtained after one insertion in the insertion order, and the sorted portion has 3 elements. After 2 iterations, the result is插入排序中的某一次插入后得到序列{2, 7, 13, 5, 3, 19, 17},此时已排序部分有3个元素。又经过2趟迭代后的结果是:{2, 3, 5, 7, 13, 19, 17}
  9. The reverse number of a sequence τ is defined as the total number of reversed pairs in the sequence, and the total number of element comparisons performed by the insertion sort in the list of size n is:一个序列的逆序数τ定义为该序列中的逆序对总数,规模为n的列表中插入排序进行的元素比较总次数为: O(n+τ)
  10. A list of length n is equally divided into n/k segments. Each segment has a length of k. There is no reverse order for elements between different segments. The worst-case complexity for insertion sort on this list is:长度为n的列表,被等分为n/k段,每段长度为k,不同段之间的元素不存在逆序。对该列表进行插入排序的最坏时间复杂度为:O(nk) 【Sorting each segment can actually be considered as separate. Sorting algorithms in some libraries (such as sort in the STL) will use the other sorting algorithms to achieve the above segmentation effect, and then call the entire sequence to insert the sort, in order to obtain a higher practical efficiency.对每段的排序实际上可以视为分别进行。在某些库中的排序算法(比如STL中的sort)会先用其它的排序算法达到以上分段的效果,再对整个序列调用插入排序,以期获得较高的实际效率。】
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊有礼貌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值