A detailed description of the direct selection sequence of the PHP sorting algorithm series

Source: Internet
Author: User
This article is mainly for the introduction of the PHP sorting algorithm series of direct selection of relevant data, with a certain reference value, interested in small partners can refer to

Direct Select sort

The basic idea of direct selection sorting (straight select sorting) is to select the minimum value from r[0]~r[n-1], to switch to r[0], to select the smallest value from r[1]~r[n-1] for the first time, and to switch to r[1], ...., i-th from r[i-1]~ R[n-1], select the minimum value, and r[i-1] exchange, ..., the n-1 time from r[n-2]~r[n-1] to select the minimum value, and r[n-2] exchange, a total of n-1 times, to get a sort code from small to large order sequence ·

The main advantages of selecting a sort are related to data movement. If an element is in the correct final position, it will not be moved. Select sort every time a pair of elements is exchanged, at least one of them will be moved to its final position, so the table of n elements is sorted for a total of up to n-1 times. In all of the sorting methods that rely entirely on swapping to move elements, choosing a sort is a very good one.

Principle

First find the smallest (large) element in the unordered sequence, place it at the beginning of the sort sequence, and then continue looking for the smallest (large) element from the remaining unsorted elements, and place it at the end of the sorted sequence. And so on until all elements are sorted.

Example

Set the array to a[0...n-1].
1. Initially, the array is all unordered area is a[0..n-1]. Make I=0
2. Select a minimal element in the unordered area a[i...n-1] and swap it with a[i]. After the exchange, A[0...I] formed an orderly area.
3.i++ and repeat the second step until the i==n-1. Sorting is complete.

Example

Sorting an array [53,89,12,98,25,37,92,5]

First Take i=0;a[i] as the minimum value, compare the latter value with A[i], if smaller than a[i], then the a[i] Exchange location, $i + +

[5,89,53,98,25,37,92,12]

First Take i=1;a[i] as the minimum value, compare the latter value with A[i], if smaller than a[i], then the a[i] Exchange location, $i + +

[5,12,89,98,53,37,92,25]

Repeat the above steps

PHP Code implementation

function Select_sort ($arr) {  $length =count ($arr);  for ($i =0; $i < $length-1; $i + +) {    for ($j = $i +1, $min = $i; $j < $length; $j + +) {      if ($arr [$min]> $arr [$j]) {        $min = $j;      }    }    $temp = $arr [$i];    $arr [$i]= $arr [$min];    $arr [$min]= $temp;  }  return $arr;}

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.