0

An algorithm to sort a list L1 of length n:

1) Create new List L2

2) Move biggest element of L1 to beginning of L2

3) Do 2) until L1 is empty

4) Print L2

Can someone figure out what the O() complexity of this is? First I thought it was O(n*log(n)) but I an not so sure anymore, now I think its O(n^2).

2
  • Why did you think it was O(n log n), and why do you now think it is O(n^2)? Commented Mar 5, 2020 at 22:54
  • Well actually initially looking at the problem I thought it was O(n^2) but I realized that L1 gets smaller as Elements are moved to L2 so then I did some research and thought it would be in O(n*log(n)) but after some simple testing in python I figured thats not right so I went back to O(n^2). Commented Mar 5, 2020 at 22:58

1 Answer 1

1

Let's say you have a list of 10 elements.

In step 2 you have to scan the whole list first to find the biggest element and move to it to list no 2.

So, scanning the whole list is (10 + 9 + 8 + ... + 1) operations = 55.

For 100 elements it will be (100 + 99 + ... + 1) which is 5050

Now for n elements, you will have (n + (n-1) + ... + 1) operations which is (n+1)*n/2 = O(n^2)

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

1 Comment

Ohh wow I was too stupid to see the sum from 1 to n because it was backwards... Thank you very much! :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.