Download | Plain Text | No Line Numbers
- #!/usr/bin/perl
-
- use Data::Dumper;
- use strict;
-
- my @src = (35, 40, 12, 30, 94, 18, 6, 67);
- #my @src = (8, 6, 2, 3, 7, 4);
- #@src = reverse(@src);
-
- our $ident = 0;
-
- sub print_array
- {
- my ($a) = @_;
- my $str = "[";
- {
- $str .= ", ";
- }
- $str .= "]";
- }
-
- sub print_marks
- {
- my ($a, $x, $pos) = @_;
- my $str = " ";
- my $i = 0;
- {
- if ($j == $$pos[$i])
- {
- $str .= $x." ";
-
- $i++;
- {
- last;
- }
- }
- else
- {
- $str .= " ";
- }
- }
- }
-
- sub partition
- {
- my ($a, $l, $r, $x) = @_;
- my $i = $l - 1;
- my $j = $r;
-
- do
- {
- do
- {
- $i++;
- }
- until($$a[$i] <= $x);
-
- do
- {
- $j--;
- }
- until($j <= $i || $$a[$j] >= $x);
-
- if ($i < $j)
- {
-
- my $tmp = $$a[$i];
- $$a[$i] = $$a[$j];
- $$a[$j] = $tmp;
- }
- }
- until($i >= $j);
-
- if ($i != $r)
- {
- my $tmp = $$a[$i];
- $$a[$i] = $$a[$r];
- $$a[$r] = $tmp;
- }
-
- }
-
- sub quicksort
- {
- my ($a, $l, $r) = @_;
- $ident += 2;
- if ($l < $r)
- {
-
- my $x = $$a[$r];
- my $p = partition($a, $l, $r, $x);
- quicksort($a, $l, $p - 1);
- quicksort($a, $p + 1, $r);
- }
- $ident -= 2;
- }
-
-
-