>Based on the O notation, approximately how many swaps and
>comparisons occur when Selection sort is called on a worst-case array of length 8?
That's very poorly worded. Also, there's no such thing as a worst case for selection sort. The best, average, and worst cases are all identical.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Run the algorithms out by hand in the worst case scenario (you have to figure this out), and while doing so, count the number of swaps and comparisons made. This will be your answer. Troublingly, merge sort would not be implemented with any swapping... and selection sort with 8 elements has a worst case of 28 comparisons, 28 swaps, assuming you move values over by swapping successive pairs of values. (But if moving values is implemented more efficiently, you'll have only 1 instance of what could be called a 'swap'.) Now, 28+1 is closer to the answer 16 than 64, but I'm sure whoever asked this question thinks that '64' is the correct answer.
In conclusion, whoever wrote these questions is nearly an absolute idiot. They want you to go from O(N log N) to 8*log8 = 24? That's lunacy, and that number has nothing to do with the actual number of swaps made. For example, the exact answer to your second problem is 17 comparisons. (Merge sort algorithms generally do not implement any swapping.) I don't blame you for being confused; the questions are written by somebody who does not understand big O notation.
Big O notation says that a value will be no more than a certain _multiple_ of some expression. That multiple could be any positive number imaginable, so big O notation can't be used to get approximations to actual, numerical answers.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
Another way of putting it is that the symbol O(N log N) has exactly the same meaning as O(50N log N). So would the expected answer be 24, or would it be 1200? This is why coming up with any value by plugging numbers into O notation expressions is completely meaningless.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
Why do you care about the answer? The questions themselves are wrong.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
You've replied to a thread that is 4 years old. I doubt the OP is still expecting an answer and will read your post.
Start a new thread for your top, so it will get noticed, and get some answers. A comparison is made whenever you compare the value of one number in the array, with the value of another number in the array.
Can't really say it any clearer than that. ;)
Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185