7. If A(0..n-1] is an array with n elements and procedure Swap exchanges its arguments, then the following code sorts A in descending order. int j + 0 while (j < n - 1 ) int k + 0 while ( k < n - j - 1 ) if ( A[k] < A[k + 1] ) Swap ( A[k], A[k + 1] ) end if k + k + 1 end while j+j + 1 end while If A initially contains n different elements sorted in ascending order, how many calls to Swap are made in total?

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter6: Arrays
Section: Chapter Questions
Problem 19RQ
icon
Related questions
Question
100%
7. If A[0..n-1] is an array with n elements and procedure Swap exchanges
its arguments, then the following code sorts A in descending order.
int j + 0
while (j < n - 1 )
int k + 0
while ( k < n - j - 1 )
if ( A[k] < A[k + 1] )
Swap ( A[k], A[k + 1] )
end if
k + k + 1
end while
j +j + 1
end while
If A initially contains n different elements sorted in ascending order, how many calls to
Swap are made in total?
Transcribed Image Text:7. If A[0..n-1] is an array with n elements and procedure Swap exchanges its arguments, then the following code sorts A in descending order. int j + 0 while (j < n - 1 ) int k + 0 while ( k < n - j - 1 ) if ( A[k] < A[k + 1] ) Swap ( A[k], A[k + 1] ) end if k + k + 1 end while j +j + 1 end while If A initially contains n different elements sorted in ascending order, how many calls to Swap are made in total?
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning