Simple selection of sorting algorithm

Source: Internet
Author: User

Basic idea:

The initial sequence (a[0]~a[n-1]) is used as the sequence to be sorted, the first time to find the smallest element in the sequence to be sorted (a[0]~a[n-1]), and the first element in the sequence a[0], the sequence (A[0]) is ordered; the next pass is sorted in the subsequence to be sorted (a[1]~a[n-1 ]). Order I, a[i-1]~a[n-1], find the smallest element that is exchanged with the first element in the subsequence a[i-1]. The order of the initial sequence is ordered after n-1.


Simple Selection Sorting Example:



Code:

void Selectsort (int a[],int N)//simple select sort {int small;for (int i=0; i<n; i++) {small=i;for (int j=i+1; j<n; J + +) {if (a[j]& Lt A[small]) Small=j;} Swap (A[i],a[small]);}}

time complexity Analysis:

The algorithm is independent of the initial sequence arrangement. Regardless of the initial sequence, the algorithm must perform a n-1 trip, performing a comparison of n-i-1 sub-keywords per trip,

The total number of comparisons is:


Therefore, the best, worst, and average time complexity for simple selection sorting is O (n^2).

In addition, the sorting algorithm passes a sequencing to determine the final position of an element. Simple selection sorting is an unstable sorting algorithm.


Resources:

"Data structure" Chen Huinan People's post and telecommunications press

Simple selection of sorting algorithm

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.