+1 vote
in Data Structures & Algorithms II by (110k points)
The given array is arr = {1,2,3,4,5}. (bubble sort is implemented with a flag variable)The number of iterations in selection sort and bubble sort respectively are __________

(a) 5 and 4

(b) 1 and 4

(c) 0 and 4

(d) 4 and 1

The question was posed to me in a job interview.

Query is from Selection Sort in chapter Sorting of Data Structures & Algorithms II

1 Answer

+1 vote
by (614k points)
selected by
Best answer
Correct option is (d) 4 and 1

The best I can explain: Selection sort is insensitive to input, hence 4(n-1) iterations. Whereas bubble sort iterates only once to set the flag to 0 as the input is already sorted.

Related questions

Welcome to TalkJarvis QnA, a question-answer community website for the people by the people. On TalkJarvis QnA you can ask your doubts, curiosity, questions and whatever going in your mind either related to studies or others. Experts and people from different fields will answer.