Java Learning Data-java Common algorithm-selection sorting algorithm

Source: Internet
Author: User

Choosesort source Program
public class Choosesort {
private static void Choosesort (int [] a) {
for (int i = 0; i < a.length; i++) {
An array loop, assuming that the first element in the data is smaller than the back, and then compared to the data behind it
int k = i;
for (int j = i+1; J <a.length; J + +) {//Data traversal, looking for the smallest array subscript for the data element
if (A[j]<a[k])
K = J;
}
Minimum value and A[i] Exchange
if (i! = k) {
I and k are compared, if not equal, I and J subscript The element is not the same element, then Exchange A[i],a[k] value, ensure that the minimum value is always the first
int temp = A[i];
A[i] = a[k];
A[K] = temp;
}
}

}

public static void Main (string[] args) {
Int[] Test1 = {51, 38, 49, 27, 62, 5, 16}; Test array
Choosesort (test1);

for (int i = 0; i < test1.length; i++) {
System.out.print (Test1[i] + "");
}
}
}
3.4.1.2 Program Run Results:
5 16 27 38 49 51 62
3.4.2 Source program Disclosure
The basic idea of choosing a sort is that each trip (assuming that it is a trip) is processed from n-i+1 data by selecting a minimum (or maximum) of the data as the first data in an ordered sequence. One of the simplest is called direct selection sorting.
Select the data structure of the sorting algorithm:
Array to sort test1 = {51, 38, 49, 27, 62, 5, 16}
Select the sorting algorithm principle:
? The first pass of the direct selection sort is to select a minimum data from all n data in the data series as the 1th element in the ordered sequence and position it in the first storage location. The second pass selects a second small element from the data sequence's n-1 data as the 2nd element in an ordered sequence, locates it in the second storage location, and so on, When the n-1 process selects a smaller element from the remaining 2 elements of the data sequence as the last 2nd element in the ordered sequence and locates it in the penultimate storage location, the entire sorting process is complete.

Java Learning Data-java Common algorithm-selection sorting algorithm

Related Article

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.