Did you know ... Search Documentation:
Pack dynworks -- prolog/quicksort.pl
PublicShow source

An implementation of the classic Quicksort sorting algorithm on generic lists. Quicksort works by selecting a 'pivot' element from the list to be sorted and partitioning the other elements into two sublists, according to whether they are less than or greater than the pivot.The sublists are then sorted recursively. For a description of the quicksort algorithm, see https://en.wikipedia.org/wiki/Quicksort .
This implementation takes as input a comparator. This is a predicate able to perform a comparison between any two elements of the input list as parameters, and return a negative number, zero, or a positive number, to indicate whether the first parameter is smaller than, equal to, or greater than the second parameter, respectively.

author
- GT Nunes
version
- 1.3.2
license
- BSD-3-Clause License
 quicksort(+List:list, :Comparator:pred, -SortedList:list) is det
Sort the contents of List according to the given comparison predicate, and unify the result with SortedList.
The comparison predicate must accept two parameters, ValueX and ValueY, which might be any two elements in List, and have the following behavior:
<Comparator>(+ValueX, +ValueY, -Result:number) is det
where Result is unified with
  a) 0 (zero)          - ValueX is equal to ValueY
  b) a negative number - ValueX is less than ValueY
  c) a positive number - ValueX is greater than ValueY

The criteria that will determine the results of the comparisons are entirely up to Comparator, and as such it must be able to handle the values it will receive. Nothing is done if List has less than 2 elements.

Arguments:
List- The list to be sorted
Comparator- Predicate to perform comparisons between any two elements in List
SortedList- The resulting sorted list
 quicksort_number_asc(+ValueX:number, +ValueY:number, -Cmp:int) is det
Compare the numbers ValueX and ValueY ascendingly.
Unify Cmp with -1, 0, or 1, depending on whether ValueX is smaller than, equal to, or greater than, ValueY.
Arguments:
ValueX- The first value to compare
ValueY- The second value to compare
Cmp- The result of the comparison
 quicksort_number_desc(+ValueX:number, +ValueY:number, -Cmp:int) is det
Compare the numbers ValueX and ValueY descendingly.
Unify Cmp with -1, 0, or 1, depending on whether ValueX is greater than, equal to, or smaller than, ValueY.
Arguments:
ValueX- The first value to compare
ValueY- The second value to compare
Cmp- The result of the comparison