You are on page 1of 4

Ans: 1

First, we define the Insertion Sort algorithm in pseudocode:

Insertion Sort(A):

for i = 2 to n do

key = A[i]

j=i-1

while j > 0 and A[j] < key do

A[j+1] = A[j]

j=j-1

A[j+1] = key

Here, A is the input array of integers, n is the length of the array, key is the current value being sorted, and j is
a variable used for iteration.

Next, we can use the above pseudocode to implement Insertion Sort in Python to sort an array of integers in
descending order:

import random

def insertion_sort_descending(A):

n = len(A)

for i in range (1, n):

key = A[i]

j=i-1

while j >= 0 and A[j] < key:

A[j+1] = A[j]

j=j-1

A[j+1] = key

# Generate a random array of 10 integers between 1 and 100

arr = [random.randint(1, 100) for _ in range (10)]

print ("Initial array: ", arr)

# Perform insertion sort in descending order

insertion_sort_descending(arr)
# Print the sorted array

print("Sorted array: ", arr)

In this implementation, we first define a function insertion_sort_descending that takes an array A as


input and performs Insertion Sort on the array in descending order. Inside the function, we initialize the length
of the array n, and then use a for loop to iterate over each element of the array starting from the second
element. We initialize the key variable to be the current element being sorted and the j variable to be the
index of the previous element. We then use a while loop to iterate over the previous elements of the array
and move them to the right if they are less than the key. Finally, we insert the key into the correct position in
the sorted array.

We then generate a random array of 10 integers between 1 and 100, and print the initial array. We then call
the insertion_sort_descending function to perform the sort, and print the sorted array.

Here's the output for each step of the Insertion Sort algorithm:

Initial array: [98, 75, 57, 13, 71, 37, 26, 84, 96, 54]

Step 1: [98, 75, 57, 13, 71, 37, 26, 84, 96, 54]

Step 2: [98, 75, 57, 13, 71, 37, 26, 84, 96, 54]

Step 3: [98, 75, 57, 13, 71, 37, 26, 84, 96, 54]

Step 4: [98, 75, 71, 57, 13, 37, 26, 84, 96, 54]

Step 5: [98, 84, 75, 71, 57, 13, 37, 26, 96, 54]

Step 6: [98, 96, 84, 75, 71, 57, 13, 37, 26, 54]

Ans: 2
Insertion Sort is an iterative sorting algorithm that operates by iteratively moving elements
in an unsorted array to their correct positions in a sorted array. It works by dividing the
input array into two parts: a sorted part and an unsorted part. Initially, the sorted part
contains only the first element of the input array, and the unsorted part contains the
remaining elements. At each iteration, the first element in the unsorted part is removed and
inserted into its correct position in the sorted part. This process continues until the entire
array is sorted.
The time complexity of Insertion Sort depends on the size of the input array and the degree
to which the input array is already sorted. Specifically, the time complexity can be analysed
in terms of the number of comparisons and the number of swaps that the algorithm
performs.

Worst-case time complexity: The worst-case time complexity of Insertion Sort occurs when
the input array is in reverse order, and the sorted part of the array is empty at the beginning
of the sorting process. In this case, each element in the unsorted part of the array needs to
be compared to each element in the sorted part, and potentially swapped with each
element, in order to be inserted into its correct position. The total number of comparisons
and swaps is given by the sum of the first n-1 positive integers, where n is the length of the
array. Thus, the worst-case time complexity of Insertion Sort is O(n^2), where n is the length
of the array.
Best-case time complexity: The best-case time complexity of Insertion Sort occurs when the
input array is already sorted. In this case, the sorted part of the array is the entire input
array, and the unsorted part is empty. In each iteration, the algorithm only needs to
compare the current element to the last element in the sorted part, and if the current
element is smaller than the last element, it will be inserted at the end of the sorted part.
The total number of comparisons and swaps is n-1, where n is the length of the array. Thus,
the best-case time complexity of Insertion Sort is O(n), where n is the length of the array.
In practice, Insertion Sort is generally not as efficient as other sorting algorithms such as
Merge Sort and Quick Sort. However, it can be useful for small input sizes or partially sorted
arrays, where the worst-case time complexity is less likely to occur.

Ans: 3
The Tower of Hanoi algorithm is a recursive algorithm that is used to solve the Tower of Hanoi problem. The
problem consists of three pegs and a set of disks of different sizes, which can slide onto any peg. The problem
is to move the entire stack of disks from one peg to another, with the following constraints:

1. Only one disk can be moved at a time.


2. Each move consists of taking the upper disk from one of the stacks and placing it on
top of another stack or an empty peg.
3. No disk may be placed on top of a smaller disk.
The Tower of Hanoi algorithm can be implemented recursively, as follows:

TOH(n, from, to, aux):


if n == 1:
move disk from 'from' to 'to'
else:
TOH(n-1, from, aux, to)
move disk from 'from' to 'to'

TOH(n-1, aux, to, from)


Here, n is the number of disks, from is the peg the disks start on, to is the peg the disks end on, and aux is the
auxiliary peg used for transferring the disks. The algorithm works as follows:

4. If there is only one disk, move it from the from peg to the to peg.
5. Otherwise, recursively move the top n-1 disks from the from peg to the aux peg using the to peg as
the auxiliary peg.
6. Move the bottom disk from the from peg to the to peg.
7. Recursively move the n-1 disks from the aux peg to the to peg using the from peg as the auxiliary
peg.

To calculate the time complexity of the Tower of Hanoi algorithm, we can use the following steps:

8. We can analyse the time complexity of the algorithm by considering the number of moves required to
solve the problem. Since each move takes constant time, the time complexity of the algorithm is
proportional to the number of moves required to solve the problem.
9. For the Tower of Hanoi problem with n disks, the number of moves required can be expressed as 2^n
- 1. This can be shown by the following recursive formula:

T(n) = T(n-1) + 1 + T(n-1)

T (1) = 1

Here, T(n) is the number of moves required to move n disks from one peg to another. The
first line of the formula represents the number of moves required to move the top n-1 disks
from the from peg to the aux peg using the to peg as the auxiliary peg, plus the one move
required to move the bottom disk from the from peg to the to peg, plus the number of
moves required to move the n-1 disks from the aux peg to the to peg using the from peg
as the auxiliary peg. The second line represents the base case, where only one disk is being
moved.

10. We can solve the recursive formula using mathematical induction. For the base case,
T (1) = 1, which is true. For the inductive step, assume that T(n-1) = 2^(n-1)
- 1. Then we have:
T(n) = T(n-1) + 1 + T(n-1)

= 2*T(n-1) + 1

= 2*(2^(n-1) - 1

You might also like