This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
csce420pine64backup/hw3/hw3pr1.pl
2017-11-11 21:08:10 -06:00

25 lines
740 B
Prolog

my_sort([], Sorted2, Sorted2).
my_sort(Unsorted, Sorted, Sorted2) :-
get_max(Unsorted, Max, LessList),
append(Sorted, [Max], NewSorted),
my_sort(LessList, NewSorted, Sorted2).
my_sort(Unsorted, Sorted) :-
my_sort(Unsorted, [], ReverseSorted),
reverse(ReverseSorted, Sorted),
write(Sorted).
get_max([], Max, LessList, Max, LessList).
get_max(Unsorted, X, Unsorted2, Max, LessList) :-
select(Y, Unsorted, Rest),
(X > Y,
append(Unsorted2, [Y], NewList),
get_max(Rest, X, NewList, Max, LessList);
append(Unsorted2, [X], NewList),
get_max(Rest, Y, NewList, Max, LessList)).
get_max(Unsorted, Max, LessList) :-
select(X, Unsorted, Unsorted2),
get_max(Unsorted2, X, [], Max, LessList).