0
0
Rubyprogramming~10 mins

Array sorting and reversing in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Array sorting and reversing
Start with array
Call sort method
Compare elements
Rearrange elements in ascending order
Sorted array ready
Call reverse method
Swap elements from ends towards center
Reversed array ready
End
The array is first sorted by comparing and rearranging elements in ascending order, then reversed by swapping elements from the ends towards the center.
Execution Sample
Ruby
arr = [3, 1, 4, 2]
sorted_arr = arr.sort
reversed_arr = sorted_arr.reverse
puts reversed_arr
This code sorts the array in ascending order, then reverses it, and prints the reversed array.
Execution Table
StepActionArray StateOutput/Result
1Initialize arr[3, 1, 4, 2]None
2Call arr.sort[3, 1, 4, 2]Returns [1, 2, 3, 4]
3Assign sorted_arr[1, 2, 3, 4]None
4Call sorted_arr.reverse[1, 2, 3, 4]Returns [4, 3, 2, 1]
5Assign reversed_arr[4, 3, 2, 1]None
6Print reversed_arr[4, 3, 2, 1]4 3 2 1
7End[4, 3, 2, 1]Program ends
💡 All steps completed, program ends after printing reversed array.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
arr[3, 1, 4, 2][3, 1, 4, 2][3, 1, 4, 2][3, 1, 4, 2]
sorted_arrnilnil[1, 2, 3, 4][1, 2, 3, 4]
reversed_arrnilnilnil[4, 3, 2, 1]
Key Moments - 3 Insights
Why does arr.sort not change the original array 'arr'?
Because in Ruby, 'sort' returns a new sorted array and does not modify the original array. This is shown in execution_table step 2 where 'arr' stays the same but 'sorted_arr' gets the sorted values.
What does reverse do to the array?
The 'reverse' method returns a new array with elements in opposite order. In step 4, 'sorted_arr' remains ascending, but 'reversed_arr' gets the reversed order.
Why do we assign the result of sort and reverse to new variables?
Because both 'sort' and 'reverse' return new arrays without changing the original. Assigning to new variables keeps the results accessible, as seen in steps 3 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'sorted_arr' after step 2?
Anil
B[3, 1, 4, 2]
C[1, 2, 3, 4]
D[4, 3, 2, 1]
💡 Hint
Check variable_tracker for 'sorted_arr' after step 2; it is assigned after step 3.
At which step does the array become reversed?
AStep 2
BStep 4
CStep 3
DStep 6
💡 Hint
Look at execution_table step 4 where reverse is called and reversed_arr is assigned.
If we changed 'arr.sort' to 'arr.sort!' (with exclamation), how would 'arr' change after step 2?
AIt would remain [3, 1, 4, 2]
BIt would become [4, 3, 2, 1]
CIt would become [1, 2, 3, 4]
DIt would become nil
💡 Hint
In Ruby, methods ending with '!' modify the original object, so 'arr' would be sorted in place.
Concept Snapshot
Array sorting and reversing in Ruby:
- Use arr.sort to get a new sorted array (original unchanged).
- Use arr.sort! to sort in place (modifies original).
- Use arr.reverse to get a new reversed array.
- Use arr.reverse! to reverse in place.
- Assign results to variables to keep changes.
Full Transcript
This visual trace shows how Ruby arrays can be sorted and reversed. Starting with an array [3, 1, 4, 2], calling 'sort' returns a new sorted array [1, 2, 3, 4] without changing the original. Then calling 'reverse' on the sorted array returns a new reversed array [4, 3, 2, 1]. Variables track these changes step by step. Key points include understanding that 'sort' and 'reverse' return new arrays, so assigning their results is necessary to keep changes. The quiz tests understanding of when variables change and the difference between methods that modify in place and those that don't.