Function to make a list as unsorted as possible
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In computational theory and practical applications, sorting algorithms are extensively studied, understood, and implemented. However, there are scenarios where one might desire the opposite - making a list as unsorted or disordered as possible. Whether it's for testing sorting algorithms' efficiency or for creating a randomized dataset, understanding how to unsort a list can be valuable. This operation does not seek to merely shuffle the list but to ensure that the list’s order is significantly different from its sorted variant.
Theoretical Basis
The concept of making a list unsorted can be abstractly understood in terms of chaos theory and entropy. A sorted list can be seen as a state of low entropy, where there's minimal disorder. Conversely, an unsorted list reaches a higher state of entropy, characterized by maximum disorder.
A formal measure of disorder in a list is "inversions." An inversion in a list is a pair of elements where the earlier element is greater than the later one in a considered sequence. The goal of unsorting a list is to maximize these inversions.
Technical Explanation
One naive approach to achieve an unsorted list could be to randomly shuffle the list using a random permutation. However, we seek more randomness to enhance the disorder quantitatively. An effective way to exceed just simple randomness is known as the "Fisher-Yates shuffle" or "Knuth shuffle."
Fisher-Yates Shuffle Algorithm
The Fisher-Yates algorithm shuffles a list in-place and achieves a uniform distribution of possible random permutations:
- Initialize: Given an array `arr` of size `n`.
- Iterate: For each element `n-1` to `1`, do:
- Pick a random index `j` where `0 ≤ j ≤ i`.
- Swap elements at indices `i` and `j`.
- Performance: Fisher-Yates shuffle runs in time complexity, making it efficient for large datasets. Given its in-place algorithm nature, it also optimizes space.
- True Randomness: Ensure the random number generator for permutations is seeded appropriately if reproducibility is desired.
- Entropy Measurement: As a measure of success in unordering, calculate the normalized version of the permutation entropy to validate the randomness.1 for i in range(n-1) for j in range(i+1, n) if arr[i] > arr[j]
Related reading
- Fuzzy date algorithm
- Fuzzy Date algorithm in Objective-C
- Fuzzy search algorithm approximate string matching algorithm
- Game on the tree, cutting branch
- General approach to developing an image classification algorithm for Dilbert cartoons
- Generalizing the algorithm for domino tiling?
- Generate a large random planar graph
- Generate a list of primes up to a certain number

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.