Selection Sort
Randomly given elements:
for (i = 0 to n):
min = i
for (j = i+1 to n):
if (A[j] < A[minimum]):
minimum = j
Swap(A[min], A[i])Example: [75, 36, 4, 9, 81, 65]
Swap function:
S, temp = a
a = b
b = tempComplexity:
- Best case:
- Average:
- Iterations:
times
Step-by-step Execution
75 36 4 9 81 65
4 36 75 9 81 65
4 9 75 36 81 65
4 9 36 75 81 65
Iterations:
Total comparisons:
Time Complexity:
Bubble Sort
Example: [75, 36, 4, 9, 81, 65]
for j = (0 to n):
for (i = 0 to n-j-1):
if (A[i] > A[i+1]):
Swap(A[i], A[i+1])Execution
36 4 9 75 65 81 → Biggest at right-most
Analysis:
Time Complexity:
Best case: