Sorting algorithms/Quicksort: Difference between revisions

Content added Content deleted
(Sorting algorithms/Quicksort in True BASIC)
(Sorting algorithms/Quicksort in Yabasic)
Line 9,729: Line 9,729:
Before: [echo, lima, charlie, whiskey, golf, papa, alfa, india, foxtrot, kilo]
Before: [echo, lima, charlie, whiskey, golf, papa, alfa, india, foxtrot, kilo]
After : [alfa, charlie, echo, foxtrot, golf, india, kilo, lima, papa, whiskey]
After : [alfa, charlie, echo, foxtrot, golf, india, kilo, lima, papa, whiskey]
</pre>

=={{header|Yabasic}}==
Rosetta Code problem: https://rosettacode.org/wiki/Sorting_algorithms/Quicksort
by Jjuanhdez, 03/2023
<syntaxhighlight lang="basic">dim array(15)
a = 0
b = arraysize(array(),1)

for i = a to b
array(i) = ran(1000)
next i

print "unsort ";
for i = a to b
print array(i) using("####");
if i = b then print ""; else print ", "; : fi
next i

quickSort(array(), a, b)

print "\n sort ";
for i = a to b
print array(i) using("####");
if i = b then print ""; else print ", "; : fi
next i
print
end

sub quickSort(array(), l, r)
local asize, i, j, pivot
size = r - l +1
if size < 2 return
i = l
j = r
pivot = array(l + int(size / 2))
repeat
while array(i) < pivot
i = i + 1
wend
while pivot < array(j)
j = j - 1
wend
if i <= j then
temp = array(i)
array(i) = array(j)
array(j) = temp
i = i + 1
j = j - 1
fi
until i > j
if l < j quickSort(array(), l, j)
if i < r quickSort(array(), i, r)
end sub</syntaxhighlight>
{{out}}
<pre>unsort 582, 796, 598, 478, 27, 125, 477, 679, 133, 513, 154, 93, 451, 463, 20
sort 20, 27, 93, 125, 133, 154, 451, 463, 477, 478, 513, 582, 598, 679, 796
</pre>
</pre>