Sorting is an arrangementobjects in a certain order, for example, in descending order or ascending order. In general, the ordering of elements is the most common manipulation with data, which makes it easier to find the right information in the future. This largely applies to various database management systems. Sorting algorithms currently exist in large numbers, although they have similar features (steps): comparison and permutation of the elements in pairs until the sequence becomes ordered.
Sorting algorithms can be classified intointernal and external. The first are characterized by the fact that all sorted elements are placed in the RAM and it is possible to obtain random access to any of them. The latter can work with data located in external memory (in files). Access to such elements can be implemented sequentially.
It is more convenient to sort the items when they arein the structure of a one-dimensional array. Each such element has a serial number, and the array element is accessed by index. Sorting algorithms in this case turn out to be the most simple and understandable for use.
Consider an internal sorting algorithm fordescending by the bubble method and its improved version, differing in the time spent for sorting. Sorting by the bubble method actually has many names. It is also called the linear sorting method or the exchange-sorting method by choice. But, however, it's not a name. Why a bubble? Once in the water, the air bubble will float up, as it is easier. So, for example, when sorting in ascending order, the smallest of the elements will appear at the top.
Let's consider the first variant of algorithm of sorting of an array by a bubble method. The verbal algorithm for sorting an array that has the identifier mas and consists of N elements looks like this:
1.Place the largest element of the array in place of the first element (mas [1]). For this we will compare it in turn with all the remaining elements (mas [2], mas [3] ... mas [N]). If it turns out that any of the remaining elements are greater than mas [1], then it is required to swap them (via the additional variable buf).
2. After excluding the element mas [1] from consideration, repeat paragraph 1 for the element mas [2].
3. These actions should be repeated for all elements except the last.
Implementation of the bubble sorting algorithm in Pascal:
About the second option (an improved methodbubble) we can say that this is a quick sort algorithm. So, if you try to use it to sort an already sorted array, the algorithm will finish its work after the first pass through the elements of the array. So, we will not spend computing resources of the system and time for meaningless comparison of the elements.
Here is the implementation of this sorting algorithm for the Pascal programming language:
So, sorting algorithms are a means of ordering data sequences. When choosing a particular algorithm, you should take into account the costs in terms of time and resources of the system.