Machine-translated by Google. Questions are authored in English.
Top

Discussion

The number of times swap function called for the selection sort on an array with N numbers is :

  • A.\(N^{2}\)
  • B.N log N
  • C.log N
  • D.N-1

Answer: D

Selection sort calls swap N-1 times.

Selection sort makes N-1 passes. In each pass it scans the unsorted part to find the smallest element and then swaps it into the first unsorted position, one swap per pass. After N-1 passes the last element is automatically in place. (An implementation that skips swapping an element with itself can do fewer, but the standard count is N-1.)

\(N^{2}\) is the order of the number of comparisons, about N(N-1)/2, not swaps. N log N is the cost of efficient sorts such as merge sort, and log N is far too few to move N elements into place.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.