Page 1 of 1

SAIL - SortList (sortowanie list)

Posted: Sat Mar 12, 2016 10:22 pm
by zoNE
Serpent wrote:Napisałem funkcje które umożliwiają sortowanie list rosnąco i malejąco.


Sortowanie rosnące (od minimum do maksimum)
Spoiler:

Code: Select all

Export Function SortListAsc(list);
var i, j, sort_list, add;
begin
sort_list := [];

for i = 1 to list do
    begin
    if i = 1 then
       sort_list := sort_list ^ list[i]
    else
     begin
     for j = 1 to sort_list do
         begin
         add := false;

         if list[i] < sort_list[j] then
            begin
            sort_list := Insert(sort_list, j, list[i]);
            add := true;
            break;
            end;
         end;
     
     if not add then
        sort_list := sort_list ^ list[i];

     end;
    end;

result := sort_list;
End;
Sortowanie malejące (od maksimum do minimum)
Spoiler:

Code: Select all

Export Function SortListDesc(list);
var i, j, sort_list, add;
begin
sort_list := [];

for i = 1 to list do
    begin
    if i = 1 then
       sort_list := sort_list ^ list[i]
    else
     begin
     for j = 1 to sort_list do
         begin
         add := false;

         if list[i] > sort_list[j] then
            begin
            sort_list := Insert(sort_list, j, list[i]);
            add := true;
            break;
            end;
         end;

     if not add then
        sort_list := sort_list ^ list[i];

     end;
    end;

result := sort_list;
End;
Przykład użycia:

Code: Select all

Export Function Tablica;
var tab1, tab2, tab3;
begin
tab1 := [9, 24, 1, 0, 12];
tab2 := SortListDesc(tab1);
tab3 := SortListAsc(tab1);
display_strings := tab2;
Wait(0$03);
display_strings := tab3;
end;
// tab2 = [24, 12, 9, 1, 0]
// tab3 = [0, 1, 9, 12, 24]
Author: Serpent
Topic: https://forum.original-war.net/viewtopi ... =42&t=4685