ComputersProgramming

Sorting algorithms as they are

Sorting is the arrangement of objects in a certain order, for example, in descending order or ascending order. In general, ordering 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 (stages): comparison and permutation of the elements in pairs until the sequence becomes ordered.

Sorting algorithms can be classified into internal and external. The first are characterized by the fact that all sorted elements are placed in the RAM and it is possible to get 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 elements when they are in the structure of a one-dimensional array. Each such element has a serial number, and the element is accessed by index. Sorting algorithms in this case turn out to be the most simple and understandable for use.

We consider an internal descending sorting algorithm 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 method of linear sorting or the method of exchange sort 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]). To do 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 the programming language Pascal:

About the second option (an improved bubble method), 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. This means that we will not spend the computing resources of the system and the time for a 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 sequencing data sequences. When choosing a particular algorithm, you should take into account the costs in terms of time and system resources.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.unansea.com. Theme powered by WordPress.