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

  • Thread starter Thread starter darkvalentine
  • Start date Start date
  • Tags Tags
    Complexity Time
Click For Summary

Homework Help Overview

The discussion revolves around determining the time complexity of a given sorting algorithm applied to an array, specifically focusing on the number of comparisons and swaps involved in the process.

Discussion Character

  • Exploratory, Mathematical reasoning

Approaches and Questions Raised

  • Participants are analyzing the structure of the algorithm, particularly the nested loops, to deduce the time complexity. Some express uncertainty about applying big O notation to this scenario.

Discussion Status

Several participants suggest that the time complexity is likely O(n^2) based on their observations of the nested loops and the number of iterations each loop performs. There is a consensus among some that both loops contribute to this complexity, although the reasoning varies slightly.

Contextual Notes

Participants mention recent study of big O notation, indicating a potential gap in understanding how to apply it effectively to this problem.

darkvalentine
Messages
11
Reaction score
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
It looks to me like n^2 because of the nested loops.
 
╔(σ_σ)╝ 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.
 
Yes n^2. The inner loop has a worst case of n and the outter loop also runs n times.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
3
Views
2K
Replies
9
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K