Download presentation
Presentation is loading. Please wait.
Published byAlexander Armstrong Modified over 6 years ago
1
Switch elements Example Suppose A = 3 and B = 4, and suppose that we want to switch these values. First attempt A = B ‘First statement B = A ‘Second statement This does not work. A B At the beginning 3 4 After the first statement 4 4 After the second statement 4 4
2
The problem is that the value of A is lost
The problem is that the value of A is lost. The solution is to save this value before switching. Second attempt Temp = A ‘First statement A = B ‘Second statement B = Temp ‘Third statement A B Temp At the beginning 3 4 undefined After the first statement 3 4 3 After the second statement 4 4 3 After the third statement 4 3 4
3
After finding the position of the largest element in the array, switch this element with the last element in the array.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.