// Insert sorting (one-dimensional array)
// The basic idea of insertion sort is to insert a record to be sorted into the appropriate position in the subfile in the preceding sorted order according to the keyword size, until all records are inserted.
Function insert_sort ($ ARR ){
$ COUNT = count ($ ARR );
For ($ I = 1; $ I <$ count; $ I ++ ){
$ TEM = $ arr [$ I]; // obtain the current value
$ J = $ I-1; // gets the previous position of the current value
While ($ arr [$ J]> $ TEM) {// if the current value is smaller than the previous one, it is not switched to the starting position of the array.
$ Arr [$ J + 1] = $ arr [$ J]; // put the previous value one by one
$ Arr [$ J] = $ TEM;
$ J --; // decrease position
}
}
Return $ arr;
}
$ Array = array (1000 );
Print_r (insert_sort ($ array); // array (1, 2, 3, 4, 6, 8, 5340)
// Select sorting
// The basic principle of selection sort is to select the records with the smallest keyword from the records to be sorted, and put the records at the end of the sorted subfile, until all records are sorted.
Function select_sort ($ ARR ){
$ COUNT = count ($ ARR); // obtain the total length of the array.
For ($ I = 0; $ I <$ count; $ I ++) {// traverses the Array
$ K = $ I; // Save the current location
For ($ J = $ I + 1; $ j <$ count; $ J ++) {// traverses data after the current position
If ($ arr [$ K]> $ arr [$ J]) // if any of
$ K = $ J; // temporary storage location
If ($ K! = $ I ){
$ TMP = $ arr [$ I]; // minimum value for temporary storage
$ Arr [$ I] = $ arr [$ K]; // place the current value in the calculated position.
$ Arr [$ K] = $ TMP; // Replace the current value with the calculated value.
}
}
}
Return $ arr;
}
$ Array = array (1000 );
Print_r (select_sort ($ array); // array (1, 2, 3, 4, 6, 8, 5340)
// Bubble sort
// The basic idea of Bubble Sorting is to compare the keywords of the records to be sorted in two pairs. If the order of the two records is the opposite, the two records are exchanged until there is no reverse order record.
Function bubble_sort ($ ARR ){
$ COUNT = count ($ ARR); // obtain the total length of the array.
If ($ count <= 0) return false;
For ($ I = 0; $ I <$ count; $ I ++) {// forward traversal Array
For ($ J = $ count-1; $ j> $ I; $ j --) {// forward traversal Array
If ($ arr [$ J] <$ arr [$ J-1]) {// compare two adjacent numbers
$ TMP = $ arr [$ J]; // small number of records saved
$ Arr [$ J] = $ arr [$ J-1]; // put the larger front
$ Arr [$ J-1] = $ TMP; // small put back
}
}
}
Return $ arr;
}
$ Array = array (1000 );
Print_r (bubble_sort ($ array); // array (1, 2, 3, 4, 6, 8, 5340)
// Quick sorting
// Fast sorting is essentially the same as Bubble sorting. It is an application of exchange sorting. So the basic idea is the same as the above Bubble sorting.
Function quick_sort ($ array ){
If (count ($ array) <= 1) return $ array;
$ Key = $ array [0];
$ Left_arr = array ();
$ Right_arr = array ();
For ($ I = 1; $ I <count ($ array); $ I ++) {// traverse the array from the second index
If ($ array [$ I] <= $ key) // if the value is less than Index 1
$ Left_arr [] = $ array [$ I]; // load the left index array (Data smaller than Index 1)
Else
$ Right_arr [] = $ array [$ I]; // otherwise, load the right index (data greater than Index 1)
}
$ Left_arr = quick_sort ($ left_arr );
$ Right_arr = quick_sort ($ right_arr );
Return array_merge ($ left_arr, array ($ Key), $ right_arr );
}
$ Array = array (1000 );
Print_r (quick_sort ($ array); // array (1, 2, 3, 4, 6, 8, 5340)
// Sort by exchange method
Function exchangesort ($ ARR ){
$ Num = count ($ ARR );
For ($ I = 0; $ I <$ num-1; $ I ++ ){
For ($ J = $ I + 1; $ j <$ num; $ J ++ ){
If ($ arr [$ J] <$ arr [$ I]) {
$ Itemp = $ arr [$ I];
$ Arr [$ I] = $ arr [$ J];
$ Arr [$ J] = $ itemp;
}
}
}
Return $ arr;
}
$ Array = array );
Print_r (exchangesort ($ array); // array (1, 2, 3, 4, 6, 8, 5340
[Technical House 2] selection of bubble insertion and quick exchange Sorting Algorithm