
- Prolog - Home
- Prolog - Introduction
- Prolog - Environment Setup
- Prolog - Hello World
- Prolog - Basics
- Prolog - Relations
- Prolog - Data Objects
- Loop & Decision Making
- Conjunctions & Disjunctions
Prolog Operators
- Prolog - Type of Operators
- Prolog - Arithmetic Comparison Operators
- Prolog - Unification Operators
- Prolog - Term Comparision Operators
- Prolog - Arithmetic Operators
- Prolog - Logical Operators
- Prolog - List Operators
- Prolog - Custom Operators
Prolog Lists
- Prolog - Lists
- Prolog - Member of List
- Prolog - Length of List
- Prolog - Concatenating Lists
- Prolog - Appending to a List
- Prolog - Deleting from a List
- Prolog - Inserting into a List
- Prolog - Permutation Operation
- Prolog - Combination Operation
- Prolog - Reverse Items of a List
- Prolog - Shift Items of a List
- Prolog - Check Order of a List
- Prolog - SubSet of a Set
- Prolog - Union of Sets
- Prolog - Intersection of Sets
- Prolog - Even and Odd Length Finding
- Prolog - Divide a List
- Prolog - Find Maximum of a List
- Prolog - Find Minimum of a List
- Prolog - Find Sum of a List
- Prolog - Sorting List using MergeSort
Built-In Predicates
- Prolog - Built-In Predicates
- Prolog - Identifying Terms
- Prolog - Decomposing Structures
- Prolog - Collecting All
- Prolog - Mathematical Predicates
- Prolog - Scientific Predicates
Miscellaneous
- Recursion and Structures
- Prolog - Backtracking
- Prolog - Preventing Backtracking
- Prolog - Different and Not
- Prolog - Inputs and Outputs
- Tree Data Structure (Case Study)
- Prolog - Examples
- Prolog - Basic Programs
- Prolog - Practical Arithmetic Examples
- Prolog - Examples of Cuts
- Towers of Hanoi Problem
- Prolog - Linked Lists
- Monkey and Banana Problem
- Prolog Useful Resources
- Prolog - Quick Guide
- Prolog - Useful Resources
- Prolog - Discussion
Prolog - Sorting List using MergeSort
In this example, we will demonstrating the sorting of a List using mergesort algorithm in Prolog.
If the list is [4,5,3,7,8,1,2], then the result will be [1,2,3,4,5,7,8]. The steps of performing merge sort are shown below −
Take the list and split them into two sub-lists. This split will be performed recursively.
Merge each split in sorted order.
Thus the entire list will be sorted.
We will define a predicate called mergesort(L, SL), it will take L and return result into SL.
Program (list_misc.pl)
mergesort([],[]). mergesort([A],[A]). mergesort([A,B|R],S) :- split([A,B|R],L1,L2), mergesort(L1,S1), mergesort(L2,S2), merge(S1,S2,S). split([],[],[]). split([A],[A],[]). split([A,B|R],[A|Ra],[B|Rb]) :- split(R,Ra,Rb). merge(A,[],A). merge([],B,B). merge([A|Ra],[B|Rb],[A|M]) :- A =< B, merge(Ra,[B|Rb],M). merge([A|Ra],[B|Rb],[B|M]) :- A > B, merge([A|Ra],Rb,M).
Output
| ?- consult('D:/TP Prolog/Sample Codes/list_misc.pl'). compiling D:/TP Prolog/Sample Codes/list_misc.pl for byte code... D:/TP Prolog/Sample Codes/list_misc.pl compiled, 8 lines read - 3045 bytes written, 5 ms yes | ?- mergesort([4,5,3,7,8,1,2],L). L = [1,2,3,4,5,7,8] ? yes | ?- mergesort([8,5,3,4,7,9,6,1],L). L = [1,3,4,5,6,7,8,9] ? yes | ?-
Explanation
split predicate
split([],[],[]) is the base case where an empty list is split into two empty lists.
split([A],[A],[]) is the base case where a one element list is split into two lists having one list with one element and another one is empty.
split([A,B|R],[A|Ra],[B|Rb]) :- split(R,Ra,Rb). is the recursive call where a lsit is split into two approximately equal lists.
merge predicate
merge(A,[],A). is the base case where one element list is merged with an empty list.
merge([],B,B) is the base case where an empty list is merged with one element list.
merge([A|Ra],[B|Rb],[A|M]) :- A =< B, merge(Ra,[B|Rb],M)) checks Head element, A of first list with Head element, B of second list. If A is less than or same as B then merge the Rest of the first List with second List and return the merged list.
merge([A|Ra],[B|Rb],[B|M]) :- A > B, merge([A|Ra],Rb,M)) checks Head element, A of first list with Head element, B of second list. If A is greater than B then merge the first list with the Rest of the second List and return the merged list.
mergesort predicate
mergesort([],[]) is the base case where an empty list is sorted and returned as such.
mergesort([A],[A]) is the base case where an list of one element is sorted and returned as such.
-
mergesort([A,B|R],S) :- , , mergesort(L2,S2), merge(S1,S2,S) sorts the first List using merge sort and return the sorted list as second list.
mergesort([A,B|R],S) is to divide the list where Head, Y represents the first element of the list and Tail represents the rest of the list. Sum is the summation of elements of the list.
split([A,B|R],L1,L2) calculates the sum of rest of the elements and store than in SumTemp.
mergesort(L1,S1) evaluates and assign addition of Head and SumTemp and returns the sum of all elements in turn.
mergesort(L2,S2) evaluates and assign addition of Head and SumTemp and returns the sum of all elements in turn.
mergesort(S1,S2,S) evaluates and assign addition of Head and SumTemp and returns the sum of all elements in turn.