Tuesday, February 3, 2009

Programming in MQL II. Sort by bubble

When writing programs often have similar problems. They are well known to experienced programmers, and, moreover, developed programming languages, these tasks have typically realized in the form of standard routines. One of these typical problems, sooner or later face a programmer, is a sort of data.
This realization of the language MQL does not contain embedded sub-sort the data, but it's not as bad as it might seem, because on the one hand MQL II has everything you need to self-realization of algorithms for sorting, and on the other hand - such tasks are an excellent tool for learning programming.
In previous issues of the journal were the basic design language MQL II, and the reader should already be familiar with such concepts as variable, its type, arrays, conditionals "if-then" and the cycles of "for-to".
For definiteness, we will call the process of sorting of the array in ascending order of their values. That is, suppose that there is an array of five elements with values of 35, 12, 38, 120, 34.
Upon completion of sorting this array, we need to get an array of five elements with values of 12, 34, 35.38, 120.
This article describes a simple way to sort the data, which is called "method of the bubble." Named because it is the fact that in the process of its execution by lower values (more light), like similar by air bubbles in the water surface (moving to the left edge of the array), a large value (more difficult) - sink (move on the right edge of the array).
Actually the algorithm itself is as follows: in the process of sorting, we "run" on the elements of the array, comparing the current element with the following. If the next element of the array is less than the current, then change their places.
"Runs" of the array of values again and again - as many times as elements in the array, each serving, if necessary, the described changes.

For visibility below shows the status of the array in each of the passages:

35, 12, 38, 120, 34
-------------------
12, 35, 38, 34, 120
12, 35, 34, 38, 120
12, 34, 35, 38, 120
12, 34, 35, 38, 120
12, 34, 35, 38, 120

To do this automatically, the following program:
var: ix (0);
var: iy (0);
var: tmp (0);
array: values [5] (0);

values [0] = 35;
values [1] = 12;
values [2] = 38;
values [3] = 120;
values [4] = 34;

for ix = 0 to 4 (
for iy = 0 to 3 (
if (values [iy]> values [iy +1]) then (
tmp = values [iy];
values [iy] = values [iy +1]; values [iy +1] = tmp;
);
);
);

In this example, we used two cycles, one of which is invested in the other. What does this mean? This means that for every change of the external cycle counter "ix", four times a cycle is performed on the internal cycle counter "iy".
Words describe what happens when we execute the inner loop:

for iy = 0 to 3 (
if (values [iy]> values [iy +1]) then (
tmp = values [iy];
values [iy] = values [iy +1];
values [iy +1] = tmp;
);
);

Recalling the description of the cycle "for-to" (Forex Magazine 2004 number 4, Education MQL II, Lesson number 4), we see that the team "for iy = 0 to 3, requires a PC to fulfill four that placed in brackets. And at the first repetition of the variable "iy" takes the value 0, then the second repeat, it is already one more t.e.1, then 2, and the last repeat of its value is 3.
Ie in this cycle, we look over all the values of the array, except the last, comparing the current element with the following. If the next element of the array is smaller than the current, then swap their values.
Attention, we are faced with another typical challenge faced by programmers! To swap values of two variables, we can not simply follow the two assignment values [iy] = values [iy +1] and values [iy +1] = values [iy], because assignment is lost after the first initial value.
For example if values [iy +1] is set to 12, and values [iy] is set to 35, in carrying values [iy] = values [iy +1], both variables will have values of 12. The second guideline values [iy +1] = values [iy] does not do anything useful, because at the time of its implementation, both variables have the same value as the initial value of the variable values [iy] for us to have disappeared forever!
What to do in this case? There are several exits from this situation: the easiest to understand - to create an additional variable for temporary storage of values values [iy]. In this case we use the variable "tmp". Now, remember the importance of values [iy] in the temporary variable, we can recover it after the transaction values [iy] = values [iy +1]. And already in the values [iy +1], we will write down the value stored in the variable "tmp".

Here's how it looks:

tmp = values [iy];
values [iy] = values [iy +1];
values [iy +1] = tmp;

Let us summarized the fact that we have:
If both write cycle, but instead of the internal cycle, to use his verbal description, it will be as follows:

for ix = 0 to 4 (
/ / "Run" on all elements of the array, except the last.
/ / If the current value is greater than that for them, then
/ / Swap values of current and next elements.
);

Once again, remembering the description of the cycle "for-to", see that the team "for ix = 0 to 4" will perform a block enclosed in curly brackets five times, ie as many times as elements in the sorted array.
Summarizing all the above again, we see that the verbal description of the algorithm, we have recorded for MQL II, is as follows:
As many times as elements in the sorted array, repeat the following procedure: perebiraya all elements of the array, except the last, to compare them with the next in the array if the current value is greater than that for him, then swap the values of current and next elements.
In the next article we bit about how you can accelerate the implementation of sorting algorithms "by the bubble, and discuss a method for fast searching the desired element in the sorted array. Also, to enhance the range of programming, we look another way of sharing the values of two variables.



Alexander Ivanov (AKA HORN)

No comments: