Lecture
Consider the advantages of direct methods:
1. The programs of these methods are easy to understand, and they are short. Recall that the programs themselves also take up memory.
2. Direct methods are particularly convenient for explaining the characteristic features of the basic principles of most sorts.
3. Complicated methods require a small number of operations, but these operations are usually more complex themselves, and therefore for sufficiently small n the direct methods turn out to be faster, although for large n they, of course, should not be used.
Sorting methods "in the same place" can be divided according to their defining principles into 3 categories:
1. Sorting by means of inclusion (by insertion)
2. Sorting by selection (by selection)
3. Sorting using exchanges (by exchange)
Sort by direct inclusion
This method is widely used when playing cards. The elements (maps) are mentally divided into the already “ready” sequence a (1), ..., a (i-1) and the initial sequence. At each step, starting with i = 2 and increasing i each time by one, the i-th element is extracted from the initial sequence and transferred to the finished sequence, and it is inserted into the right place.
Direct methods include direct selection.
Consider the entire array row and select an element smaller or larger than element a (i), determine its place in the array - k, and then swap element a (i) and element a (k).
for i = 1 to n - 1
x = a (i)
k = i
for j = i + 1 to n
if x> a (j) then
k = j
x = a (k)
endif
next j
a (k) = a (i)
a (i) = x
next i
return
for i: = 1 to n - 1 do
begin
x: = a [i];
k: = i;
for j: = i + 1 to n do
if x> a [j] then
begin
k: = j;
x: = a [k];
end;
a [k]: = a [i];
a [i]: = x;
end;
Efficiency of the algorithm:
In the worst case, sorting by direct choice gives the order of n 2 , as for the number of comparisons, and for the number of displacements.
Create a group of N students. Enter them: last name, first name, year of birth, assessment of subjects: p. And al., Higher. mathematics, physics, programming, the total score of the session; Develop a program using the method of "direct selection", which would carry out the sorting (according to option):
1. Names of students alphabetically.
2. Names of students alphabetically in reverse order.
3. Seniority students (starting from the oldest).
4. Seniority students (starting from the youngest).
5. Students on a general score (ascending).
6. Students by total score (descending).
7. Students according to the results of the 1st exam (ascending).
8. Students according to the results of the 2nd exam (descending).
9. Students according to the results of the 3rd exam (ascending).
10. Students according to the results of the 4th exam (descending).
11. Names in alphabetical order.
12. Names in reverse alphabetical order.
Comments
To leave a comment
Structures and data processing algorithms.
Terms: Structures and data processing algorithms.