How to resolve the algorithm Sorting algorithms/Bogosort step by step in the Prolog programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sorting algorithms/Bogosort step by step in the Prolog programming language
Table of Contents
Problem Statement
Bogosort a list of numbers.
Bogosort simply shuffles a collection randomly until it is sorted.
"Bogosort" is a perversely inefficient algorithm only used as an in-joke.
Its average run-time is O(n!) because the chance that any given shuffle of a set will end up in sorted order is about one in n factorial, and the worst case is infinite since there's no guarantee that a random shuffling will ever produce a sorted sequence.
Its best case is O(n) since a single pass through the elements may suffice to order them.
Pseudocode:
The Knuth shuffle may be used to implement the shuffle part of this algorithm.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sorting algorithms/Bogosort step by step in the Prolog programming language
Source code in the prolog programming language
bogo_sort(L,Rl) :-
min_list(L,Min),
repeat,
random_permutation(L,Rl),
is_sorted(Rl,Min),
!.
is_sorted([],_).
is_sorted([N|T],P) :-
N >= P,
is_sorted(T,N).
You may also check:How to resolve the algorithm Element-wise operations step by step in the jq programming language
You may also check:How to resolve the algorithm Tree traversal step by step in the Ursala programming language
You may also check:How to resolve the algorithm HTTP step by step in the zkl programming language
You may also check:How to resolve the algorithm Append a record to the end of a text file step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Unicode variable names step by step in the Kotlin programming language