The principle and realization of Java Simple choice sorting algorithm _java

Source: Internet
Author: User

Simple selection Sort: (Choose the minimum, put it first, then the first one goes backwards, so the loop) the first is compared with each of the following, each time making the smallest top, the first forward backward (that is, just selected first is the minimum value, no longer participate in the comparison, the comparison of the number minus 1)

Complexity: The number of operations required to record movement is less 0--3 (n-1), regardless of the initial arrangement of records, the required number of keywords between the same times, are N (n-1)/2, the total time complexity of O (N2);
Space complexity O (1)

Algorithm improvement: Each contrast, is to put the smallest value to the first, so you can compare to the end, find the minimum, directly put to the first place, eliminating the meaningless transfer of mobile operations. can also change a direction, the last one with each previous comparison, each time the maximum value of the bottom, the last one forward.

Java source code:

Copy Code code as follows:

public static void Selectsort (date[] days) {
int min;
Date temp;
for (int i = 0; i < days.length; i++) {
min = i;
for (int j = min + 1; j < Days.length; J + +) {
if (Days[min].compare (days[j]) > 0) {
min = j;
}
}
if (min!= i) {
temp = Days[i];
Days[i] = Days[min];
Days[min] = temp;
}
}
}
Class Date {
int year, month, day;

Date (int y, int m, int d) {
Year = y;
month = m;
Day = D;
}

public int Compare (date date) {
Return to year > date.year? 1:year < date.year? -1
: Month > Date.month? 1:month < Date.month? -1
: Day > Date.day? 1:day < Date.day? -1:0;
}

public void print () {
SYSTEM.OUT.PRINTLN (Year + "" + month + "" + day);
}
}


  Simple selection sort (plain Selection sort):

A simple selection sort is similar to a bubble sort (Bubble sort), each time selecting a maximum value to fill the current position in the remaining set of elements. The only difference is that the bubble sort exchanges the position of the element each time it is found to be less than (or greater than) the current value, and the simple selection sort is to select the maximum value in the remaining elements and the current position to exchange data.

For example, for element set r={37, 40, 38, 42, 461, 5, 7, 9, 12}

In the first sort: 37 direct and 5 exchange, forming a new sequence r1={5,40,38,42,461,37,7,9,12}
In the second sort: 40 Direct and 7 exchange, forming a new sequence r2={5,7,38,42,461,37,40,9,12}

And so on, until the last element (note: In the second order, 38:42 is small, but they do not exchange data).

The following is a Java implementation version of the simple selection order:

Copy Code code as follows:

public static void Selectionsort (int[] data) {
if (data = NULL | | Data.length <= 1)
Return
int I, J, value, Minpos, len = data.length;
int outer = len-1, tmp;
for (i = 0; i < outer; i++) {
Value = Data[i];
Minpos =-1;
for (j = i + 1; J < Len; J + +) {
if (Data[j] < value) {
Minpos = j;
Value = Data[j];
}
}
if (Minpos!=-1) {
TMP = Data[i];
Data[i] = value;
Data[minpos] = tmp;
}
for (int k = 0; K < Len; k++) {
System.out.print (Data[k] + ",");
//            }
System.out.println ();
}
}
public static void Main (string[] args) {
int[] coll = {
37, 40, 38, 42, 461, 5, 7, 9, 12
};
Selectionsort (coll);
for (int i = 0; i < coll.length; i++) {
System.out.print (Coll[i] + ",");
}
}

Tree Selection sort (Selection sort)
The tree selection sorting algorithm is a typical algorithm to change time in space relative to the simple selection sort. The idea is to treat sorted N elements, construct a relatively small (n+1)/2 number, and then construct a relatively small number of [N+1]/4 until there is only one element. Constructed into a complete binary tree.
When sorted, the element is the smallest, the smallest element is removed, the element is replaced with the maximum value, and the complete binary tree is adjusted.
The following is a Java implementation version of the tree selection sort:

Copy Code code as follows:

public static void Treeselectionsort (int[] data) {
if (data = NULL | | Data.length <= 1)
Return
int len = data.length, low = 0, I, J;
Add Auxiliary space
int[] tmp = new int[2*len-1];
int tsize = Tmp.length;
Construct a tree
For (i =len-1, j=tmp.length-1;i >=0 i--, j--) {
Tmp[j]=data[i];
}
for (i = tSize-1; i > 0; i-=2) {
tmp[(I-1)/2] = Tmp[i] > tmp[i-1]? Tmp[i-1]:tmp[i];
}
End
Remove the minimum node.
while (Low < Len) {
data[low++] = tmp[0];
for (j=tsize-1;tmp[j]!=tmp[0];j--);
TMP[J] = Integer.max_value;
while (J > 0) {
if (j%2 = = 0) {//If the right node
tmp[(J-1)/2] = Tmp[j] > tmp[j-1]?tmp[j-1]:tmp[j];
j = (j-1)/2;
}else{//If the left node
TMP[J/2]=TMP[J] > tmp[j+1]? TMP[J+1]:TMP[J];
j = J/2;
}
}
}
}

When constructing a complete binary tree, a set of N elements is required to 2*n-1 an auxiliary space.
Code:

Copy Code code as follows:

while (J > 0) {
if (j%2 = = 0) {//If the right node
tmp[(J-1)/2] = Tmp[j] > tmp[j-1]?tmp[j-1]:tmp[j];
j = (j-1)/2;
}else{//If the left node
TMP[J/2]=TMP[J] > tmp[j+1]? TMP[J+1]:TMP[J];
j = J/2;
}
}

The minimum value in the new collection of recursive constructs is implemented.

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.