Lecture3
Lecture3
Dr. V.A.Kandappan
Assistant Professor,
Department of Computer Science,
Shiv Nadar University Chennai.
??
1 For an array with n elements, How many permutations/reorders are possible?
2 What is the time complexity to establish that a given array is sorted?
Algorithm isSorted(A)
Require: An array of numbers: A sizeof(A) is n
Ensure: True if the array is sorted in ascending order, False otherwise.
1: procedure isSorted(A)
2: for i ← 0 to len(A) − 2 do ▷ Iteration costs O (n) comparisons
3: if A[i] > A[i + 1] then
4: return False
5: end if
6: end for
7: return True
8: end procedure
P1 : 9 7 1
P2 : 1 9 7
P3 : 9 1 7
P4 : 7 9 1
P5 : 1 7 9
P6 : 7 1 9
P1 : 9 7 1
P2 : 1 9 7
P3 : 9 1 7
P4 : 7 9 1
P5 : 1 7 9
P6 : 7 1 9
Algorithm 1. permutationSort(allPermutations)
Require: A list of all permutations: allPermutations
Ensure: The sorted list if found
1: procedure permutationSort(allPermutations)
2: for perm in allPermutations do
3: if isSorted(perm) then
4: return perm ▷ Return the first sorted permutation found
5: end if
6: end for
7: return None ▷ No sorted permutation found
8: end procedure
The computational Complexity of the above algorithm is O (n!) and the computational
complexity of naive permutation sort is O (n × n!), where n is the size of the array.
??
Given a list of n element how many steps does it take to sort the elements
in the list.
• Sorted List: 11, 12, 22, 25, 34, 64, 90
Computational Complexity
The number of comparisons performed in this algorithm is O n2 .
3, 1, 2
3 1, 2
1 2
1, 2
1, 2, 3
Kandappan (SNU Chennai) CS2004 DAA January 6, 2025 10 / 14
Decision Tree
Consider c1 , c2 , c3 be the elements of an array that are unique. Then the
decision tree model for any sorting algorithm that uses only comparisons
looks like below.
?c1 < c2
L = log2 (n!)
Stirling’s Approximation
√ n n
n! ≈ 2πn
e
Buckets Values
0 170,90
2 802,2
4 24
5 45,75
6 66
Buckets Values
0 802,2
2 24
4 45
6 66
7 170,75
9 90