7 Kinds of PHP Basic sort realization Method _php skill

Source: Internet
Author: User
Tags php language

This article summarizes the commonly used 7 sorts of sorting method, and realizes with the PHP language.

1. Direct Insertion Sort


 * *  Direct insertion Sort, the idea of inserting sort is: the element before the current insertion position is ordered,
 *  If the element inserted in the current position is larger than the last element of the ordered element, nothing is done,
 * Otherwise the  insertion position is found in the ordered sequence. and inserts the
/function Insertsort ($arr) {
  $len = count ($arr);  
  for ($i = 1; $i < $len $i + +) {
    if ($arr [$i-1] > $arr [i]) {for
      ($j = $i-1; $j >= 0; $j--) {
        $tmp = $arr [$j +1];
        if ($tmp < $arr [$j]) {
          $arr [$j +1] = $arr [$j];
          $arr [$j] = $tmp;
        } else{break
  ;  
  }}} return $arr;
}

2, bubble sort


  /* Bubble sort idea: Carry out n-1 trip bubble sort, adjust maximum value to array (sub array) at the end of each 22 comparison/
function Bubblesort ($arr) {
  $len = count ($arr); For
  ($i = 1; $i < $len; $i + +) {for
    ($j = 0; $j < $len-$i; $j + +) {
      if ($arr [$j] > $arr [$j +1]) {
        $tm p = $arr [$j +1];
        $arr [$j +1] = $arr [$j];
        $arr [$j] = $tmp;
      }
  }} return $arr;
}

3, Simple choice of sorting


  /* Simple sorting, simple sort idea: from the first element of the array to determine the small to large
element
/function Selectsort ($arr) {
  $len = count ($arr);
  for ($i = 0; $i < $len; $i + +) {
    $k = $i;
    for ($j = $i +1; $j < $len; $j + +) {
      if ($arr [$k] > $arr [$j]) {
        $k = $j;
      }
    }
    if ($k!= $i) {
      $tmp = $arr [$i];
      $arr [$i] = $arr [$k];
      $arr [$k] = $tmp;
    }
  }
  return $arr;
}

4, Hill sort


  /* Hill sort, Hill sort principle: Divide the array into several subsequence by the specified step, and then sort the sequence (in this direct)/
function Shellsort ($arr) {
  $len = count ($ ARR);
  $k = Floor ($len/2);
  while ($k > 0) {
    for ($i = 0; $i < $k; $i + +) {
      for ($j = $i; $j < $len, ($j + $k) < $len; $j = $j + $k) {
        if ($arr [$j] > $arr [$j + $k]) {
          $tmp = $arr [$j + $k];
          $arr [$j + $k] = $arr [$j];
          $arr [$j] = $tmp;
        }
    }} $k = Floor ($k/2);
  }
  return $arr;
}

5. Quick Sort


 * *  Quick Sort, quick-arrange thinking: Through a trip sort the records to be sorted into two separate sections, in which the key of the record is not greater than the key for the other part of the record,  and then the two parts of the record continue to be sorted quickly. In order to reach the entire sequence, the specific approach requires
 *  each set a standard keyword and a pointer to the key of the first record and the keyword of the last record, respectively.
 *  quickSort ($arr, 0, Count ($arr)-1);
 */
function QuickSort (& $arr, $low, $high) {
  if ($low < $high) {
    $i = $low;
    $j = $high;
    $primary = $arr [$low];
    while ($i < $j) {while
      ($i < $j && $arr [$j] >= $primary) {
        $j-;
      }
      if ($i < $j) {
        $arr [$i + +] = $arr [$j];
      }
      while ($i < $j && $arr [$i] <= $primary) {
        $i + +;
      }
      if ($i < $j) {
        $arr [$j-] = $arr [$i];
      }
    }
    $arr [$i] = $primary;
    QuickSort ($arr, $low, $i-1);
    QuickSort ($arr, $i +1, $high);
  }

6, Heap sorting


/////Adjust the Dagen process of the child heap, $s the location of the root of the heap, $m the last element position
function Heapadjust (& $arr, $s, $m) {
  $tmp = $arr [$s];
  The role of the left sub heap or the right child heap//For loop may be affected by the adjustment to Dagen
  is to ensure that the child heap is also Dagen for
  ($j = 2* $s + 1; $j <= $m; $j = 2* $j + 1) {
    //Locate the root node The largest of the children, and then use the largest one compared to the root node,
    //Jorda is adjusted, otherwise Dagen the characteristics of the jump out of the loop  
    if ($j < $m && $arr [$j] < $arr [$j +1]) {
      $j + +;
    }
    if ($tmp >= $arr [$j]) {break
      ;
    }
    $arr [$s] = $arr [$j];
    $s = $j;
  }
  $arr [$s] = $tmp;
}

Heap Sort
function Heapsort ($arr) {
  $len = count ($arr);
  In turn, the heap is adjusted from the child heap to Dagen for
  ($i = Floor ($len/2-1), $i >= 0; $i-) {
    heapadjust ($arr, $i, $len-1);
  }
  In turn, the root node is swapped to the last position, the heap is again adjusted to the large root heap, find the secondary maximum,
  //And then get an ordered array for
  ($n = $len-1; $n > 0; $n-) {
    $tmp = $arr [$n];< c33/> $arr [$n] = $arr [0];
    $arr [0] = $tmp;
    Heapadjust ($arr, 0, $n-1);
  }
  return $arr;
}

7. Merge sort

 * * Merge sort, where the two-way merge////will be ordered $arr1[s. M], $arr 2[M+1..N] merge into ordered $arr2[s. N] Function Merge (& $arr 1, & $arr 2, $s, $m, $n) {for ($k = $s, $i = $s, $j = $m +1; $i <= $m && $j <= $n;
    $k + +) {if ($arr 1[$i]< $arr 1[$j]) {$arr 2[$k] = $arr 1[$i + +];
    }else {$arr 2[$k] = $arr 1[$j + +];
    } if ($i <= $m) {for (; $i <= $m; $i + +) {$arr 2[$k + +] = $arr 1[$i];
    ' Else if ' ($j <= $n) {for (; $j <= $n; $j + +) {$arr 2[$k + +] = $arr 1[$j];
  The two-way merge function Msort (& $arr 1, & $arr 2, $s, $t) {if ($s = = $t) {$arr 2[$s] = $arr 1[$s];
    }else {$m = Floor (($s + $t)/2);
    $tmp _arr = Array ();
    Msort ($arr 1, $tmp _arr, $s, $m);
    Msort ($arr 1, $tmp _arr, $m +1, $t);
  Merge ($tmp _arr, $arr 2, $s, $m, $t);
  A two-merge function MergeSort ($arr) {$len = count ($arr) for elements in an array $arr[0..n-1]
  Msort ($arr, $arr, 0, $len-1);
return $arr; }

Use experience
If the number of the sorted records n is small, the direct insertion sort and simple selection can be used, when the information of the record itself is large, it is better to choose a simple sorting method.
If you want to sort records in the basic order of keywords, suitable for direct insertion and bubble sort.
If the n value is large, it can be sorted by quick sort, heap sort and merge. In addition, fast sorting is considered to be the best method in the internal sorting method.

The above is the entire content of this article, I hope to help you learn.

Related Article

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.