标签:java i++ com rabl string loading imp big OLE
算法分析:
789456,首先选择第一个为最小值,跟后面的值进行比较7小于8不动,7小于9不动,7大于4则进行互换489756
789456 原始数据
489756 --->479856--->459876 min=4
459876 --->458976--->457986--->456987 min=5
456987 --->456897--->456798 min=6
456798 --->456789 min=7
456789 min=8
动图演示:

代码实现:
package com.hy.sort.selectionsort;
import java.util.Arrays;
/**
* @author hanyong
* @date 2020/11/3 23:41
*/
public class SelectionSort {
/**
* 大于0说明c1大与c2
*
* @param c1
* @param c2
* @return
*/
private boolean isBigger(Comparable c1, Comparable c2) {
return c1.compareTo(c2) > 0;
}
/**
* 交换数组对应索引的值
*
* @param comparables
* @param i
* @param j
*/
private void exchange(Comparable[] comparables, int i, int j) {
Comparable temp = comparables[i];
comparables[i] = comparables[j];
comparables[j] = temp;
}
/**
* 选择排序实现
* @param comparables
*/
private void selectionSort(Comparable[] comparables) {
//假定的最小值索引由0依次递增,可以到到数第二个
for (int i = 0; i <= comparables.length - 2; i++) {
//假设本次的最小值索引为i
int minIndex = i;
for (int j = i + 1; j <= comparables.length - 1; j++) {
if (isBigger(comparables[minIndex], comparables[j])) {
//更换最小值索引
minIndex = j;
}
exchange(comparables, i, minIndex);
}
}
}
public static void main(String[] args) {
Integer[] numbs = {4, 3, 6, 10, 9, 8};
SelectionSort selectionSort = new SelectionSort();
selectionSort.selectionSort(numbs);
System.out.println(Arrays.toString(numbs));
}
}
时间复杂度分析:
654321
数据比较次数:(n-1)+(n-2)+(n-3)+...+1=((n-1)+1)/2 * (n-1)/2=n2/4-n/4
数据交换次数:n-1次
合计:n2/4+3n/4-1 时间复杂度为O(n2)
标签:java i++ com rabl string loading imp big OLE
原文地址:https://www.cnblogs.com/yongzhewuwei/p/13923562.html