知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

  • 练习题

    • 选择题 & 填空题

      • What does the method print
      • Appending strings and ints
      • Varargs method invocation
      • Good reasons to use Java modules
      • Number generators
      • When the keyword cannot be used
      • Size and capacity
      • The number of copies
        • Topic
        • Problem
        • Hint & Explain
        • Diagrammatize
      • The number of insertions
    • 代码题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • 练习题
  • 选择题 & 填空题
Jim
2022-09-24
目录

The number of copies

# Topic

Dynamic array

# Problem

Assume that for a dynamic array of size 22 and capacity 44 we perform the following sequence of operations:

add 5
add 7
add 9
insert 3 1
insert 6 2
1
2
3
4
5

提示

Here, add x*x* means that we append an element to the array, insert x*x* ii means that we insert xx to the array at the ii-th index (assuming zero-based indexing).

How many copy operations of an individual array element will we perform when doing the required operations with the array?

Note that elements are copied in the following cases:

  • When the array capacity increases, all elements from the old array are copied.
  • When we insert an element into the middle of the array. In this case, all elements from the right half of the array are copied after the inserted element, starting at the index of the inserted element.

Write down the total number of times any individual element will be copied assuming that the scaling factor is 2.

Enter a number:

12

Correct.

# Hint & Explain

Saying our dynamic array is initially: | * | * | | |

  • adding 5 and 7: | * | * | add(5) | add(7) |
  • a resize while adding 9, will copy 4 elements | copy () | copy () | copy (5) | copy (7) | add(9) | | | |
  • the first insertion moves all elements that will follow 3 after inserting it at index 1 ( that means 4 copies starting from the old index 1 before insertion) | * | insert (3) | copy (*) | copy (5) | copy (7) | copy (9) | | |
  • then the same with the second insertion, moves all elements that will follow 6 after inserting it at index 2 (that means 4 copies starting from the old index 2 before insertion) | * | 3 | insert (6) | copy (*) | copy (5) | copy (7) | copy (9) | |

# Diagrammatize

image-20220925000604051

编辑 (opens new window)
#Problem#Data structure
上次更新: 2022/09/25, 10:41:23
Size and capacity
The number of insertions

← Size and capacity The number of insertions→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式