This is my code for a simple selection sort.usually the complexity (time) for a sort is number of iterations it has taken for sorting O(n^2) in case of selection sort
When I dry ran this code against sample string of 98765, it gave me 25 iterations.
Just to cross check with my dry ran output i put 2 vbl- noi and noj in my code.
Q: will the number of total iterations be = noi*noj or noi+noj;
int index = 0; int noi = 0, noj = 0;
for (j = 0; j < 5; j++)
{
noj++;
index = j;
for (i = j; i < 5; i++)
{
if (a[index] > a[i])
{
a[index] = a[index] + a[i];
a[i] = a[index] - a[i];
a[index] = a[index] - a[i];
noi++;
}
}
}
number of iterations is always 15 (5+4+3+2+1) because in your loops there are
j<5andi<5. So your code complexity is O(n^0) because in your case n is 5