MA214 Algorithms and Data Structures 2023/24 Exercises 5Python

Java Python MA214 Algorithms and Data Structures

2023/24

Exercises 5

(Quick Sort, decision trees, Counting Sort)

Exercise 5.1.  Quick Sort                                                                                            4 pts

(a) Give an example showing that the implementation of the Quick Sort algorithm discussed in the lecture is not stable.

(b) Suppose you can use additional space.  Explain how the Partition procedure can be modified so that Quick Sort becomes stable.

(c) Implement the proposed Partition procedure in Python.

(d) One disadvantage of the (non-randomized) Quick Sort algorithm discussed in the lecture is that it takes worst-case time Θ(n2 ), even on inputs that are already sorted.  One attempt to fix this is the “median-of-three rule.”  This rule looks at the first, the middle, and the last entry and takes the median of the three as a pivot. Implement this modified partition procedure in Python.

(e) What is the worst-case running time of your algorithm from (d)?

Exercise 5.2.  Decision tree for Quick Sort            &nb MA214 Algorithms and Data Structures 2023/24 Exercises 5Python sp;                                                     4 pts

Consider the variant of Quick Sort that we discussed in the lecture, in which the Parti- tion procedure picks the last element as pivot.

(a) Draw the decision tree for inputs A of length |A| = 3.

(b) Overall inputs A of length |A| = 3, what is the maximum number of comparisons Quick Sort needs to carry out to determine the output?

(c) Give an example input that achieves that maximum number.

(d) How many reachable leaves are in the decision tree for Quick Sort on inputs A of length |A| = 3?

(e) For each reachable leaf, give an example input that leads to that leaf.

Exercise 5.3.  Counting Sort and Radix Sort                                                               2 pts

(a) In the Counting Sort algorithm that we saw in the lectures, inStep 3 the elements of the list A are traversed in reversed order. Consider a variant in which the elements of A are traversed from beginning to end instead (and everything else remains the same). Show that this variant is not stable.

(b) Assume the Counting Sort variant from (a) is used in the stages of Radix Sort. Show that the resulting algorithm is not correct         

  • 19
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
数据结构和算法是计算机科学中非常重要的概念和工具。它们用来解决各种计算问题,并帮助我们设计高效的程序。 在Python中,有许多常用的数据结构和算法可用于处理和操作数据。以下是一些常见的数据结构和算法: 1. 数组(Array):数组是一种存储固定大小元素的数据结构。在Python中,我们可以使用列表(List)来表示数组。 2. 链表(Linked List):链表是一种动态数据结构,它通过节点(Node)的方式来存储和访问数据。在Python中,我们可以使用列表(List)或自定义类来实现链表。 3. 栈(Stack)和队列(Queue):栈和队列都是一种特殊的数据结构,用来存储和访问数据。在Python中,我们可以使用列表(List)来实现栈和队列。 4. 树(Tree)和图(Graph):树和图是一种常见的非线性数据结构,用来表示层次关系和连接关系。在Python中,我们可以使用自定义类来实现树和图。 5. 排序算法:排序算法是一种将数据按照特定顺序进行排序的算法。在Python中,我们可以使用内置的sorted()函数来进行排序,或者实现一些常见的排序算法,如冒泡排序、插入排序、选择排序、快速排序等。 6. 查找算法:查找算法是一种在数据集中查找指定元素的算法。在Python中,我们可以使用内置的in关键字来进行简单的查找,或者实现一些高级的查找算法,如二分查找、哈希表等。 除了上述提到的数据结构和算法外,Python还提供了许多其他实用的数据结构和算法,如堆(Heap)、哈希表(Hash Table)、图算法(Graph Algorithms)等,可以根据具体问题选择合适的工具。 总而言之,了解和掌握数据结构和算法对于Python开发者来说是非常重要的,它们可以帮助我们提高程序效率、解决复杂的计算问题,并在面试和竞赛中展示出色的编程能力。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值