Question: The sorting algorithm 1 Function sort(A) 2 For i=1 to length (A)-1 3 For i=1 to length (A) down to i+1 4 If A[j] <

 The  sorting algorithm 1 Function sort(A) 2 For i=1 to length (A)-1 3


 For i=1 to length (A) down to i+1 4 If A[j] < A[j-1] 5 swap A[j] and


 A[j-1] 6 Print (A)


(a) What is printed when sort is run on A=[5,4,3,2,1] ? First element of of list is right most A.[1]=5. 


(b) Consider operation of sort on list of length N. How many comparison in line 4 take place when i=1 and when i=N-1


(c) How many comparisons take place in total? show your working. You may use result 1+2+dots..+n=n[(n+1)/(2)]

Step by Step Solution

3.36 Rating (149 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

a When sort is run on A54321 the following will be printed 1 2 3 4 5 This is because the algorithm w... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!