(08-23-2013, 09:45 AM)lady_godiva Wrote: That is true, but i also believe that this forum and this post should be an occasion to learn about it and understand it better as understanding how things work and reasons behind them should be one of the main purpose here.
You are right, but we need an entire tutorial to explain that. It would be out of topic for a tutorial about sorting algorithms to explain the whole concept.
I have it on my list to make one, but don't expect it soon. I have a huge todo-list.
(08-23-2013, 10:19 AM)Psycho_Coder Wrote: (08-23-2013, 07:03 AM)Ex094 Wrote: @Deque If I improve my code then how would I figure out whether it's optimized or not, I'm guessing the time complexity or something, Please do tell me about this
I think I have explained the time complexity of bubble sort in my earlier reply. However to find the complexity of any algorithm you need to know the Master Theorem.
No, you didn't explain time complexity per se. You explained, why bubblesort is O(n²) and that is just an example. It doesn't really answer the question how to apply this in general or what time complexity actually is.
The Master Theorem only helps finding the time complexity for
some recursive algorithms.
There is no single method to get the time complexity for every algorithm. In some cases you have to make mathematical proofs. This is not trivial.
(08-23-2013, 07:03 AM)Ex094 Wrote: @Deque If I improve my code then how would I figure out whether it's optimized or not, I'm guessing the time complexity or something, Please do tell me about this
The time complexity only tells you how much the time grows with growing input.
I.e. how much the sorting time grows if you increase the number of elements that are to be sorted.
A better time complexity does NOT mean that the algorithm is better or faster. Because constant time needed is ignored, it is often the case that algorithms with better time complexity have a huge constant overhead. Meaning, they don't grow that much with growing inputs, they are just slow with every input.
So in order to answer your question: You can always determine the efficiency of an algorithm based on sample input of a given size by testing (measuring the time needed). At least for the practical case (if you write a software) this is enough and the best you can do.
In sorting algorithms you can count the comparisons and swaps needed, which gives a relatively good measure too. If you change your algorithm so that less swaps and comparisons are done, you have probably made it better (this is the part that Psycho_Coder explained in detail with his post.) unless you introduced some functions that eat a lot of (constant) time.
Edit: I will publish a tutorial about performance measurement. It is an old paper I wrote two years ago.