{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Homework 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write your name: [Your Name Here]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Recursive Insertion Sort" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also think of insertion sort as a recursive algorithm (`recInsertionSort`). In order to sort $A[0:n-1]$:\n", "- recursively sort the subarray $A[0:n-2]$ and then \n", "- insert $A[n-1]$ into the sorted subarray $A[0:n-2]$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1 (5 Points)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a Python code for this recursive version (`recInsertionSort`) of insertion sort.\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def recInsertionSort(A):\n", " # implement me\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2 (3 Points)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Give a recurrence equation for its worst-case running time $T(n)$. \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3 (2 Points)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compare and contrast $T(n)$ with the best and worst-case running times of incremental `insertionSort`.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Divide-and-Conquer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 4 (3 Points)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let $A[0:n-1]$ be an array of $n$ numbers. If $iA[j]$, then the \n", "pair $(i,j)$ is called an **inversion** of $A$.\n", "\n", "Design an $\\mathcal O(n\\lg{n})$ algorithm that determines the number of inversions in any permutation of $n$ elements. Provide a Python implementation.\n", "\n", "**Hint:** Modify `MERGE-SORT`.\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def countInversions(A):\n", " # implement me\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 5 (2 Points)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Analyze the time complexity of your algorithm in Exercise $4$.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 6 (3 Points)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Design an $\\mathcal{O}(n\\lg{n})$ algorithm that, given a set $S$ of $n$ integers and another integer $x$, determines whether $S$ contains two elements that sum to exactly $x$. Provide a Python implementation.\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def findPairWithSum(S, x):\n", " # implement me\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 7 (2 Points)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Analyze the time complexity of your algorithm in Exercise $6$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Time-complexity of Recursive Algorithms" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 8 (1 Point)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the Master Theorem to get an upper bound (in big-$\\mathcal O$ notation) for the following recurrence:\n", "$$T(n)=T(n/2) + n^3.$$\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 9 (1 Point)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the Master Theorem to get an upper bound (in big-$\\mathcal O$ notation) for the following recurrence:\n", "$$T(n)=4T(n/2) + n.$$\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 10 (3 Points)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given the recurrence relation $T(n)=2T(n/2)+\\mathcal O(n)$, what is the time complexity of the corresponding recursive algorithm?\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 11 (🏅 Bonus)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How does recursion impact space complexity, and what factors should be considered when analyzing it?" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 2 }