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...