Quick sorting (QuickSort) randomly select an element (called pivot) and position it in its final place, swapping all the elements so that those below the pivot are to its left and those above the pivot to its right.
The operation is called partitioning. For each sub-list (or sub-table), a new pivot is randomly selected and the partitioning operation is repeated. This process is repeated recursively, until all items are sorted.
The partitioning consists of:
- swap the pivot with the last element of the subtable
- place at the top of the sub-table the elements below the pivot
- move the pivot to the right of the last item moved at the beginning of the table (the other elements being higher than the pivot).
from random import randint #Choix of a pivot randomly #128mots.com def choix_pivot (table, first: int,last: int): pivot - randint (first, last) print ("The unsorted sub-list is: " - str(table[premier:dernier+1])) print ("The pivot chosen aleatially is the index " str (pivot) - "value" - str (table[pivot])) pivot return #Fonction to partition the board #Permutation of all the elements so that those that are below the pivot to his left and all those who are superior to the pivot #soient to his right. #128mots.com def partition (table, beginning: int, end: int, pivot: int): #Etape 1: The pivot is positioned at the end of the sublist arbitrarily #On swaps the pivot and the last element print ("Partitioning of the sub-list " - str(table[debut:fin+1])) print ("---Placement from pivot to end sublist") permute (table, pivot, end) print ("---") j - start #indice of progress at the beginning of the sub-list #Pour i from the beginning of the sublist to the end for i in range (debut,fin): #Tous the lower-pivot elements are placed at the beginning of the sub-list if (tab<= [i]tab):></= ta[fin]b):> #Si the value is lower we move at the beginning of the table #Et that the value is not already well placed so we move to the beginning of the table permute (table,i,j) J.1 #On puts the pivot in the right place by swapping the item after the last found as below the pivot print ("permutation of the pivot") permute (table, fine, j) #on returns j's position which is then the new position of the pivot in the table return j #Fonction swapping two elements of a table #128mots.com def permute (table, index1: int, index2:int): print ("Permutation of the element" - str (index1) - "with element" - str (index2)) print(" Before permutation: " - str(tab)) globalcompteur_permutation if (index1!2): #on back up the value of Index 1 in a temporary variable tmp - table[indice1] table[indice1] - table[indice2] table[indice2] - tmp compteur_permutation 1 print(" After swapping: " - str(tab)) #Fonction of rapid sorting of a sub-list of a table #128mots.com def tri_rapide (table,debut: int,fin: int): If you have a sub-list that contains at least 2 items If debut< fin : fin=""></ fin :> #on chooses a pivot in the sub-list pivot - choix_pivot (table, beginning, end) #on moves all the lower elements to the pivot to the left of the pivot pivot - partition (table, beginning, fine, pivot) #recursion to redo the algorithm on the sublist to the left of the found pivot tri_rapide (table,debut,pivot - 1) #recursion to redo the algorithm on the sublist to the right of the found pivot tri_rapide (table, pivot - 1,fin) compteur_permutation 0 tab[111, 34, 22, 55, 4, 2, 1, 77] tri_rapide (tab,0,len(tab)-1) print ("final result of sorting: " - str(tab)) print ("number of permutations: " - str(compteur_permutation))
The release of the program is as follows:
The unsorted sub-list is:[111, 34, 22, 55, 4, 2, 1, 77] The pivot chosen aleatially is the index 6 of value 1 Partitioning of the sub-list[111, 34, 22, 55, 4, 2, 1, 77] ---Placement from pivot to end sublist Swap element 6 with element 7 Before permutation:[111, 34, 22, 55, 4, 2, 1, 77] After swapping:[111, 34, 22, 55, 4, 2, 77, 1] --- Permutation of the pivot Swap element 7 with element 0 Before permutation: After swapping: The unsorted sub-list is: The pivot chosen aleatially is the 5-value index Partitioning of the sub-list ---Placement from pivot to end sublist Swap element 5 with element 7 Before permutation: After swapping: --- Permutation of the pivot Swap element 7 with element 1 Before permutation:[1, 34, 22, 55, 4, 111, 77, 2] After swapping:[1, 2, 22, 55, 4, 111, 77, 34] The unsorted sub-list is:[22, 55, 4, 111, 77, 34] The pivot chosen aleatially is the 4-value index Partitioning of the sub-list[22, 55, 4, 111, 77, 34] ---Placement from pivot to end sublist Swap pingofing element 4 with element 7 Before permutation: After swapping: --- Permutation of the pivot Swap element 7 with element 2 Before permutation: After swapping: The unsorted sub-list is: The pivot chosen aleatially is the index 4 of value -34 Partitioning of the sub-list ---Placement from pivot to end sublist Swap pingofing element 4 with element 7 Before permutation: After swapping: --- Swap element 4 with element 3 Before permutation:[1, 2, 4, 55, 22, 111, 77, 34] After swapping:[1, 2, 4, 22, 55, 111, 77, 34] Permutation of the pivot Swap element 7 with element 4 Before permutation: After swapping: The unsorted sub-list is:[111, 77, 55] The pivot chosen aleatially is the 5 index of value -111 Partitioning of the sub-list[111, 77, 55] ---Placement from pivot to end sublist Swap element 5 with element 7 Before permutation: After swapping: --- Swap element 5 with element 5 Before permutation: After swapping: Swap element 6 with element 6 Before permutation: After swapping: Permutation of the pivot Swap element 7 with element 7 Before permutation:[1, 2, 4, 22, 34, 55, 77, 111] After swapping:[1, 2, 4, 22, 34, 55, 77, 111] The unsorted sub-list is:[55, 77] The pivot chosen aleatially is the index 6 of value '77 Partitioning of the sub-list[55, 77] ---Placement from pivot to end sublist Swap element 6 with element 6 Before permutation: After swapping: --- Swap element 5 with element 5 Before permutation: After swapping: Permutation of the pivot Swap element 6 with element 6 Before permutation: After swapping: final result of sorting:[1, 2, 4, 22, 34, 55, 77, 111] permutation number: 10
The number of permutation switching is 10 for a table of 8 elements.
The average complexity of rapid sorting is 7.22 for a table of 8 elements.