Sorting algorithms/Quicksort: Difference between revisions

m (If you tried to run the offending code, you will get "TypeError: object of type 'filter' has no len()". So this means you either change the type to list or tuple.)
 
(One intermediate revision by the same user not shown)
Line 8,261:
<pre>
[-50, -34, -25, -20, -10, 5, 9, 11, 13, 19, 29, 30, 35, 36]
</pre>
 
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
function Partition(a: array of integer; l,r: integer): integer;
begin
var i := l - 1;
var j := r + 1;
var x := a[l];
while True do
begin
repeat
i += 1;
until a[i]>=x;
repeat
j -= 1;
until a[j]<=x;
if i<j then
Swap(a[i],a[j])
else
begin
Result := j;
exit;
end;
end;
end;
procedure QuickSort(a: array of integer; l,r: integer);
begin
if l>=r then exit;
var j := Partition(a,l,r);
QuickSort(a,l,j);
QuickSort(a,j+1,r);
end;
 
const n = 20;
 
begin
var a := ArrRandom(n);
Println('Before: ');
Println(a);
QuickSort(a,0,a.Length-1);
Println('After sorting: ');
Println(a);
end.
</syntaxhighlight>
{{out}}
<pre>
Before:
[67,95,79,96,14,56,25,9,4,56,70,62,33,52,13,12,73,19,8,72]
After sorting:
[4,8,9,12,13,14,19,25,33,52,56,56,62,67,70,72,73,79,95,96]
</pre>
 
246

edits