Java sorting algorithm summary of selection sort

  • 2020-04-01 03:50:34
  • OfStack

This article illustrates the selection sort of Java sorting algorithm summary. Share with you for your reference. Specific analysis is as follows:

The basic operation of selection sort is to select the smallest (or largest) element from the data elements to be sorted at each pass and place it at the end of the sorted sequence until all the data elements to be sorted are sorted. The algorithm is not stable, the extra space of O(1), the time complexity of comparison is O(n^2), the time complexity of exchange is O(n), is not adaptive. It is not recommended in most cases. Only if you want to reduce the number of swaps.
 
The basic idea
 
The direct selection sort of n records can be sorted through n-1 times of direct selection sort to obtain the ordered result:
(1) initial state: the disordered area is R[1..n], the ordered area is empty.
Second order sort
In the unordered area R[1..n], select the record R[k] with the smallest keyword, and exchange it with the first record R[1] in the unordered area, so that R[1..1] and R[2..n] become the new ordered area with the number of records increasing by 1 and the new unordered area with the number of records decreasing by 1, respectively.
...
The I th sort

At the beginning of the ith sort, the current ordered and unordered regions are R[1.. I -1] and R(1 Or less I Or less n-1), respectively. The sequence sort selects the record R[k] with the smallest keyword from the current unordered area and exchanges it with the first record R in the unordered area, so that R[1.. I] and R become the new ordered area with the number of records increasing by 1 and the new unordered area with the number of records decreasing by 1, respectively.
In this way, the direct selection sort of n records can be sorted by n-1 direct selection sort.
 
Code implementation


public class Test { 
  public static int[] a = {10,32,1,9,5,7,12,0,4,3};
  //Default data array
  public static void main(String args[]) { 
    int i; //Loop count variable
    int Index = a.length;//Data index variable
    System.out.print(" Before ordering : "); 
    for (i = 0; i < Index - 1; i++) 
      System.out.printf("%3s", a); 
    System.out.println(""); 
    SelectSort(Index - 1); //Selection sort
    //Sorted result
    System.out.print(" After ordering : "); 
    for (i = 0; i < Index - 1; i++) 
      System.out.printf("%3s", a); 
    System.out.println(""); 
  } 
  public static void SelectSort(int Index) { 
    int i, j, k; //Loop count variable
    int MinValue; //Minimum variable
    int IndexMin; //Minimum index variable
    int Temp; //Temporary variable
    for (i = 0; i < Index - 1; i++) { 
      MinValue = 32767;//Current minimum
      IndexMin = 0; //Stores the index value of the minimum value
      for (j = i; j < Index; j++) { 
        if (a[j] < MinValue) //Find the minimum
        { 
          MinValue = a[j]; //Store minimum
          IndexMin = j; 
        } 
        Temp = a; //Exchange two values
        a = a; 
        a = Temp; 
      } 
      System.out.print(" The sorting of : "); 
      for (k = 0; k < Index; k++) 
        System.out.printf("%3s", a[k]); 
      System.out.println(""); 
    } 
  } 
}

Selection sort is the same as bubble sort in that the outermost loop is still executed n-1 times and is still inefficient.

I hope this article has been helpful to your Java programming.


Related articles: