Live2d

算法学习之选择排序

该文是对选择排序算法的一些总结和分析,具体代码和注释如下:


/**算法思想:选择排序法是一种不稳定的排序算法。它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,
然后放到已排序序列的末尾。以此类推,直到全部待排序的数据元素排完。*/
import jdk.nashorn.internal.objects.Global;

import java.util.ArrayList; //导入list容器类
import java.util.Comparator; //导入接口抽象类
import java.util.List;
import java.util.Scanner; //导入输入流sanner类

 

public class Selection {

// This class should not be instantiated.
private Selection() { //创建构造方法
}

/**
* Rearranges the array in ascending order, using the natural order.
*
* @param a the array to be sorted
*/
public static void sort(Comparable[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int min = i;
for (int j = i + 1; j < n; j++) {
if (less(a[j], a[min])) min = j; //在数组中进行比较值,使用less方法,选择数组中最小的值将对应序号的值赋给min
//less函数返回true则表示a[j]<a[min],即可已对min重新赋值

}
exch(a, i, min); //使用排序函数exch将找到的最小值位置进行交换
assert isSorted(a, 0, i);
}
assert isSorted(a);
}

/**
* Rearranges the array in ascending order, using a comparator.
*
* @param a the array
* @param comparator the comparator specifying the order
*/
public static void sort(Object[] a, Comparator comparator) {
int n = a.length;
for (int i = 0; i < n; i++) {
int min = i;
for (int j = i + 1; j < n; j++) {
if (less(comparator, a[j], a[min])) min = j;
}
exch(a, i, min);
assert isSorted(a, comparator, 0, i);
}
assert isSorted(a, comparator);
}


/***************************************************************************
* Helper sorting functions.
***************************************************************************/

// is v < w ?
private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0; //对传入的参数v 和w就行比较 ,v<w则返回true,否则返回false
}

// is v < w ?
private static boolean less(Comparator comparator, Object v, Object w) {
return comparator.compare(v, w) < 0;
}


// exchange a[i] and a[j]
private static void exch(Object[] a, int i, int j) { //数组中位置交换方法
Object swap = a[i];
a[i] = a[j];
a[j] = swap;
}


/***************************************************************************
* Check if array is sorted - useful for debugging.
***************************************************************************/

// is the array a[] sorted?
private static boolean isSorted(Comparable[] a) { //返回排序后的数组
return isSorted(a, 0, a.length - 1);
}

// is the array sorted from a[lo] to a[hi]
private static boolean isSorted(Comparable[] a, int lo, int hi) { //以下皆为重载方法
for (int i = lo + 1; i <= hi; i++)
if (less(a[i], a[i - 1])) return false;
return true;
}

// is the array a[] sorted?
private static boolean isSorted(Object[] a, Comparator comparator) {
return isSorted(a, comparator, 0, a.length - 1);
}

// is the array sorted from a[lo] to a[hi]
private static boolean isSorted(Object[] a, Comparator comparator, int lo, int hi) {
for (int i = lo + 1; i <= hi; i++)
if (less(comparator, a[i], a[i - 1])) return false;
return true;
}


// print array to standard output
private static void show(Comparable[] a) {
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List list = new ArrayList();
while (in.hasNext()) {
String s = in.nextLine();

String[] a = s.split(",");
Selection.sort(a);
show(a);
}
}
}

posted @ 2021-03-24 17:21  IkNyEa  阅读(62)  评论(0)    收藏  举报