6k views

Which one of the following in place sorting algorithms needs the minimum number of swaps?

1. Quick sort
2. Insertion sort
3. Selection sort
4. Heap sort
+4
Shouldn't this be Insertion Sort?

As in Insertion Sort, we "technically" don't swap, but shift elements to place the element under consideration in proper place
+1
does swap here means shifting the elements??
0
NU OF SWAP
IN BUBBLE SORT =O(n2)
in selection sort=n
in insertion sort =0
+5
In insertion sort (implementation like Cormen) we should use the term Movement or Shifting.
Number of swapping:   We cant say it is zero. Swappings are done indirectly. (Just check initially 2nd element is swapped with first, other swaps are done indirectly)

It should not be asked actually.  Because we do not swap anything directly.
Why it is asked in GATE again in ISRO?
I think if they are asking about swapping in insertion sort, then they are following that approach of insertion sort where swapping is done.
See this https://cs.stackexchange.com/questions/21455/how-can-i-quantify-the-number-of-swaps-required-for-insertion-sort
This approach where swapping is done is more popular as insertion sort.. See wiki.
In wiki, they also have written insertion sort with approach2(Cormen) & recursive one. They also have written 2nd approach is little bit faster. But they have analysed the complexity based on 1st approach..
+4

In case of Insertion sort ,

best case is O(n) swaps , when the array is already sorted .

worst case O(n2) swaps

source:wikipedia

0
@Bikram sir..  as per the 1st approach where swapping is done..  Best case O(n) swap.. Worst case O(n sq) swap.

As per 2nd approach (like Cormen)..  best case 0 movements.. Worst case n(n-1)/2 movements..   :)
0

@ahwan

in worst case :

n(n-1)/2 = (n2 - n) /2 which is nothing but O(n2)  :)

0
Swaps in approach 1 & movements in approach 2 in worst case are O(n sq)..  but in best case swaps for approach 1 is O(n) where as no movements are done in approach 2. By the way key replaces the number with itself.. But it is not movement I guess..
0
yes it is swap , replacing key is same as swapping .
0
@Ahwan

where do the difference between swaps and movements?
+4
@srestha
Swap: Exchange of values between two variables.
a,b
temp=a; a=b; b=temp;

Movement:
a,b,c,d
I want insert d between a and b....

key =d
d=c;
c=b;
b=key
Now it is a,d,b,c
Here I did something like movement.  You have seen such movements in insertion sort.
0
thank u:)
0
So which version of insertion sort do we have to consider :1st or 2nd (according to wiki) for exams ?
0
ISRO has a pattern of asking every ambiguous previous year GATE question lol
0
In place algo i.e space O(1) but for this quick sort not applicable as it takes O(n)

am i correct?plz suggest any1.
0
in insertion sort we do half exchanges, i think they are counted as swaps in this question.

Selection sort.

Because in selection the maximum swaps which can take place are $O(n)$

Because we pick up an element an find the minimum (in case of forward sorting) from the next index till the end of array and than perform the swap

Hence, $O(n)$ whereas in all other algos the swaps are greater ( considering Worst-Case scenario )
edited
0
Please compare number of swaps in selection sort with other sorting techniques given in above question.
+22

Number of swaps : Worst case scenario

1. Quick sort = O(n2)
2. Insertion sort = О(n2
3. Selection sort = O(n)
4. Heap sort = O(n logn)
+7

@Ahwan

In case of selection sort , number of swaps require :

Best case: No swaps required as all elements are properly arranged

Worst case: n-1 swaps required

Reference:

https://stackoverflow.com/questions/26688765/what-are-the-number-of-swaps-required-in-selection-sort-for-each-case

another source :

While selection sort is preferable to insertion sort in terms of number of writes

(Θ(n)  swaps versus Ο(n2) swaps),

That means selection sort has O(n) swaps while insertion sort have O(n2) swaps in worst case.

Reference :

https://en.wikipedia.org/wiki/Selection_sort#Comparison_to_other_sorting_algorithms

0
@bikram sir in selection sort, in best case also the number of swaps are O(n). In best case 1st smallest element will be A[1] itself and if we don't do any modification in the standard algorithm definition given in cormen then A[1] will be swapped with itself. Hence in each iteration element will be swapped with itself.
+1

@vineet

In Best case , number of swaps are O(n) or 0 swaps , it really depends on the implementation .

And swapping with itself at first position only count as 0 swaps , as all elements are properly arranged .

What i read is in Best case number of swaps is 0 but it depends how selection sort is done..

for this gate question , no doubt answer is selection sort ..

0
yes sir it depends on implementation but, if we don't modify i.e if we write this check-> if(min!=j) then swap(A[min],A[j]) then there will be no swaps in best case. but if don't check and directly swap then in best also there will be O(n) swaps. But what i think is that algorithm without check should be considered as standard algorithm because in cormen it is simply mentioned "find first smallest element and swap it with A[1]......." now if A[1] itself is first smallest then also there should be swap even though it is not necessary.
0
partition (arr[], low, high)
{
// pivot (Element to be placed at right position)
pivot = arr[high];

i = (low - 1)  // Index of smaller element

for (j = low; j <= high- 1; j++) // no. of iterations=high-1-low+1
{
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot)
{
i++;    // increment index of smaller element
swap arr[i] and arr[j]
}
}
swap arr[i + 1] and arr[high]) // +1 swap
return (i + 1)
}

To understand my doubt please refer to the code above.

In the 1st level high=n and low=1. For the worst case of swapping, for each iteration there should be swapping. Hence total no. of swappings would be high-1-low+1=n-1-1+0=n-2 and outside the loop there is another swapping. Total swapping would be = n-1 in the 1st level. No. of swaps=O(n).

At 2nd level, if the pivot divides the array into equal halves then no. of swapping in this way is (low=1,high=(n/2)-1) so total swaps= (n/2)-1-1+1+1=n/2 and as there is another array set where low=n/2+1 and high=n so swaps=n/2 and hence total swaps=n.

If pivot element is placed at the end (for sorted array) then, low=1, high=n-1 so swaps= high-1-low+1+1=(n-1)-1-1+1+1=n-1.

We can assume that at each level the no. of swaps is O(n).

No. of levels= logn.

Total no. of swaps = nlogn.

How will it be n^2 @Bikram Sir ?

0
Sir in worst case, number of levels in quick sort is n .

Number of swaps in 1st level = n-1. In 2nd level n-2  and so on.

Total swaps= (n-1)+(n-2)+....+1=n(n-1)/2

= no of inversions
(C) Selection sort
Number of swaps
Quick sort = $\Theta (nlogn)$
Insertion sort =  $\Theta (n^{2})$
Selction sort = no swaps required since we are shifting elements in the array and not swapping
Heap sort =  $\Theta (nlogn)$

Option C
+3
in  quick  wont it be n^2 ?
+3

Yes, for Quick Sort , worst case time complexity O(n2)

on average, the algorithm takes O(n log n) comparisons to sort n items.

+1
sir, in quisck sort, in worst case number of comparison will be O(n2) for dataset like 1,2,3,4,5...

but in worst case we have to do only nLogn swaps like 9,2,4,5,6,7,8

number of swaps will be like this n+(n/2+n/2)+(n/4+n/4+n/4+n/4) which is O(nlgn)

Let's try to analyse the number of swaps in each of the given sorting algorithms. Quick sort – Worst Case input for maximum number of swaps will be already sorted array in decreasing order. Recurrence for Total number of swaps in this case : T(n) = T(n-1) + O(n) // O(n) swaps will occur in alternate calls to partition algorithm. = O(n2) Insertion sort - Worst Case input for maximum number of swaps will be already sorted array in ascending order.When a new element is inserted into an already sorted array of k size, it can lead to k swaps (in case it is the smallest of all) in worst case. For n-1 iterations of insertion sort, total swaps will be O(n2). Selection sort – There is no Worst case input for selection sort. Since it searches for the index of kth minimum element in kth iteration and then in one swap, it places that element into its orrect position. For n-1 iterations of selection sort, it can have O(n) swaps. Heap sort – Total number of swaps in Heap sort can be O(nlogn) as after performing Build-heap which may require O(n) swaps, it performs n-1 extract-min operations resulting into O(nlogn) swaps.

1
2