Simple sorting of the PHP sorting algorithm (easy Selection sort)

Source: Internet
Author: User
This article mainly introduced the PHP sorting algorithm's Simple choice sort (easy Selection sort), combined with the example form more detailed analysis the simple choice sorting algorithm principle and the related realization skill, needs the friend to refer to the next

In this paper, we describe the simple selection of the PHP sorting algorithm (Selection sort). Share to everyone for your reference, as follows:

Basic idea:

Through the comparison between the N-i keyword, the smallest record of the keyword is selected from the N-i + 1 records and exchanged with the first (1 <= i <= N) records, and the sequence of records is completed after the n-1 trip.

Algorithm implementation:

<?php//Simple selection sort//Swap functions function Swap (array & $arr, $a, $b) {  $temp = $arr [$a];  $arr [$a] = $arr [$b];  $arr [$b] = $temp;} Simple selection sorting algorithm function Selectsort (array & $arr) {  $count = count ($arr);  for ($i = 0; $i < $count-1; $i + +) {    //record all elements after $i element minimum subscript    $min = $i;    for ($j = $i + 1; $j < $count; $j + +) {      if ($arr [$j] < $arr [$min]) {        $min = $j;      }    }    if ($min! = $i) {      swap ($arr, $min, $i);}}}  $arr = Array (9,1,5,8,3,7,4,6,2); Selectsort ($arr); Var_dump ($arr);

Operation Result:

Array (9) {[0]=> int (1) [1]=> int (2) [2]=> int (3) [3]=> int (4) [4]=> int (5) [5]=> int (6) [6]=> Int (7) [7]=> Int (8) [8]=> int (9)}

Analysis of Complexity:

You need to move records less often during simple selection sorting. In the best case, the initial state of the records to be sorted is already in a positive order, so you do not need to move the records.

In the worst case, the initial state of the records to be sorted is the largest of the first records, and then the records are ordered from small to large, and the maximum number of times to move Records is 3 (n-1). Simple selection The number of comparisons that need to be made in the sort process is independent of the arrangement of the sequence of records to be sorted in the initial state. When I=1, a n-1 comparison is required, and n-2 comparisons are required when i=2, and so on, the total number of comparisons required is (n-1) + (n-2) +...+2+1=n (n-1)/2, that is, the time complexity of the comparison operation is O (n^2), The time complexity of the move operation is O (n).

Simple selection sorting is an unstable sort.

This article refers to from the "Big Talk Data Structure", in this only record, convenient to consult later, great God do not spray!

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.