Unit 2 - R23
Unit 2 - R23
Algorithms
UNIT 2
Selection Sort
• Iterate through the list, finding the
smallest among the remaining
elements and placing it in its final
position.
● While the number of key comparisons is ● The algorithm doesn't exploit any
O(n2), the number of key swaps is O(n), specific patterns in the data; instead, it
making it more efficient in this aspect just systematically selects the smallest
compared to some other sorting element and places it in its correct
algorithms. position.
● The space complexity of the selection ● While not the most efficient sorting
sort algorithm is O(1), commonly algorithm, selection sort serves as a
referred to as constant space. basic illustration of the brute-force
Regardless of the size of the input array, approach to problem-solving.
the additional memory used by the
algorithm remains constant.
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept
Basic Idea:
• Compare adjacent elements of the list.
Bubble Sort •
•
If they are out of order, swap them.
Repeat this process until the entire list
is sorted.
=
Example: Illustration using the list
https://visualgo.net/en/sorting • The algorithm grows quadratically
with the size of the input.
for i ← 0 to n - m do
j←0
while j < m and P[j] = T[i + j] do
j←j+1
if j = m return i # i returns index of starting
position
return -1
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept
● Algorithm Overview:
● The input size can be expressed as a ● In this case, the algorithm only needs to
combination of both the text length and perform a single comparison between
the pattern length, such as (n, m) or (10, each character of the pattern and the
3). corresponding character in the text.
As the text and pattern lengths increase, the ● Therefore, the best-case time complexity
input size increases accordingly. is O(m), where m is the length of the
pattern.
The number of comparisons made by the
algorithm grows linearly with the size of the
input.
Salesman Problem
objective is to find the shortest possible
tour that visits each city exactly once and
returns to the starting city.
• Decrease-by-a-Constant: In this
variation, the size of the problem instance Example: Decrease-by-a-Constant
is reduced by the same constant on each (Decrease-by-One):
iteration of the algorithm. Typically, this
constant is equal to one. Example: Factorial Calculation
Factorial of a number n is defined as: n!
• Decrease-by-a-Constant-Factor: Here, =n×(n−1)!
the problem instance is reduced by the • At each step, the problem size is reduced
same constant factor on each iteration. by 1.
Typically, this factor is equal to two. • If n=5, we compute 5! by calling 4!, then
3!, etc., until we reach the base case.
• Variable-Size Decrease: In this variety,
the size-reduction pattern varies from one
iteration of the algorithm to another.
// Insert key into its correct position ● Therefore, we use the Θ notation to
A[j + 1] ← key express the precise growth rate of the
algorithm's time complexity as n2.
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept
Algorithms for Generating Combinatorial Objects
4. Swapping Elements:
1. Swap the element k with the adjacent
element to which its arrow points.
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept
5. Reversing Direction: ● Let's illustrate these steps with an
1. Reverse the direction of the example for n = 3:
arrows for all elements greater ● Initial permutation: [1, 2, 3]
than k.
2,3 are mobile so choose the
6. Generating Permutations: ● 1 2 3 greater
2. Add the new permutation to the 2,3 are swapped & 3 is mobile
● points to 1
list of permutations generated. 1 3 2
3 ,1 are swapped and 2 is
mobile points to 1 so swap 2,1
7. Repeat: ● 3 1 2
2 is mobile but 3 is greater so
3. Repeat steps 2 to 6 until there reverse direction of 3 & 3
are no more mobile elements in ● 3 2 1 point to 2 so swap
3 is mobile & pointing to 1 so
the permutation. swap
● 2 3 1
No more mobile elements so
stop
● 2 1 3
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept
● Start with an empty set, which
represents the subset with no
elements.
Prepared by M.V.Bhuvaneswari, Asst.Prof, CSE (AI&ML,DS) Dept 1 is the termination condition, so we add 2080
Here's a formal statement of the
problem:
K=2
2 3
6
5
3
5
1
K=2
4
1. After the first pass, every second person • Interestingly, we can represent n in
is eliminated, leaving k survivors.
binary form.
Additionally, the person in the first
position is eliminated.
• The survivor's position J(n) is obtained
2. The survivor's position among the by cyclically shifting the binary
remaining k people will be denoted by representation of n one bit to the left.
J(k).
• For example, if n=6, represented in
3. To find the survivor's position among the
binary as 110, the survivor's position
original 2k+1 people, we double J(k) and
add 1, since the position numbering is 101, which is 5.
changes after the first pass and we
eliminate the person in the first position. • Similarly, if n=7, represented in binary
as 111, the survivor's position is 111,
4. So, for odd n, we have the recurrence which is 7.
relation: J(2k+1)=2J(k)+1
Since Num sum is Non-zero player Since the Num sum is zero second
who makes first move wins player wins