Сортировка массива
Эти алгоритмы выстраивают числа в массиве от меньших к большему.
Простая сортировка
SUB(sort)
$siz = ARRSIZE($arr)
$srtcheck = 0
$srt = 0
FOR($a=$siz-1,($a>0)&($srt=0),-1)
FOR($b=0,$b<$a)
IF($arr[$b] > $arr[$b+1])
$srtcheck = 1
$tmp = $arr[$b]
$arr[$b] = $arr[$b+1]
$arr[$b+1] = $tmp
END_IF
END_CYC
IF($srtcheck = 0)
$srt = 1
END_IF
END_CYC
END_SUB
arrpush ($arr, 454)
arrpush ($arr, 66)
arrpush ($arr, 43)
arrpush ($arr, 77)
arrpush ($arr, 22)
arrpush ($arr, 1)
LOGCLEAR
LOGWRITE ("Было")
FOR($i = 0, $i < ARRSIZE($arr))
LOGWRITE ($arr[$i])
END_CYC
LOGWRITE ("")
sort()
LOGWRITE ("Стало")
FOR($i = 0, $i < ARRSIZE($arr))
LOGWRITE ($arr[$i])
END_CYC
HALT
Парная сортировка (например сортировка координат) по первому числу
SUB(sort)
$siz = ARRSIZE($arr)
$srtcheck = 0
$srt = 0
FOR($a=$siz-2,($a>0)&($srt=0),-2)
FOR($b=0,$b<$a,2)
IF($arr[$b] > $arr[$b+2])
$srtcheck = 1
$tmp = $arr[$b]
$tmp2 = $arr[$b+1]
$arr[$b] = $arr[$b+2]
$arr[$b+1] = $arr[$b+3]
$arr[$b+2] = $tmp
$arr[$b+3] = $tmp2
END_IF
END_CYC
IF($srtcheck = 0)
$srt = 1
END_IF
END_CYC
END_SUB
arrpush ($arr, 22)
arrpush ($arr, 66)
arrpush ($arr, 22)
arrpush ($arr, 77)
arrpush ($arr, 22)
arrpush ($arr, 88)
arrpush ($arr, 201)
arrpush ($arr, 66)
arrpush ($arr, 201)
arrpush ($arr, 77)
arrpush ($arr, 201)
arrpush ($arr, 88)
arrpush ($arr, 10)
arrpush ($arr, 66)
arrpush ($arr, 10)
arrpush ($arr, 77)
arrpush ($arr, 10)
arrpush ($arr, 88)
LOGCLEAR
LOGWRITE ("Было")
FOR($i = 0, $i < ARRSIZE($arr), 2)
LOGWRITE ($arr[$i]," : ",$arr[$i+1])
END_CYC
LOGWRITE ("")
sort()
LOGWRITE ("Стало")
FOR($i = 0, $i < ARRSIZE($arr), 2)
LOGWRITE ($arr[$i]," : ",$arr[$i+1])
END_CYC
HALT
Парная сортировка по первому затем по второму числу
SUB(sort)
$siz = ARRSIZE($arr)
$srtcheck = 0
$srt = 0
FOR($a=$siz-2,($a>0)&($srt=0),-2)
FOR($b=0,$b<$a,2)
IF($arr[$b] > $arr[$b+2])
$srtcheck = 1
$tmp = $arr[$b]
$tmp2 = $arr[$b+1]
$arr[$b] = $arr[$b+2]
$arr[$b+1] = $arr[$b+3]
$arr[$b+2] = $tmp
$arr[$b+3] = $tmp2
END_IF
END_CYC
IF($srtcheck = 0)
$srt = 1
END_IF
END_CYC
$srtcheck = 0
$srt = 0
FOR($a=$siz-2,($a>0)&($srt=0),-2)
FOR($b=0,$b<$a,2)
IF($arr[$b] = $arr[$b+2])
IF($arr[$b+1] > $arr[$b+3])
$srtcheck = 1
$tmp = $arr[$b+1]
$tmp2 = $arr[$b+3]
$arr[$b+1] = $tmp2
$arr[$b+3] = $tmp
END_IF
END_IF
END_CYC
IF($srtcheck = 0)
$srt = 1
END_IF
END_CYC
END_SUB
arrpush ($arr, 22)
arrpush ($arr, 60)
arrpush ($arr, 22)
arrpush ($arr, 77)
arrpush ($arr, 22)
arrpush ($arr, 88)
arrpush ($arr, 201)
arrpush ($arr, 66)
arrpush ($arr, 201)
arrpush ($arr, 30)
arrpush ($arr, 201)
arrpush ($arr, 88)
arrpush ($arr, 10)
arrpush ($arr, 66)
arrpush ($arr, 10)
arrpush ($arr, 77)
arrpush ($arr, 10)
arrpush ($arr, 28)
LOGCLEAR
LOGWRITE ("Было")
FOR($i = 0, $i < ARRSIZE($arr), 2)
LOGWRITE ($arr[$i]," : ",$arr[$i+1])
END_CYC
LOGWRITE ("")
sort()
LOGWRITE ("Стало")
FOR($i = 0, $i < ARRSIZE($arr), 2)
LOGWRITE ($arr[$i]," : ",$arr[$i+1])
END_CYC
HALT