मैं सोच रहा हूँ कि क्या किसी सरणी के "क्रमबद्धता" को मापने का एक मानक तरीका है? एक सरणी जो संभव व्युत्क्रमों की औसत संख्या है, को अधिकतम रूप से अनसोल्ड माना जाएगा? मेरा मतलब है कि यह मूल रूप से जहाँ तक संभव हो या तो छँटा हुआ या उल्टा छँटा हुआ है।
मैं सोच रहा हूँ कि क्या किसी सरणी के "क्रमबद्धता" को मापने का एक मानक तरीका है? एक सरणी जो संभव व्युत्क्रमों की औसत संख्या है, को अधिकतम रूप से अनसोल्ड माना जाएगा? मेरा मतलब है कि यह मूल रूप से जहाँ तक संभव हो या तो छँटा हुआ या उल्टा छँटा हुआ है।
जवाबों:
नहीं, यह आपके आवेदन पर निर्भर करता है। छँटाई के उपायों को अक्सर विकार के उपायों के रूप में संदर्भित किया जाता है , जो से आर , जहां एन < एन से कार्य हैं अलग-अलग nonnegative पूर्णांकों के सभी परिमित अनुक्रमों का संग्रह है। एस्टिविल-कास्त्रो और वुड द्वारा सर्वेक्षण [1] अनुकूली सॉर्ट एल्गोरिदम के संदर्भ में विकार के 11 विभिन्न उपायों को सूचीबद्ध करता है और उनकी चर्चा करता है।
आक्रमण की संख्या कुछ मामलों के लिए काम कर सकती है, लेकिन कभी-कभी अपर्याप्त होती है। [1] में दिया गया एक उदाहरण अनुक्रम है
that has a quadratic number of inversions, but only consists of two ascending runs. It is nearly sorted, but this is not captured by inversions.
Mannila [1] axiomatizes presortedness (with a focus on comparison-based algorithms) as follows (paraphrasing).
Let a totally ordered set. Then a mapping from (the sequences of distinct elements from ) to the naturals is a measure of presortedness if it satisfies below conditions.
If is sorted then .
If with , and for all , then .
If is a subsequence of , then .
If for all and for some , then .
for all and .
Examples of such measures are the
Note that random distributions using these measures have been defined, i.e. such that make sequences that are more/less sorted more or less likely. These are called Ewens-like distributions [2, Ch. 4-5; 3, Example 12; 4], a special case of which is the so-called Mallows distribution. The weights are parametric in a constant and fulfill
.
Note how defines the uniform distribution (for all ).
Since it is possible to sample permutations w.r.t. these measures efficiently, this body of work can be useful in practice when benchmarking sorting algorithms.
I have my own definition of "sortedness" of a sequence.
Given any sequence [a,b,c,…] we compare it with the sorted sequence containing the same elements, count number of matches and divide it by the number of elements in the sequence.
For example, given sequence [5,1,2,3,4]
we proceed as follows:
1) sort the sequence: [1,2,3,4,5]
2) compare the sorted sequence with the original by moving it one position at a time and counting the maximal number of matches:
[5,1,2,3,4]
[1,2,3,4,5] one match
[5,1,2,3,4]
[1,2,3,4,5] no matches
[5,1,2,3,4]
[1,2,3,4,5] no matches
[5,1,2,3,4]
[1,2,3,4,5] no matches
[5,1,2,3,4]
[1,2,3,4,5] no matches
[5,1,2,3,4]
[1,2,3,4,5] 4 matches
[5,1,2,3,4]
[1,2,3,4,5] no matches
...
[5,1,2,3,4]
[1,2,3,4,5] no matches
3) The maximal number of matches is 4, we can calculate the "sortedness" as 4/5 = 0.8.
Sortedness of a sorted sequence would be 1, and sortedness of a sequence with elements placed in reversed order would be 1/n.
The idea behind this definition is to estimate the minimal amount of work we would need to do to convert any sequence to the sorted sequence. In the example above we need to move just one element, the 5 (there are many ways, but moving 5 is the most efficient). When the elements would be placed in reversed order, we would need to move 4 elements. And when the sequence were sorted, no work is needed.
I hope my definition makes sense.
If you need something quick and dirty (summation signs scare me) I wrote a super easy disorder function in C++ for a Class named Array which generates int arrays filled with randomly generated numbers:
void Array::disorder() {
double disorderValue = 0;
int counter = this->arraySize;
for (int n = 0; n < this->arraySize; n++) {
disorderValue += abs(((n + 1) - array[n]));
// cout << "disorderValue variable test value = " << disorderValue << endl;
counter++;
}
cout << "Disorder Value = " << (disorderValue / this->arraySize) / (this->arraySize / 2) << "\n" << endl;
}
Function simply compares the value in each element to the index of the element + 1 so that an array in reverse order has a disorder value of 1, and a sorted array has a disorder value of 0. Not sophisticated, but working.
Michael