Skip to content

Instantly share code, notes, and snippets.

@hpcx82
Created May 25, 2012 06:21
Show Gist options
  • Save hpcx82/2786148 to your computer and use it in GitHub Desktop.
Save hpcx82/2786148 to your computer and use it in GitHub Desktop.
quick sort in functional style with: scala, c++, perl, java
#include <iostream>
#include <vector>
using namespace std;
vector<int> concat(const vector<int>& v1, const vector<int>& v2, const vector<int>& v3)
{
vector<int> res = v1;
res.insert(res.end(), v2.begin(), v2.end());
res.insert(res.end(), v3.begin(), v3.end());
return res;
}
template<class Predicate>
vector<int> filter(const vector<int>& input, Predicate pred)
{
vector<int> res;
for(auto it = input.begin(); it < input.end(); ++it)
{
if(pred(*it)) res.push_back(*it);
}
return res;
}
vector<int> sort(const vector<int>& input)
{
if(input.size() <= 1)
return input;
else
{
int pivot = input[0];
return concat(
sort(filter(input, [=](int x){return x < pivot;})),
filter(input, [=](int x){return x == pivot;}),
sort(filter(input, [=](int x){return x > pivot;}))
);
}
}
int main()
{
int arr[] = {3, 9, 3, -1, 3, 4, 8, 234, 923, 98, 12, 12, -13};
vector<int> vec(arr, arr+sizeof(arr)/sizeof(arr[0]));
vector<int> sorted = sort(vec);
for(auto it = sorted.begin(); it < sorted.end(); ++it)
{
cout << *it << "\t";
}
cout << endl;
return 0;
}
public class quicksort
{
private static abstract class Predicate
{
abstract public boolean check(int i);
}
private static int[] concat(int[] a1, int[] a2, int[] a3)
{
int[] res = new int[a1.length + a2.length + a3.length];
System.arraycopy(a1, 0, res, 0, a1.length);
System.arraycopy(a2, 0, res, a1.length, a2.length);
System.arraycopy(a3, 0, res, a1.length + a2.length, a3.length);
return res;
}
private static int[] filter(int[] input, Predicate pred)
{
int[] tmp = new int[input.length];
int count = 0;
for(int i : input)
{
if(pred.check(i))
tmp[count++] = i;
}
int[] res = new int[count];
System.arraycopy(tmp, 0, res, 0, count);
return res;
}
public static int[] sort(int[] input)
{
if(input.length <= 1)
return input;
else
{
final int pivot = input[0];
return concat(
sort(filter(input, new Predicate() {@Override public boolean check(int i) {return i < pivot;}})),
filter(input, new Predicate() {@Override public boolean check(int i) {return i == pivot;}}),
sort(filter(input, new Predicate() {@Override public boolean check(int i) {return i > pivot;}}))
);
}
}
public static void main(String[] args)
{
int[] array= {1, 2, 3, 5, 6, 0, 9, -1, 0, 234, 98};
int[] output = sort(array);
for(int i : output)
System.out.println(i);
}
}
use strict;
use warnings;
sub qsort
{
my @arr = @_;
my $length = @arr;
if($length <= 1)
{
return @arr;
}
else
{
my $pivot = $arr[0];
return (qsort(grep { $_ < $pivot } @arr),
(grep { $_ == $pivot } @arr),
qsort(grep { $_ > $pivot } @arr));
}
}
sub main()
{
my @input = (1, 3, 5, 19, -1, 34, 0, 1324, 0, 2341, -93);
print join "\n", qsort(@input);
}
main();
object quicksort extends Application {
def sort(xs: Array[Int]): Array[Int] = {
if(xs.length <= 1) xs
else {
val pivot = xs(0)
Array.concat(
sort(xs filter (pivot >)),
xs filter (pivot == ),
sort(xs filter (pivot <)))
}
}
val numbers = Array(3, 2, 1, 6, -5, -98, 231, 9, 2);
val numbers2 = sort(numbers);
for(i <- 0 to numbers2.length-1)
println(numbers2(i));
}
@hpcx82
Copy link
Author

hpcx82 commented May 25, 2012

Line Of Code: C++/Java > Perl > Scala

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment