Select sorting-Python and PHP implementation version, and sorting-pythonphp implementation

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.