Home > Java > javaTutorial > body text

JAVA simple selection sorting algorithm principle and implementation

高洛峰
Release: 2017-01-17 13:08:35
Original
2202 people have browsed it

Simple selection sorting: (select the smallest value, put it first, and then move the first value backward, and so on) Compare the first value with each subsequent one one by one, and put the smallest one at the top each time. The bits are pushed backward (that is, the first bit just selected is the minimum value and will no longer participate in the comparison, and the number of comparisons is reduced by 1)

Complexity: The number of operations required to move the record is less 0--3 (n-1), regardless of the initial arrangement of the records, the number of comparisons required between keywords is the same, which is n(n-1)/2, and the total time complexity is O(n2);
space Complexity O(1)

Algorithm improvement: Every comparison is to put the smallest value first, so you can compare it to the end, find the smallest value, and put it directly in the first place. Eliminate meaningless swap and move operations. You can also change the direction, and compare the last bit with each previous one, making the maximum value sink to the bottom each time, and the last bit advances forward.

JAVA source code:

 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 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);
 }
}
Copy after login

Simple Selection Sort:

Simple Selection Sort is similar to Bubble Sort (Bubble Sort), each time it will be in the remaining Select the highest value from the set of elements below and fill it into the current position. The only difference is that bubble sort will exchange the position of the element every time it finds that it is less than (or greater than) the current value, while simple selection sort selects the highest value among the remaining elements and exchanges data with the current position.

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

In the first sorting: 37 is directly exchanged with 5, Form a new sequence R1={5,40,38,42,461,37,7,9,12}
In the second sorting: 40 is directly exchanged with 7 to form a new sequence R2={5,7, 38,42,461,37,40,9,12}

and so on until the last element (note: in the second pass of sorting, 38 is smaller than 42, but they do not exchange data).

The following is a Java implementation version of simple selection sorting:

  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] + " , ");
  }
  }
Copy after login

Tree Selection Sort (Tree Selection Sort)
The tree selection sorting algorithm is typical of simple selection sorting. Algorithm for exchanging space for time. The idea is to treat the sorted N elements, construct relatively small (n+1)/2 numbers, and then construct relatively small [n+1]/4 numbers until there is only one element. Constructed into a complete binary tree.
When sorting, that element is the smallest, take out the smallest element, replace the element with the "maximum value", and then adjust the complete binary tree.
The following is a Java implementation version of tree selection sorting:

  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){  //如果是右节点
  tmp[(j-1)/2] = tmp[j] > tmp[j-1]?tmp[j-1]:tmp[j];
  j = (j-1)/2;
  }else{  //如果是左节点
  tmp[j/2]=tmp[j] > tmp[j+1]? tmp[j+1]:tmp[j];
  j = j/2;
  }
  }
  }
  }
Copy after login

When constructing a complete binary tree, a set of N elements requires 2*N -1 auxiliary space.
Code:

  while(j > 0){
  if(j%2 == 0){  //如果是右节点
  tmp[(j-1)/2] = tmp[j] > tmp[j-1]?tmp[j-1]:tmp[j];
  j = (j-1)/2;
  }else{  //如果是左节点
  tmp[j/2]=tmp[j] > tmp[j+1]? tmp[j+1]:tmp[j];
  j = j/2;
  }
  }
Copy after login

implements recursive construction of the minimum value in the new set.

For more articles related to the principle and implementation of JAVA simple selection sorting algorithm, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template