Php implements bubble sorting, select sorting, insert sorting, and quick sorting. fast sorting: c language quick sorting algorithm c language

Source: Internet
Author: User
Quick sorting, insert sorting: php implements bubble sorting, select sorting, insert sorting, and quick sorting: four basic sorting methods in C language were read when I learned data structure by myself, I forgot about the C language. I recently used php to re-write the four sorting methods and review the increasingly unfamiliar algorithms. Directly paste the code .; Print_r ($ var); echo;} $ arrarray, I recently used php to re-write the four sorting methods and review the increasingly unfamiliar algorithms. Directly paste the code.

 "; Print_r ($ var); echo"
";}$ Arr = array (, 77); printf (" ** original array ** "); p ($ arr ); /*** bubble sort method ** @ param $ arr sort array * train of thought: compare it with adjacent numbers. if the left side is larger than the right side, the switch position is displayed. * Two nodes in one direction: the number of cycles twice, and the bubble direction (that is, the initial value and termination condition of $ j) * num is optimized, once the loop is not switched, the bubble has been completed **/function bubbleSort ($ arr) {print ("** bubble sort method **"); $ len = count ($ arr ); $ temp = 0; $ num = 1; for ($ I = 0; ($ I <$ len-1) & ($ num> 0); $ I ++) {$ num = 0; for ($ j = $ len-1; $ j> $ I; $ j --) {if ($ arr [$ j]> $ arr [$ j-1]) {continue;} else {// bubble swap $ temp = $ arr [$ j-1]; $ arr [$ j-1] = $ arr [$ j]; $ arr [$ j] = $ temp; $ num ++ ;}}return $ arr ;} p (bubbleSort ($ arr);/*** select sorting method * @ param $ arr sorting array * train of thought: every round of loop To find the smallest number, save its subscript to the leftmost number *, and find the smallest number by comparing it with the right number. if the right side is small, save its subscript, then compare the value of this subscript with the next right digit until all right digits are compared, and then store this minimum value in the leftmost digit **/function selectSort ($ arr) {print ("** select sort method **"); $ len = count ($ arr); $ buff = 0; for ($ I = 0; $ I <$ len-1; $ I ++) {$ temp = $ I; for ($ j = $ I; $ j <$ len-1; $ j ++) {if ($ arr [$ temp]> $ arr [$ j + 1]) {$ temp = $ j + 1; // if the number on the right is smaller, save its subscript, used to compare with the following data} // Obtain the smallest numeric subscript if ($ temp! = $ I) {// if the subscript is not $ I, it is exchanged. otherwise, $ buff = $ arr [$ temp] is unnecessary. $ arr [$ temp] = $ arr [$ I]; $ arr [$ I] = $ buff;} return $ arr;} p (selectSort ($ arr )); /*** insert sorting method ** @ param $ arr sorting array * train of thought: assuming that the preceding number is already sorted, insert the nth number to the preceding order number. Insert the second data into * The first data to form an ordered array, and then insert the third number into an ordered array consisting of the first two numbers to form an * ordered array, finally, the sorting is completed in this way. **/function insertSort ($ arr) {print ("** insert sorting method **"); $ len = count ($ arr ); for ($ I = 1; $ I <$ len; $ I ++) {$ temp = $ arr [$ I]; // the data cache to be inserted for ($ j = $ I-1; $ j> = 0; $ j --) {if ($ temp <$ arr [$ j]) {$ arr [$ j + 1] = $ arr [$ j]; // if the data to be inserted is smaller than the last one in the original array, exchange with $ arr [$ j] = $ temp;} else break;} return $ arr;} p (insertSort ($ arr )); /*** quick sorting method ** @ param $ arr sorting array * train of thought: Select a benchmark element and scan it to sort the elements to be sorted The sequence is divided into two parts, and then the method is recursively called for each part * Finally, the sorted array on the left is returned and the sorted array on the right is merged with the reference element, which is an ordered array. **/Function quickSort ($ arr) {$ len = count ($ arr); if ($ len <= 1) return $ arr; $ baseNum = $ arr [0]; $ leftArr = $ rightArr = array (); // It is divided into two parts based on the reference element: for ($ I = 1; $ I <$ len; $ I ++) {if ($ arr [$ I] <= $ arr [0]) {$ leftArr [] = $ arr [$ I];} else {$ rightArr [] = $ arr [$ I] ;}$ leftArr = quickSort ($ leftArr); // recursive call $ rightArr = quickSort ($ rightArr ); return array_merge ($ leftArr, array ($ baseNum), $ rightArr);} print ("** quick sort method **"); p (quickSort ($ arr);?>

Running result:



The above describes how to implement bubble sorting, select sorting, insert sorting, and quick sorting in php, including fast sorting and insert sorting content, and hope to help friends who are interested in PHP tutorials.

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.