Algorithmic Analysis of Sort Functions
From charlesreid1
Analysis of Selection Sort
Consider the following selection sort algorithm:
selection_sort(int s[], int n)
{
int i, j; // counters
int min; // index of minimum
for(i=0; i<n; i++) {
min = i;
for(j=i+1; j<n; j++)
if(s[j] < s[min]) min = j;
swap(&s[i], &s[min]);
}
}
performing the algorithmic analysis:
- for loop with i index operates O(n) times
- second for loop operates O(i) times, within the loop that runs n times, for an algorithmic complexity given below.
$ O(i) : \sum_{i=1}^{n} i = \dfrac{n(n+1)}{2} \sim O(n^2) $
Overall this algorithm is quadratic.