Home  >  Article  >  Java  >  Example tutorial on implementing selection sort algorithm in Java

Example tutorial on implementing selection sort algorithm in Java

高洛峰
高洛峰Original
2017-01-19 09:37:021081browse

Selection sorting concept

Selection sorting is also an exchange sorting algorithm, which has a certain degree of similarity with bubble sorting. Therefore, I personally think that selection sorting can be regarded as an improved algorithm of bubble sorting. The idea is this:
Suppose now we want to sort the array arr[], which has n elements.
1 Compare the first element (in Java, the subscript is 0) and the second element. If the former is greater than the latter, then it must not be the smallest, but we are not as anxious as bubble sorting exchange. We can set a temporary variable a to store the subscript of the current smallest element. Then we continue to compare the current smallest element with the third element. If it is still not the smallest, then we modify the value of a. In this way, until the comparison with the last element is completed, you can be sure that a stores the subscript of the smallest element.
2. If the value of a is not 0 (the initial value, that is, the subscript of the first element), exchange the two elements with subscripts a and 0.
3. Repeat the above process, this time starting from the element with subscript 1, because the smallest element has been placed at the position with subscript 0.
4. Do this until only the last element is left. You can be sure that this element is the largest.
5. Sorting completed.
Obviously, this algorithm also requires n-1 rounds of sorting.
It should be noted that the above description is only the method of finding the minimum value each time. In fact, you can also find the maximum value every time, but then you need to put it at the end of the array every time.

Java implementation code:
SelectArray.java

package ch02;
 
public class SelectArray {
  // 数组
  private long[] arr;
 
  // 数组中有效数据的大小
  private int elems;
 
  // 默认构造函数
  public SelectArray() {
    arr = new long[50];
  }
 
  public SelectArray(int max) {
    arr = new long[max];
  }
 
  // 插入数据
  public void insert(long value) {
    arr[elems] = value;
    elems++;
  }
 
  // 显示数据
  public void display() {
    for (int i = 0; i < elems; i++) {
      System.out.print(arr[i] + " ");
    }
    System.out.println();
  }
 
  // 选择排序
  public void selectSort(){
    int min = 0;
    long tmp = 0L;
    for(int i = 0; i < elems -1; i++){
      min = i;
      for(int j = i + 1; j < elems; j++) {
        if(arr[j] < arr[min]) {
          min = j;
        }
      }
      tmp = arr[i];
      arr[i] = arr[min];
      arr[min] = tmp;
    }
  }
}

Test code:

package ch02;
 
public class TestSelectArray {
 
  public static void main(String[] args) {
    SelectArray sArr = new SelectArray();
    sArr.insert(89);
    sArr.insert(54);
    sArr.insert(667);
    sArr.insert(7);
    sArr.insert(12);
    sArr.insert(43);
    sArr.insert(12);
 
    sArr.display();
    sArr.selectSort();
    sArr.display();
 
  }
 
}

Result:

Example tutorial on implementing selection sort algorithm in Java


For more related articles on Java implementation of selection sorting algorithm example tutorials, please pay attention to the PHP Chinese website!


Statement:
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