Algo Lec 4
Algo Lec 4
Step-by-Step Example
Input Array:
[64, 25, 12, 22, 11]
We want to sort this in ascending order.
22, 11].
The smallest element is 11.
64].
2 2
Design and Analysis of Algorithms 3
64].
The smallest element is 12.
64].
64].
3 3
Design and Analysis of Algorithms 4
25, 64].
Key Points
Selection sort always performs n-1
4 4
Design and Analysis of Algorithms 5
Algorithm:
selection_sort(arr):
n = len(arr)
for i to(n)
min_index = i
for j in range(i + 1, n):
if arr[j] < arr[min_index]:
min_index = j
5 5
Design and Analysis of Algorithms 6
Summary Table
Number of Time
Case Comparisons Complexity
Best Case 2n(n−1) O(n2)
Worst Case 2n(n−1) O(n2)
Average
Case 2n(n−1) O(n2)
7 7
Design and Analysis of Algorithms 8
Space Complexity
10 10