当前位置:网站首页>Select sort

Select sort

2020-11-08 18:38:00 Irving the procedural ape

The principle of the selection sorting algorithm is to find the smallest element in the array , Then exchange it with the first element of the array , Then find the smallest of the remaining elements , Then switch to the second element of the array , And so on , Until all the elements are sorted

The simple implementation is as follows :

1 package  Selection sort ; 2  3 public class DemoMain { 4  public static void main(String[] args) { 5   Integer[] a = {5, 2, 6, 3, 4, 7}; 6   sort(a); 7   System.out.println(isSorted(a)); 8   show(a); 9  }10 11  /**12   *  Sort 13   * @param a  Sorted array 14  */15  public static void sort(Comparable[] a) {16   for (int i = 0; i < a.length - 1; i++) {17    // Get the subscript of the element to be exchanged 18    int index = i;19    // Find the smallest element , Assign subscript to index20    for (int j = i + 1; j < a.length; j++) {21     if (less(a[index], a[j])) {22      index = j;23     }24    }25    // Pass the subscript of the element to be exchanged i And the subscript of the minimum index26    exch(a, i, index);27   }28  }29 30  /**31   *  Compare the size ,v > w The value is greater than 032   *  Use compareTo It's because the reference type implements Comparable Interface 33   * @param v  Parameters passed in the sort method 134   * @param w35   * @return  Return the judgment result 36  */37  private static b.........

版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢