Simple sorting algorithm java sorting algorithm summary heap sorting calculation

Source: Internet
Author: User
Simple sorting algorithm: & lt ;? Php $ arrarray (28,112, 20,434, 65,765, 33,553 $ Arr = array (
12,
45,
89,
3,
24,
55,
223,
76,
22,
11,
28,
112,
20,
434,
23,
65,
65,
765,
6,
8,
23,
5,
33,
553,
45,
423,
64,
77,
84,
23
);
/**
* Bubble sorting algorithm, with a time complexity of n2/2 times
* The basic concept of bubble sorting is to compare two adjacent numbers in sequence, put the decimal places in front, and put the large numbers in the back. That is, in the first step: first compare the number of 1st and the number of 2nd, and put the decimal number before and after the large number.
* Then compare the numbers of 2nd and 3rd. Place the decimal places before and after the large number, and continue until the last two digits are compared. Place the decimal places before and after the large number. So far, the first stop is over, and the maximum number is placed to the bottom.
* In the second round: The comparison starts from the first logarithm (because the number of 2nd is exchanged with the number of 3rd, the number of 1st is no less than 2nd). before placing the decimal number, after a large number is placed, it is always compared to the second number at the bottom.
* (The penultimate position is already the largest), and the second stop, get a new maximum number at the penultimate position (in fact, it is the second largest number in the entire series ). Repeat the above process,
* Until the sorting is completed.
*/
Function maopao_sort ($ arr)
{
$ Count = count ($ arr );
$ Tmp;
$ M = 0; // calculate the number of executions
For ($ I = 0; $ I <$ count-1; $ I ++ ){
For ($ j = 0; $ j <$ count-1-$ I; $ j ++ ){
If ($ arr [$ j]> $ arr [$ j + 1]) {
$ Tmp = $ arr [$ j];
$ Arr [$ j] = $ arr [$ j + 1];
$ Arr [$ j + 1] = $ tmp;
}
$ M ++;
}
}
Print_r ($ arr );
Echo $ m;
}
/**
* Select the sorting algorithm. The time complexity is n2/2 times.
* 1. find the element with the smallest keyword from the sequence to be sorted;
* 2. if the minimum element is not the first element of the sequence to be sorted, swap it with the first element;
* 3. Find the smallest keyword element from the remaining N-1 elements, repeat steps (1) and (2) until the sorting ends.
*/
Function select_sort ($ arr)
{
$ Count = count ($ arr );
$ Tmp;
$ M = 0; // calculate the number of executions
For ($ I = 0; $ I <$ count-1; $ I ++ ){
$ P = $ I;
For ($ j = $ I + 1; $ j <$ count; $ j ++ ){
If ($ arr [$ p]> $ arr [$ j]) {
$ P = $ j;
}
$ M ++;
}
If ($ p! = $ I ){
$ Tmp = $ arr [$ p];
$ Arr [$ p] = $ arr [$ I];
$ Arr [$ I] = $ tmp;
}
}
Print_r ($ arr );
Echo $ m;
}
/**
* Insert the sorting algorithm. The time complexity is n2/2 times.
* Insertion sorting means that each step inserts a data to be sorted to an appropriate position in the sorted data according to its size until all data is inserted.
*/
Function insert_sort ($ arr)
{
$ Count = count ($ arr );
$ Tmp;
$ M = 0; // calculate the number of executions
For ($ I = 1; $ I <$ count; $ I ++ ){
$ Tmp = $ arr [$ I];
For ($ j = $ I-1; $ j> = 0; $ j --){
If ($ tmp <$ arr [$ j]) {
$ Arr [$ j + 1] = $ arr [$ j];
$ Arr [$ j] = $ tmp;
} Else {
Break;
}
$ M ++;
}
}
Print_r ($ arr );
Echo $ m;
}
/**
* Fast sorting algorithm with a time complexity of n2/2 times
* The basic idea of this method is:
* 1. first, extract a number from the sequence as the reference number.
* 2. in the partitioning process, place all the numbers greater than this number to its right, and all the numbers smaller than or equal to it to its left.
* 3. repeat the second step for the left and right intervals until each interval has only one number.
*/
Function quick_sort ($ arr)
{
$ Count = count ($ arr );
If ($ count <= 1 ){
Return $ arr;
}
$ Tmp = $ arr [0];
$ Left_array = array ();
$ Right_array = array ();

For ($ I = 1; $ I <$ count; $ I ++ ){
If ($ arr [$ I] <= $ tmp ){
$ Left_array [] = $ arr [$ I];
} Else {
$ Right_array [] = $ arr [$ I];
}
$ M ++;
}

$ Left_array = quick_sort ($ left_array );
$ Right_array = quick_sort ($ right_array );
Return array_merge ($ left_array, array (
$ Tmp
), $ Right_array );
}
// Print_r (quick_sort ($ arr ));
/// Print_r (quickSort ($ arr ));
Function quickpaixu ($ arr)
{
$ Count = count ($ arr );
If ($ count <= 1 ){
Return $ arr;
}
$ Key = $ arr [0]; // Obtain a value for comparison later;
$ Left_arr = array ();
$ Right_arr = array ();
For ($ I = 1; $ I <$ count; $ I ++) {// place a value greater than $ key on the right and a small value on the left;
If ($ arr [$ I] <= $ key ){
$ Left_arr [] = $ arr [$ I];
} Else {
$ Right_arr [] = $ arr [$ I];
}
}
$ Left_arr = quickpaixu ($ left_arr); // recursive;
$ Right_arr = quickpaixu ($ right_arr );
Return array_merge ($ left_arr, array (
$ Key
), $ Right_arr); // combines the left and right values into an array;
} // The following is the test
// Print_r (quickpaixu ($ arr ));
?>

The above introduces simple sorting algorithms, including sorting algorithms and simple content. I hope my friends who are interested in PHP tutorials will be helpful.

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.