Subsequently repeated in the same way for the remaining array elements with no longer includes elements that have been ordered array.
Suppose the array elements will be sorted ascending:
Step 1: Determine the maximum price in the L [1 .. N] maximum price Interchange with L [N]
Step 2: Determine the maximum price in the L [1 .. N-1] Interchange maximum price with L [N-1]
Step 3: Determine the maximum price in the L [1 .. N-2] Interchange maximum price with L [N-2]. .
Step N-1: Determine the maximum price in the L [1 .. 2] Interchange maximum price with L [2]
Remaining element is L [1] does not need to be sorted because it is the last element.
So at each step there is a sorting process of finding the maximum price checkers exchange process a two-element array. Number of sorting steps is N-1.
For example :
Given an Array instance with N = 6 pieces Ascending elements not, this array will be sorted ascending. Element of the array is 25, 27, 10, 8, 76, 21
The Alhorithm are as follows:
Procedure MaksSort (input / output L: LarikInt, Input N: Integer)
{
K. Early : Element Array L has been defined
K. End : Element Array ascending sequences L such that L [1] ≤ L [2] ≤ ... ≤ L [N]
Process : Sorting array elements with L so that the sequential ascending Sort Maximum method declaration}
i : integer {counter for the number of steps}
j : integer {counter to find the maximum value}
Temp : integer {variables to exchange}
imaks : integer {maximum array element index while}
max : integer {maximum array element while}
For i ← N downto 2 do
Maks ← L[1]
iMaks ← 1
for j ← 2 to i do
if L[j] > Maks then
Maks ← L[j]
iMaks ← j
endif
endfor
{Swap L[i] with L[iMaks] }
Temp ← L[i]
L[i] ← L[iMaks]
L[imaks] ← Temp
endfor
Minimum Ordering Algorithm
Suppose Array elements will be sorted decline:
Step 1: Determine the minumum price in L [1 .. N] Interchange minumum price with L [N]
Step 2: Determine the minumum price in L [1 .. N-1] minumum price swap with L [N-1]
Step 3: Determine the minumum price in L [1 .. N-2] Interchange minumum price with L [N-2]
.
.
.
Step N-1: Determine the minumum price in L [1 .. 2] Interchange minumum price with L [2]
The remaining elements are L [1], does not need sorted out as the only one for ascending sorting is
the reverse, ie exchanging minimum elements with the elements left in the array is not ordered
The algorithm are as follows:
Procedure MinimumSort (input / output L: LarikInt, Input N: Integer)
{
K. Early : Element Array L has been defined
K. End : Element Array L sequences decreased such that L [1] ≥ L [2] ≥ ... ≥ L [N]
Process : Sorting array elements with L so that the sequential decline Sort Minimum method declaration}
i : integer {counter for the number of steps}
j : integer {counter to find the minimum value}
Temp : integer {auxiliary variables to exchange}
Imin : integer {index while the minimum array element}
Min : integer {minimum integer array element temporary}
For i ← 1 to N-1 do
Min ← L[1]
iMin ← 1
for j ← i+1 to N do
if L[j] > Min then
Min ←
L[j]
iMin ← j
endif
endfor
{Swap L[i] with L[iMin] }
Temp ← L[i]
L[i] ← L[iMin]
L[iMin] ← Temp
endfor
No comments:
Post a Comment