7 votes 7 votes How many comparisons are needed to sort an array of length $5$ if a straight selection sort is used and array is already in the opposite order? $1$ $10$ $15$ $20$ Algorithms isro2008 algorithms sorting selection-sort + – ajit asked Sep 20, 2015 • edited Dec 4, 2022 by Lakshman Bhaiya ajit 9.8k views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
Best answer 8 votes 8 votes 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 nelements (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 whatever order they are arranged the number of comparisons will be sum of n-1 terms. Here n=5 So total comparisons= 4*(4+1)/2=10. Bipin answered Sep 29, 2015 • selected Jun 28, 2016 by Desert_Warrior Bipin comment Share Follow See all 3 Comments See all 3 3 Comments reply Anindita commented Jun 22, 2016 reply Follow Share what is the formula actually?...Is it n(n-1)/2 or n(n+1)/2?...Please reply ASAP. 0 votes 0 votes Kapil commented Jun 23, 2016 reply Follow Share dont follow the formula, follow the general algorithm It will be easy. It is n(n - 1)/2 2 votes 2 votes saurav _overflow commented Jun 28, 2016 reply Follow Share N(N-1)/2 0 votes 0 votes Please log in or register to add a comment.
3 votes 3 votes b) 10 each cell denote the total number of comparison performed to select the element to be placed in that cell. 4 3 2 1 0 amarVashishth answered Sep 20, 2015 amarVashishth comment Share Follow See all 0 reply Please log in or register to add a comment.
1 votes 1 votes The number of comparisons needed will be.. 1st Iteration - 4 2nd Iteration - 3 3rd Iteration -2 4rth Iteration -1 So to fully sort - 4+3+2+1 = 10 comparisons are needed. sai krishna Pujari answered Jul 2, 2016 sai krishna Pujari comment Share Follow See all 0 reply Please log in or register to add a comment.