1
import sys final_set = [] init_set = [] for i in range(1,len(sys.argv)): init_set.append(sys.argv[i]) for i in range(len(init_set)): cur_min = min(init_set) final_set.append(cur_min) init_set.remove(cur_min) print final_set 

This fairly basic sorting algorithm must already have a name. Can anyone identify it and its time complexity?

2
  • 1
    Note that those are lists, not sets. Commented May 27, 2012 at 5:42
  • Yeah, I wasn't really thinking when I named them. Commented May 27, 2012 at 5:46

1 Answer 1

5

This appears to be a Selection sort, which has quadratic complexity.

Sign up to request clarification or add additional context in comments.

6 Comments

On all counts of operations that I performed the operation count was less than n^2. Why is this? Is n^2/2 considered the same thing as n^2 in algorithmic complexity?
@user1419802 Checking the minimum has to go through each element.
Since one item of the list is removed each time the set becomes one smaller meaning that the first iteration takes n operations, the next takes n-1 operations and so on. That would presumably be less than n^2.
@user1419802 The mathematical definition of big-o complexity doesn't consider an exact count of operations, but rather how the number of operations grows. So, to estimate it empirically, you need to look at the operation count across several (preferably as many as practical) ns. But yes, an algorithm which always has n^2/2 operations is order n^2.
@user1419802 likewise, an algorithm that always has exactly twelve operations is called O(1) rather than O(12). Multiplying or dividing by a constant factor doesn't change the complexity (which is something you can prove if you decide to look at the math behind it), so we tend to just leave those out.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.