How to select and sort using php _ PHP Tutorial

Source: Internet
Author: User
Use php to solve the problem of selecting and sorting. 1. Definition: Selectionsort is a simple and intuitive sorting algorithm. It works as follows. First, find the smallest (large) element in the unordered sequence and store it in the sorting order 1. Definition: Selection sort is a simple and intuitive sorting algorithm. It works as follows. First, find the smallest (large) element in the unordered sequence, Store to the starting position of the sorting sequenceThen, find the smallest (large) element from the remaining unordered elements and put it at the end of the sorted sequence. And so on until all elements are sorted.

Reference code:

The code is as follows:


// Selection sort is a simple and intuitive sorting algorithm. It works as follows. First, find the smallest (large) element in the unordered sequence, store it to the starting position of the sorting sequence, and then continue searching for the smallest (large) element from the remaining unordered elements, put it at the end of the sorted sequence. And so on until all elements are sorted.

Function selectSort (& $ arr ){
// Define the variable for exchange
$ Temp = 0;
For ($ I = 0; $ I
// Assume $ I is the minimum value.
$ Valmin = $ arr [$ I];
// Subscript of the minimum record value
$ Minkey = $ I;
For ($ j = $ I + 1; $ j
// The minimum value is greater than the subsequent number.
If ($ valmin> $ arr [$ j]) {
$ Valmin = $ arr [$ j];
$ Minkey = $ j;
}
}
// Exchange
$ Temp = $ arr [$ I];
$ Arr [$ I] = $ arr [$ minkey];
$ Arr [$ minkey] = $ temp;
}
}

$ Arr = array (,-1 );
SelectSort ($ arr );
Print_r ($ arr );

?>

Selection sort is a simple and intuitive sorting algorithm. It works as follows. First, find the smallest (large) element in the unordered sequence and store it in the sorting order...

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.