Sorting algorithm Selection

Source: Internet
Author: User

Select Sort Description

The basic idea is to first find the smallest (or largest) element in an unordered sequence, and then store it in the beginning 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.

Select Sort Code

/*

* Select sort

* Parameter Description

* A----with sorted array

* N----Array length

*/

void Select_sort (int a[], int n)

{

int i;//The end position of the ordered area

int j;//The end position of the unordered area

int min;//unordered area min element position

for (i=0;i<n;i++)

{

Min=i;

Find the smallest element between a[i-1]....a[n-1] and assign a value to min.

for (j=i+1;j<n;j++)

{

if (A[j]<a[min])

{

Min=j;

}//end of If

}//End of For2

If min!=i, then Exchange A[i] and A[min]

if (min!=i)

{

Swap (a[i],a[min]);

}//End of If

}

}

Time Complexity of O (N2)

Sorting algorithm Selection

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.