Basic algorithm: Select sorting

Source: Internet
Author: User

Overall Thinking:

1. first, compare the first and second numbers. If the second number is large, use the first number to continue to compare with the third number. Otherwise, use the second number to compare with the third number. If the third number is small, compare the third number with the fourth number, and so on. until the smallest number is found in the number to be sorted, and the number is switched to the first number.

2. Start from the second number and compare it with the third number until the second smallest number is found, and the second number is switched to the second number, and so on.

Package SortSelect;

Public class SortSelect {

Public static void main (String args []) {
Int sortData [] = {4, 1, 2, 3, 8, 6, 5, 9, 11 };
SortSelet (sortData );
System. out. println (sortData );
}

Static int I, j, tmp, min;

Public static void sortSelet (int data []) {
For (I = 0; I <data. length-1; ++ I ){
Min = I;
J = I + 1;
While (true ){
If (j> = data. length)
Break;
If (data [j] <data [min]) {
Min = j;
}
++ J;
}
/*
* Switch location.
*/
Tmp = data [I];
Data [I] = data [min]; // assign the smallest data [I] after each comparison;
Data [min] = tmp;

}
}
}

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.