C#: Bogosort
Write a C# Sharp program to sort a list of elements using Bogosort sort.
In computer science, bogosort is a particularly ineffective sorting algorithm based on the generate and test paradigm. The algorithm successively generates permutations of its input until it finds one that is sorted. It is not useful for sorting, but may be used for educational purposes, to contrast it with other more realistic algorithms.
Two versions of the algorithm exist: a deterministic version that enumerates all permutations until it hits a sorted one, and a randomized version that randomly permutes its input. An analogy for the working of the latter version is to sort a deck of cards by throwing the deck into the air, picking the cards up at random, and repeating the process until the deck is sorted. Its name comes from the word bogus.
Sample Solution:
C# Sharp Code:
Sample Output:
Sorting... Bogo_sort iteration 0: 2 1 3 0 Bogo_sort iteration 1: 3 1 0 2 Bogo_sort iteration 2: 1 2 3 0 Bogo_sort iteration 3: 0 2 3 1 Bogo_sort iteration 4: 2 1 0 3 Bogo_sort iteration 5: 2 0 3 1 Bogo_sort iteration 6: 3 1 2 0 Bogo_sort iteration 7: 1 3 0 2 Bogo_sort iteration 8: 2 0 1 3 Bogo_sort iteration 9: 1 2 3 0 Bogo_sort iteration 10: 1 0 2 3 ---- Bogo_sort iteration 38: 1 0 3 2 Bogo_sort iteration 39: 0 1 2 3 Bogo_sort completed after 39 iterations. Press any key to exit.
Flowchart:
C# Sharp Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C# Sharp program to sort a list of elements using Shell sort.
Next: Write a C# Sharp program to sort a list of elements using Bubble sort.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics