How to resolve the algorithm Sorting algorithms/Bogosort step by step in the Common Lisp programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sorting algorithms/Bogosort step by step in the Common Lisp 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 Common Lisp programming language
Source code in the common programming language
(defun nshuffle (sequence)
(loop for i from (length sequence) downto 2
do (rotatef (elt sequence (random i))
(elt sequence (1- i ))))
sequence)
(defun sortedp (list predicate)
(every predicate list (rest list)))
(defun bogosort (list predicate)
(do ((list list (nshuffle list)))
((sortedp list predicate) list)))
You may also check:How to resolve the algorithm Stirling numbers of the first kind step by step in the REXX programming language
You may also check:How to resolve the algorithm Twin primes step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Phrase reversals step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Polyspiral step by step in the Scala programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Fish programming language