Algorithm
Algorithm
Explanation:
• Insertion Sort:
o Best case is O(n) when the array is already sorted.
o Efficient for small or nearly sorted data.
o Performs fewer comparisons/swaps than Bubble Sort in general.
• Bubble Sort:
o Also O(n) in best case (with optimization – early exit if no swaps).
o Performs a lot of unnecessary swaps – usually slower in practice.
• Selection Sort:
o Always O(n²), regardless of input.
o Finds the minimum each time and swaps once per pass – fewest
swaps among the three.
o Not stable, unless specifically modified.
Summary:
• Insertion Sort is generally the most efficient of the three for small or
nearly sorted datasets.
• Bubble Sort is simple but rarely used in practice due to inefficiency.
• Selection Sort is predictable but lacks adaptability and stability.
Given an array apply radix sort. (No need for algorithm).
Write the steps.
Sure! Here's how Radix Sort works step-by-step on an example array. Let's say
we are given this array:
[170, 45, 75, 90, 802, 24, 2, 66]
We will sort the numbers digit by digit starting from the least significant digit
(LSD).
Quick Sort & Merge Sort. Write the algorithm. Explain with
example the execution. Analyze (derive) the best, average
and worst time complexity.
Quick sort algorithm :-
QUICKSORT(arr, low, high):
if low < high:
pi = PARTITION(arr, low, high)
QUICKSORT(arr, low, pi - 1)
QUICKSORT(arr, pi + 1, high)
4-Queen Problem
Write all possible optimum solutions
Total Solutions:
There are 2 unique solutions (if you ignore symmetrical
rotations/reflections),
but 8 total solutions when you count all symmetrical versions.
Here are all 8 valid board configurations where Q represents a queen:
♟ Solution 1
.Q..
...Q
Q...
..Q.
Positions: [1, 3, 0, 2]
♟ Solution 2
..Q.
Q...
...Q
.Q..
Positions: [2, 0, 3, 1]
♟ Solution 3
..Q.
.Q..
...Q
Q...
Positions: [2, 1, 3, 0]
♟ Solution 4
.Q..
...Q
..Q.
Q...
Positions: [1, 3, 2, 0]
♟ Solution 5
Q...
..Q.
.Q..
...Q
Positions: [0, 2, 1, 3]
♟ Solution 6
...Q
.Q..
Q...
..Q.
Positions: [3, 1, 0, 2]
♟ Solution 7
Q...
...Q
.Q..
..Q.
Positions: [0, 3, 1, 2]
♟ Solution 8
..Q.
Q...
...Q
.Q..
Positions: [2, 0, 3, 1] (same as Solution 2 — but possibly rotated)