Select sorting-Python and PHP implementation version, and sorting-pythonphp implementation
Select sort Python implementation
Import random # generate the array to be sorted a = [random. randint (1,999) for x in range ()] # select sort def selectionSort (array): I = 0 while I <len (array)-1: min = I j = I + 1 while j <len (array): if array [min]> array [j]: min = j + 1 if I! = Min: array [I], array [min] = array [min], array [I] I = I + 1 # process the array selectionSort (a) using the selected sorting method) print ()
PHP implementation of quick sorting
<? Php // generate the array to be sorted $ a = []; for ($ I = 0; $ I <36; $ I ++) {array_push ($, mt_rand (1,999);} shuffle ($ ); /*** select the sorting algorithm selection sort * @ param [type] a address of the array to be sorted * @ return null */function selectionSort (& $ a) {$ min = 0; $ len = count ($ a); for ($ I = 0; $ I <$ len-1; $ I ++) {$ min = $ I; // search for elements smaller than $ a [$ I] in the array elements marked after $ I for ($ j = $ I + 1; $ j <$ len; $ j ++) {// If an element smaller than $ a [$ I] is found, record the subscript of this element if ($ a [$ min]> $ a [$ j]) {$ min = $ j ;}} // If an element smaller than $ a [$ I] is found If ($ min! = $ I) {$ a [$ I] + = $ a [$ min]; $ a [$ min] = $ a [$ I]-$ a [$ min]; $ a [$ I]-= $ a [$ min] ;}} selectionSort ($ a); var_dump ($ );
Description of sorting time complexity
Number of comparisons: n (n-1)/2
Number of exchanges: The best time is 0; the worst time is n-1.
Total time complexity: O (n2)
Although the time complexity of sorting and Bubble Sorting is the same as that of O (n2), the performance is slightly better than that of fake sorting.