Review data structure: Sort (iii)--select sort

Source: Internet
Author: User

The core of the selection sort is: Select the smallest element and the header interchange per trip.


Time complexity: O (n^2).

Select Sort is an unstable sort, for what? because it is not good to handle the position of the two number of positions, for example, sequence 5 8 5 2 9, we know that the first time to select the 1th element 5 and 2 Exchange, then the original sequence of 2 5 of the relative sequence is destroyed, so the selection is not a stable sorting algorithm. So a stable sort is sure not to change the relationship between equal numbers before the position.


The implementation code is as follows:


#include <iostream>using namespace std; void Selectsort (int a[], int n) {for (int i = 0; i < n-1; i++)  //Note n-1{int minindex = i+1; for (int j = i+1; J < N J + +) {if (A[j] < A[minindex]) Minindex = j;  Select i+1 the smallest number}if (A[minindex] < a[i]) swap (A[minindex], a[i]); }}int Main () {int a[] = {1, 6, 4, 3, 8, 2}; Selectsort (A, 6); for (int i = 0; i < 6; i++) cout<<a[i]<< "; cout<<endl; return 0; }

Reference Links:


http://blog.csdn.net/xiazdong/article/details/8462393


http://blog.csdn.net/hguisu/article/details/7776068


Http://zhidao.baidu.com/link?url=zFcwJJZtDlBDx-5GK6_4EVHMseEJg_mWwNnPVAym4yPJk9YjYch3qpF_-FSyKspp_wxQZnS1UUzlpZxGhspHUK





Review data structure: Sort (iii)--select sort

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.