I am so confused on how to tell a run time of a algorithm. so i came up with some ex for me to understand. could any one tell me if i am find the run time right plz.
ex1 - run time for this loop is O(n)? bc it going to loop through n times.
for(i = 0; i < n; i++)
{
//do some thing
}
ex2 - O(n-1)? bc it going to loop through n-1 times.
for(i = 0; i < n-1; i++)
{
//do some thing
}
ex3 - O((n)(n)) bc n times n
for(i = 0; i < n; i++)
{
for(x = 0; x < n; i++)
{
//do some thing
}
}
ex4 - to find value from sorted array is O(n) bc it would run n times.
int a[n] = {1,2,3,4};
int x = 2;
for(i = 0; i < n; i++)
{
if(a[i] = x)
{
//do some thing
}
}
ex5 - to find value from UNSORTED array also is O(n) bc it would run n times.
int a[n] = {4,3,1,2};
int x = 2;
for(i = 0; i < n; i++)
{
if(a[i] = x)
{
//do some thing
}
}