From charlesreid1

Revision as of 07:31, 28 May 2017 by Admin (talk | contribs) (Created page with "==Analysis of Selection Sort== Consider the following selection sort algorithm: <pre> selection_sort(int s[], int n) { int i, j; // counters int min; // index of min...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.

Analysis of Insertion Sort