Professional Documents
Culture Documents
Insertion Sort(A):
for i = 2 to n do
key = A[i]
j=i-1
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)
key = A[i]
j=i-1
A[j+1] = A[j]
j=j-1
A[j+1] = key
insertion_sort_descending(arr)
# Print 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:
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 (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