# Sorting algorithm Analysis "three": Select Sort (with python&c++ code)

Source: Internet
Author: User

principle

Select Sort (Selection sort) is a simple and intuitive sorting algorithm. It works as follows.

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.

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.

Analysis of complexity

Select the sort of interchange operation between and times. The comparison operation for the selection sort is between the times. The assignment operation of the selection sort is between and times.

The number of comparisons, compared to the initial state of the keyword, is independent of the total number of comparisons. The number of exchanges, preferably, has been ordered, exchanged 0 times, the worst case is, reverse, exchange times. The number of exchanges is less than the bubble sort, because the swap requires more CPU time than compared to the CPU time, and the value is smaller, the selection sort is faster than the bubble sort.

In-situ operations are almost the only advantage of selecting a sort, and when squareness (space complexity) is required, you can consider choosing a sort, which is very rare in practical applications.

The algorithm implements the Python version:

```#-*-encoding:utf-8-*-        def select_sort (ST_BF):    # Select sort for    i in Xrange (Len (ST_BF)):        temp = st_bf[i] Location        = i        # find the remainder minimum        for J in Xrange (I+1, Len (ST_BF)):            if ST_BF[J] < temp:                temp = st_bf[j]

location = J        Print  temp, location        # moving array while location! =        I:            st_bf[location] = st_bf[ LOCATION-1] Location-            = 1        st_bf[i] = temp        print st_bf        ST_BF = [6, 5, 3, 1, 8, 7, 2, 4, 2]print ST_BFSE Lect_sort (ST_BF)
```
The result is:

C + + version:

`#include <iostream>using namespace std;void print (int st_bf[], size_t size) {for (size_t i = 0; i < size; i++    ) {cout<< st_bf[i] << ""; } cout<< Endl;}  void Select_sort (int *st_bf, size_t size) {//select sort for (size_t i = 0; i < size; i++) {int temp = St_bf[i]; The cache needs to compare the values size_t location = i;            The position of the cache minimum value for (size_t j = i+1; j < size; J + +) {//Find the remainder minimum if (St_bf[j] < temp)                {temp = St_bf[j];            location = j;        }} cout << temp << "" << location << Endl;            Moving an array while (location! = i) {st_bf[location] = st_bf[location-1];        Location-= 1;        } St_bf[i] = temp;    Print (st_bf, size);    }}int Main () {int st_bf2[] = {6, 5, 3, 1, 8, 7, 2, 4, 2};    size_t size2 = sizeof (ST_BF2)/sizeof (st_bf2[0]);    Select_sort (ST_BF2, size2); return 0;}`
The result is ditto, not posted.

Actually can also optimize one step, the last step does not compare, certainly is the biggest. Don't change it.

If you see this blog post is not incomplete, that is I to prevent the crawler to release the first half of the reason, please see the original author blog.

If this blog post is helpful to you, for a good network environment, do not recommend reprint, Recommended Collection! If you must reprint, please bring the suffix and the address of this document.

Related Keywords:
Related Article

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.