选择排序
2018-02-01 本文已影响7人
Stroman
package com.company;
public class SelectionSort {
/**
* 从无序序列中选择一个最大的或者最小的
* 然后插入到有序序列的后面
* 采用交换的方式
* @param sourceArray
*/
static public void selectionSort0(int[] sourceArray) {
int arrarLength = sourceArray.length;
for (int counter = 0;counter < arrarLength;counter++) {
int maxElement = sourceArray[counter];
int maxIndex = counter;
for (int counter0 = counter;counter0 < arrarLength;counter0++) {
//找出未有序的序列中最大的那个元素
if (sourceArray[counter0] > maxElement) {
//这个决定了是升序排列还是降序排列
maxIndex = counter0;
maxElement = sourceArray[maxIndex];
} else continue;
}
//交换
if (counter != maxIndex) {
sourceArray[maxIndex] = sourceArray[counter];
sourceArray[counter] = maxElement;
}
}
}
}