# Sorting World

This world provides tools to experiment with the sorting algorithms. It can be used in two different ways: the first one is naturally to write the required sorting algorithms. But it is also possible to simply use the demo mode of each exercise to observe the behavior of sorting algorithms. It helps understanding the differences between each of them.

## Methods available to sorting algorithms

Method | Action | Cost
[!java|c]int [/!]getValueCount() [!scala]:Int[/!] | Returns the amount of values in the array | none
[!java]boolean [/!][!c]int [/!]isSmaller([!java|c]int [/!]i[!scala]:Int[/!], [!java|c]int [/!]j[!scala]:Int[/!]) [!scala]:Boolean[/!] | Returns true if the content of cell i is strictly smaller than the one of cell j | two reads
[!java]boolean [/!][!c]int [/!]isSmallerThan([!java|c]int [/!]i[!scala]:Int[/!], [!java|c]int [/!]value[!scala]:Int[/!])[!scala] :Boolean[/!] | Returns true if the content of cell i is strictly smaller than the `value` | one read
[!java|c]void [/!]swap([!java|c]int [/!]i[!scala]:Int[/!], [!java|c]int [/!]j[!scala]:Int[/!]) | Swaps the content of cell i and the one of cell j | two reads, two writes
[!java|c]void [/!]copy([!java|c]int [/!]from[!scala]:Int[/!], [!java|c]int [/!]to[!scala]:Int[/!]) | Copy the content of cell 'from' into the cell 'to' | one read, one write
[!java|c]int [/!]getValue([!java|c]int [/!]idx[!scala]:Int[/!]) | Returns the value of cell idx | one read
[!java|c]void [/!]setValue([!java|c]int [/!]idx[!scala]:Int[/!], [!java|c]int [/!]value[!scala]:Int[/!]) | Sets cell 'idx' to the `value` | one write
[!java]boolean [/!][!c]int [/!]isSelected() [!scala]:Boolean[/!] | Returns whether the current world is selected in the graphical interface. | none

## History view

It is not enough to sort the array to pass the exercises. Your solution must strictly follow the expected behavior of each exercise. This is enforced by checking that your algorithm needs the same amount of read and write operations to sort the array. When they don't match, understanding the difference between your code and the expected solution can reveal very difficult.

To help in this process, it is possible to graphically explore the history of your sorting algorithm. Switch to the Objective view and use the contextual menu (right click) to switch from the view of the current state to the view of its history.

The history view is a bit hairly at the first glance, but actually rather simple: The time flows from left to right on this graph, and each row is a cell of your array. The curved lines that go navigate between rows represent a given data value. When two lines cross, this means that two values were swapped at this time stamp; A line fork represent a value copy; When a value is magenta and followed by an interrogation mark (?), it was read using getValue(); If the value is red and followed with an exclamation point (!), it was written using setValue().

This view, inspired from Aldo Cortesi, reveals very helpful understand the inner behavior of sorting algorithms.