3. Algorithm Tracing. If the Sort algorithm is applied to the input list (7, 6,
ID: 3902287 • Letter: 3
Question
3. Algorithm Tracing. If the Sort algorithm is applied to the input list (7, 6, 3, 4, 1, 2) how many exchanges are performed between elements in this input list? List each swap in the order in which it is made during the execution of the algorithm. Note that we are not talking about comparisons here, but rather only comparisons that result in an interchange. (10 points) Sort is the following sorting algorithm (which you may assume is correct), called initially with i 1 and j - n to sort a list of n numbers in array A: Sort(A, i,j) 1if Ali>AU 3 ifi +12j then exchange A[i] ?? ALi] 4 then return 6 7 8 Sort(A,i,j-k) Sort(A, i +k,j) Sort(A, i.j-k)Explanation / Answer
=> Sort(A[7, 6, 3, 4, 1, 2], 1, 6)
A[1] > A[6] => 1 Exchange
New Array: A[2, 6, 3, 4, 1, 7]
K : 2
=> Sort(A[2, 6, 3, 4, 1, 7], 1, 4)
K : 1
=> Sort(A[2, 6, 3, 4, 1, 7], 1, 3)
K : 1
=> Sort(A[2, 6, 3, 4, 1, 7], 1, 2)
Return
=> Sort(A[2, 6, 3, 4, 1, 7], 2, 3)
A[2] > A[3] => 1 Exchange
New Array: A[2, 3, 6, 4, 1, 7]
Return
=> Sort(A[2, 3, 6, 4, 1, 7], 1, 2)
Return
=> Sort(A[2, 3, 6, 4, 1, 7], 2, 4)
K : 1
=> Sort(A[2, 3, 6, 4, 1, 7], 2, 3)
Return
=> Sort(A[2, 3, 6, 4, 1, 7], 3, 4)
A[3] > A[4] => 1 Exchange
New Array: A[2, 3, 4, 6, 1, 7]
Return
=> Sort(A[2, 3, 4, 6, 1, 7], 2, 3)
Return
=> Sort(A[2, 3, 4, 6, 1, 7], 1, 3)
K : 1
=> Sort(A[2, 3, 4, 6, 1, 7], 1, 2)
Return
=> Sort(A[2, 3, 4, 6, 1, 7], 2, 3)
Return
=> Sort(A[2, 3, 4, 6, 1, 7], 1, 2)
Return
=> Sort(A[2, 3, 4, 6, 1, 7], 3, 6)
K : 1
=> Sort(A[2, 3, 4, 6, 1, 7], 3, 5)
A[3] > A[5] => 1 Exchange
New Array: A[2, 3, 1, 6, 4, 7]
K : 1
=> Sort(A[2, 3, 1, 6, 4, 7], 3, 4)
Return
=> Sort(A[2, 3, 1, 6, 4, 7], 4, 5)
A[4] > A[5] => 1 Exchange
New Array: A[2, 3, 1, 4, 6, 7]
Return
=> Sort(A[2, 3, 1, 4, 6, 7], 3, 4)
Return
=> Sort(A[2, 3, 1, 4, 6, 7], 4, 6)
K : 1
=> Sort(A[2, 3, 1, 4, 6, 7], 4, 5)
Return
=> Sort(A[2, 3, 1, 4, 6, 7], 5, 6)
Return
=> Sort(A[2, 3, 1, 4, 6, 7], 4, 5)
Return
=> Sort(A[2, 3, 1, 4, 6, 7], 3, 5)
K : 1
=> Sort(A[2, 3, 1, 4, 6, 7], 3, 4)
Return
=> Sort(A[2, 3, 1, 4, 6, 7], 4, 5)
Return
=> Sort(A[2, 3, 1, 4, 6, 7], 3, 4)
Return
=> Sort(A[2, 3, 1, 4, 6, 7], 1, 4)
K : 1
=> Sort(A[2, 3, 1, 4, 6, 7], 1, 3)
A[1] > A[3] => 1 Exchange
New Array: A[1, 3, 2, 4, 6, 7]
K : 1
=> Sort(A[1, 3, 2, 4, 6, 7], 1, 2)
Return
=> Sort(A[1, 3, 2, 4, 6, 7], 2, 3)
A[2] > A[3] => 1 Exchange
New Array: A[1, 2, 3, 4, 6, 7]
Return
=> Sort(A[1, 2, 3, 4, 6, 7], 1, 2)
Return
=> Sort(A[1, 2, 3, 4, 6, 7], 2, 4)
K : 1
=> Sort(A[1, 2, 3, 4, 6, 7], 2, 3)
Return
=> Sort(A[1, 2, 3, 4, 6, 7], 3, 4)
Return
=> Sort(A[1, 2, 3, 4, 6, 7], 2, 3)
Return
=> Sort(A[1, 2, 3, 4, 6, 7], 1, 3)
K : 1
=> Sort(A[1, 2, 3, 4, 6, 7], 1, 2)
Return
=> Sort(A[1, 2, 3, 4, 6, 7], 2, 3)
Return
=> Sort(A[1, 2, 3, 4, 6, 7], 1, 2)
Return
====================
total 7 exchanges were made.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.