diff --git a/nowy.pl b/nowy.pl new file mode 100644 index 0000000..5afb3a0 --- /dev/null +++ b/nowy.pl @@ -0,0 +1,14 @@ +% Implementacja Quicksorta w Prologu +% Autor korpo, https://github.com/korpo + +quicksort([],[]). +quicksort([X],[X]). +quicksort([X|Xs],Ys) :- + partition(Xs,X,Left,Right), + quicksort(Left,Ls), + quicksort(Right,Rs), + append(Ls,[X|Rs],Ys). + +partition([X|Xs],Y,[X|Ls],Rs) :- X =< Y, partition(Xs,Y,Ls,Rs). +partition([X|Xs],Y,Ls,[X|Rs]) :- X > Y, partition(Xs,Y,Ls,Rs). +partition([],_,[],[]). diff --git a/quicksort.pl b/quicksort.pl index c18564e..5afb3a0 100644 --- a/quicksort.pl +++ b/quicksort.pl @@ -1,17 +1,14 @@ % Implementacja Quicksorta w Prologu % Autor korpo, https://github.com/korpo -project_dedalus_quicksort([X],[X]). -project_dedalus_quicksort([X|Xs],Ys) :- - project_dedalus_quicksort_helper_partition(Xs,X,Left,Right), - project_dedalus_quicksort(Left,Ls), - project_dedalus_quicksort(Right,Rs), +quicksort([],[]). +quicksort([X],[X]). +quicksort([X|Xs],Ys) :- + partition(Xs,X,Left,Right), + quicksort(Left,Ls), + quicksort(Right,Rs), append(Ls,[X|Rs],Ys). -project_dedalus_quicksort_helper_partition([X|Xs],Y,[X|Ls],Rs) :- - X =< Y, - project_dedalus_quicksort_helper_partition(Xs,Y,Ls,Rs). -project_dedalus_quicksort_helper_partition([X|Xs],Y,Ls,[X|Rs]) :- - X > Y, - project_dedalus_quicksort_helper_partition(Xs,Y,Ls,Rs). -project_dedalus_quicksort_helper_partition([],_,[],[]). +partition([X|Xs],Y,[X|Ls],Rs) :- X =< Y, partition(Xs,Y,Ls,Rs). +partition([X|Xs],Y,Ls,[X|Rs]) :- X > Y, partition(Xs,Y,Ls,Rs). +partition([],_,[],[]). diff --git a/tests.pl b/tests.pl index fbaf4d2..2fc5d56 100644 --- a/tests.pl +++ b/tests.pl @@ -1,14 +1,14 @@ :- begin_tests(qs). :- use_module(library(lists)). -test(project_dedalus_quicksort,[nondet]) :- +test(quicksort,[nondet]) :- L = [4,5,1], - project_dedalus_quicksort(L, Sorted), + quicksort(L, Sorted), sort(L, Sorted). test(quicksort,[nondet]) :- - L=[8,4,2,1,3,6,5,7,12,10,9,11,14,13,15], - project_dedalus_quicksort(L, Sorted), + randseq(15,15,L), + quicksort(L, Sorted), sort(L, Sorted). :- end_tests(qs).