What is the Time Complexity of this Sorting Algorithm on an Array?

Overall, the time complexity is n^2.In summary, the time complexity of the given sorting algorithm on an array of size n is n^2, as there are 2 nested loops, each running n times. This can be determined using the big O notation.
  • #1
darkvalentine
12
0

Homework Statement



Compute the time complexity of the following sorting algorithm on an array L[0..n-1] in terms of n. Basic Operation only includes comparison and swap.
sort (L, n)
{
int i=0, j;
while(i<n-1){
s = i ;
j=i+1;
while(j<n){
if (L[j]<L) s = j;
j++;
}
swap (L,L);
i++;
}
}


Homework Equations



N/A

The Attempt at a Solution


Well, we have just studied about the big O notation last week, but I have no idea how we apply it to solve this kind of problem.
 
Physics news on Phys.org
  • #2
It looks to me like n^2 because of the nested loops.
 
  • #3
╔(σ_σ)╝ said:
It looks to me like n^2 because of the nested loops.

Thank you, I think it x2 because of 2 loops, and each loop is executed n times.
 
  • #4
Yes n^2. The inner loop has a worst case of n and the outter loop also runs n times.
 

Related to What is the Time Complexity of this Sorting Algorithm on an Array?

What is time complexity in computer science?

Time complexity in computer science refers to the analysis and measurement of the amount of time it takes for a computer algorithm to run as a function of the input size. It is used to evaluate the efficiency and performance of algorithms.

Why is time complexity important?

Time complexity is important because it helps us understand the performance and scalability of algorithms. It allows us to compare different algorithms and choose the most efficient one for a given problem or input size. It also helps in predicting the time it will take for an algorithm to run on larger input sizes, which is crucial for real-world applications.

How is time complexity measured?

Time complexity is measured using the Big O notation, which represents the upper bound on the time it takes for an algorithm to run as a function of the input size. It ignores constant factors and lower order terms, providing a general idea of the growth rate of the algorithm.

What is the worst case time complexity?

The worst case time complexity represents the maximum amount of time an algorithm will take to run for a given input size. It is denoted by the Big O notation and is used to evaluate the worst possible performance of an algorithm. It is important to consider the worst case scenario when analyzing the efficiency of an algorithm.

What are some common time complexities?

Some common time complexities include O(1) or constant time, O(log n) or logarithmic time, O(n) or linear time, O(n^2) or quadratic time, O(n log n) or linearithmic time, O(n^3) or cubic time, and O(2^n) or exponential time. These complexities represent different growth rates of algorithms and can be used to compare and analyze their efficiency.

Similar threads

  • Calculus and Beyond Homework Help
Replies
0
Views
481
  • Calculus and Beyond Homework Help
Replies
12
Views
1K
  • Calculus and Beyond Homework Help
Replies
3
Views
1K
  • Calculus and Beyond Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
616
Replies
4
Views
815
Replies
1
Views
861
  • Calculus and Beyond Homework Help
Replies
3
Views
998
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top