Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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.


Download ppt "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."

Similar presentations


Ads by Google