previous |
start |
next
Analyzing the Performance of Selection Sort Algorithm
- In an array of size n, count how many times an array
element is visited
- To find the smallest, visit n elements + 2 visits for
the swap
- To find the next smallest, visit (n-1) elements + 2
visits for the swap
- The last term is 2 elements visited to find the smallest + 2
visits for the swap
- n + 2 + (n-1) + 2 + (n-2) +2 + . . .+ 2 +
2
- This can be simplified to n2 /2 +
n/2 - 3
- n/2 - 3 is small compared to n2 /2 -
so let's ignore it
- also ignore the 1/2 - it divides out when comparing ratios
- The number of visits is of the order n2
- Using big-Oh notation: The number of visits is
O(n2)
- Multiplying the number of elements in an array by 2 multiplies the processing time by 4
previous |
start |
next