There are several sorting algorithms, but the main among them is the Quick Sort.
It consists of defining one of the elements of the list as a pivot element. In the example below, it is considered the element 3 as pivot, being represented by the black risk. The purpose is to organize all the elements that are inferior to the pivot to your left and all those that are superior to your right. Thus, at the end, the pivot element will already be in its final position. After, the same logic is executed recursively for the elements on the left and for the elements on the right, defining new pivot elements.
The executed logic is as follows:
From the end to the beginning, the first element is checked to the right of the pivot by exchanging its positions. In this case, the first item below 3 is 1.
From the beginning to the end, the first element that is superior to it is verified at the left of the pivot, exchanging its positions. In this case, the first item exceeding 3 is 5.
Repeat the check of step 1, finding the value 2, swapping the positions again.
Repeat the check of step 2, finding the value 4, swapping the positions again.
The pivot element will be in its final position, then repeat the algorithm for the vectors [1, 2] and [4, 5] separately, until the vector is completely ordered.
Quick Sort in Python
def quick_sort(vector):
# Se o vetor tiver comprimento 1, terminou a ordenação:
if len(vector) <= 1:
return vector
else:
return quick_sort([x for x in vector[1:] if x < vector[0]]) + \ # Ordena o vetor a esquerda
[vector[0]] + \ # Elemento pivô na posição final
quick_sort([x for x in vector[1:] if x>=vector[0]]) # Ordena o vetor a direita
See working on Repl.it
Minimum of permutations
Given that the question originated from a test/proof, I believe that the prior knowledge about the sorting algorithms was expected, thus, to answer, I can take as reference that the Quick Sort is already the most efficient algorithm and the one that requires less permutations during sorting, so I just calculate the number of permutations that such an algorithm will do to determine the minimum possible.
Considering a generic vector, the worst case occurs when the pivot is the largest or smallest element and the vector is previously ordered inversely. That is, the worst case occurs when the sub-vectors are unbalanced: one with size n-1
and another 0, being n
the size of the vector. Therefore, the number of comparisons and permutations made in each sub-vector will be n-1
for the left vector and 0
from the right. In the second iteration, the vectors will have sizes n-2
and 1, so there will be n-2
comparisons to the total. So it continues until the vector is ordered, so the total of comparisons/permutations will be equal to:
The best case occurs when the pivot element divides the vector into equal sizes, with n/2
comparisons on each sub-vector, so the total is equal to:
Sources:
In some specific language?
– user28595
In any language
– Thomas E.
Using pointers solves your problem
– R.Santos