Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of comparisons in Straight Selection sort

I found this question in a Pearson book.

How many comparisons are needed to sort an array of length 5 
(whose element are already in opposite order) using straight selection sort?
a. 5
b. 20
c. 4
d. 10

It is given that the correct answer is b. 20

But I think it would be d. 10

Please explain how the answer is 20.

I also Googled for straight selection sort but I am getting only results for Selection sort. I also found the term exchange selection sort. Till now, I have only heard about Selection sort only, so please give some views on the difference between Exchange and Straight selection sort.

Thanks in advance.

like image 721
Utkal Sinha Avatar asked Jan 10 '14 12:01

Utkal Sinha


People also ask

How does selection sort differ from other sorting algorithms?

One thing which distinguishes selection sort from other sorting algorithms is that it makes the minimum possible number of swaps, n − 1 in the worst case. Here is an example of this sort algorithm sorting five elements: Selection sort animation.

What is the difference between straight and exchange selection sort?

The difference between what I'd assume are straight and exchange selection sort don't affect the number of comparisons. It would make sense that exchange exchanges (swaps) elements and straight shifts elements up (or uses a data structure that doesn't require shifting up, such as a linked list).

What is the time complexity of selection sort?

(May 2019) In computer science, selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n 2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort.

What is the worst case of selection sort?

The worst case is the case when the array is already sorted (with one swap) but the smallest element is the last element. For example, if the sorted number as a1, a2, ..., aN, then: a2, a3, ..., aN, a1 will be the worst case for our particular implementation of Selection Sort.


Video Answer


1 Answers

10 should be the correct answer.

From Wikipedia:

Selection sort is not difficult to analyze compared to other sorting algorithms since none of the loops depend on the data in the array. Selecting the lowest element requires scanning all n elements (this takes n − 1 comparisons) and then swapping it into the first position. Finding the next lowest element requires scanning the remaining n − 1 elements and so on, for (n − 1) + (n − 2) + ... + 2 + 1 = n(n − 1) / 2 ∈ Θ(n^2) comparisons.

So, for 5 elements, it'd be 5*4/2 = 20/2 = 10 (note "none of the loops depend on the data in the array", so the fact that it's in descending order doesn't play a role in the number of comparisons).

The difference between what I'd assume are straight and exchange selection sort don't affect the number of comparisons.

It would make sense that exchange exchanges (swaps) elements and straight shifts elements up (or uses a data structure that doesn't require shifting up, such as a linked list). Note that we only do comparisons to find the element we want, there are no (element) comparisons involved with either swapping, shifting or linked-list insertion.

Wikipedia uses exchanging as the default algorithm and lists the alternative under "Variants".

like image 119
Bernhard Barker Avatar answered Sep 25 '22 17:09

Bernhard Barker