Data Structure-sorting: Select sorting (select sorting directly)

Source: Internet
Author: User

Straight selection sort)
Basic Idea of directly selecting sorting
The Direct selection and sorting of files with N records can be directly selected and sorted through n-1 rows to get the ordered results:
① Initial status: the disordered area is R [1. N], and the ordered area is empty.
② Sorting by 1st bits
In the unordered zone R [1 .. n] selects the record R [k] with the minimum keyword, swaps it with the 1st records R [1] In the unordered area, so that R [1 .. 1] and R [2 .. n] into a new ordered area with one more record count and a new unordered area with one fewer record count.
......
③ Sort by I
At the beginning of the I-th sorting, the current ordered and disordered areas are R [1 .. I-1] and R [I. N] (1 ≤ I ≤ N-1), respectively ). This sort field selects the record R [k] with the smallest keyword from the current unordered area and exchanges it with the 1st records R [I] In the unordered area, so that R [1 .. i] and R [I + 1 .. n] into a new ordered area with one more record count and a new unordered area with one fewer record count.
In this way, the direct sorting of files with N records can be directly selected through n-1 to obtain the ordered results.

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace exselectionsorter
{
Class selectionsorter
{
Private int min;
Public void sort (INT [] ARR)
{
For (INT I = 0; I <arr. Length-1; ++ I)
{
Min = I;
For (Int J = I + 1; j <arr. length; ++ J)
{
If (ARR [J] <arr [Min])
Min = J;
}
Int T = arr [Min];
Arr [Min] = arr [I];
Arr [I] = T;
}
}
Static void main (string [] ARGs)
{
Int [] array = new int [] {1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
Selectionsorter S = new selectionsorter ();
S. Sort (array );
Foreach (INT m in array)
Console. writeline ("{0}", M );
}
}

}

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.