Select sort 1

Source: Internet
Author: User

Select sort (Selection sort)

A simple and intuitive sorting algorithm. It works by selecting the smallest (or largest) element of the data element to be sorted each time, storing it at the beginning of the sequence until all the data elements to be sorted are exhausted. Select Sort is an unstable sort method (for example, sequence [5, 5, 3] swaps the first [5] with [3] for the first time, causing the first 5 to move back to the second 5).

Select sort (Selection sort) is a simple and intuitive sorting algorithm. The basic idea is to first find the smallest (or largest) element in the unsorted sequence and then store it in the starting position of the sequence, and then continue looking for the smallest (or largest) element from the remaining unsorted elements, and then place it at the end of the sorted sequence. And so on until all elements are sorted.

Plot selection sorting

Take the sequence {20,40,30,10,60,50} as an example to demonstrate its selection ordering process (for example).

The sorting process is as follows:

1th trip : i=0. Find the minimum value a[3]=10 in a[1...5], and then swap a[0] and a[3]. Array change: 20,40,30,10,60,50--> 10,40,30,20,60,50

2nd trip : I=1. Find the minimum value a[3]=20 in a[2...5], and then swap a[1] and a[3]. Array change: 10,40,30,20,60,50--> 10,20,30,40,60,50

3rd trip : i=2. Find the minimum value in a[3...5], because the minimum value is greater than a[2], the trip does not do any processing.

4th trip : i=3. Find the minimum value in a[4...5], because the minimum value is greater than a[3], the trip does not do any processing.

5th Trip : i=4. Exchange A[4] and a[5] data. Array change: 10,20,30,40,60,50--> 10,20,30,40,50,60

Select Sort Code

Based on the above process, it is not difficult to write down the code implementation of the selection sort, which is arranged in ascending order.

/** Select Sort * * Parameter description: * A--array to be sorted * N--The length of the array*/ voidSelect_sort (intA[],intN) {    intI//the end position of the ordered area    intJ//starting position of the unordered area    intMin//the smallest element position in an unordered region     for(i=0; i<n; i++) {min=i; //find "a[i+1". A[n] "min element, and assign to Min         for(j=i+1; j<n; J + +)        {            if(A[j] <a[min]) min=J; }        //if min!=i, then Exchange A[i] and A[min]. //after the exchange, guaranteed a[0]. A[i] elements are ordered.         if(min! =i) swap (A[i], a[min]); }}

Time complexity and stability

The time complexity for selecting a sort is O (N2): Suppose there are n numbers in the ordered sequence. The time complexity of traversing a trip is O (N), how many times do you need to traverse it? N-1 Times Therefore, the time complexity of selecting sorting is O (N2).

Select Sort is a stable algorithm that satisfies the definition of a stable algorithm: Suppose that there is a a[i]=a[j in the sequence, if A[i] precedes a[j before ordering, and A[i] is still in front of A[j]. Then this sort algorithm is stable!

Select sort 1

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.