常用算法之选择排序
基本思想:在要排序的一组数中,选出最小的一个数与第一个位置的数交换;然后在剩下的数当中再找最小的与第二个位置的数交换,如此循环到倒数第二个数和最后一个数比较为止。
选择排序的基本考虑:第i趟简单选择排序是指通过n-i次关键字的比较,从n-i+1个记录中选出关键字最小的记录,并和第i个记录进行交换。先临时记录其位置,只有在一趟
循环完以后确定了最小的数据,才会发生交换。
逐步选取n-1到1(Java里面数组以0开始标记),分别作为第n,n-1,...,2层顶,第2层顶筑好了之后,只剩下一个比它小的元素,排序结束。
每个顶的筑成算法如下:用maxIndex变量记录最大元素的位置,初始化为顶所在的位置(默认顶最大),从顶的前面一位到位置0,依次和当前的最大元素比较,
如果比当前的最大元素大,maxIndex更新为新的位置,循环结束已经知道最大元素的位置maxIndex,如果最大元素不是顶,那么交换最大元素和顶。选择排序的Java实现以及测试代码如下:
写法一:
private static void SelectSort(int[] array) {
if (array == null && array.length < 2) {
return;
}
int size = array.length;
for (int i = size - 1; i > 0; i--) {
int maxIndex = i;
for (int j = i - 1; j > 0; j--) {
if (array[j] > array[maxIndex]) {
maxIndex = j;
}
}
if (maxIndex != i)
exchangeElements(array, i, maxIndex);
}
}
// 选择排序
public static void exchangeElements(int[] array, int index1, int index2) {
int temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
public static void printArray(int[] array) {
System.out.print("{");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
if (i < array.length - 1) {
System.out.print(", ");
}
}
System.out.println("}");
}
写法二:
public static void selectSort(int[] number) {
if (number == null && number.length < 2)
return;
int size = number.length;// 数组长度
int temp = 0;// 临时值
for (int i = 0; i < size; i++) {
int j = i;// 待确定位置
for (int k = size - 1; k > i; k--) {// 选择第i个位置
if (number[k] < number[j]) {
j = k;
}
}
// exchange
temp = number[i];
number[i] = number[j];
number[j] = temp;
}
}
public static void main(String[] args) {
int array[] = { 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5 };
System.out.println("选择排序前:");
printArray(array);
// SelectSort(array);
selectSort(array);
System.out.println("选择排序后:");
printArray(array);
}
结果如下:
![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZcAAABHCAIAAABETZ3BAAAHrElEQVR4nO2dWYKsIAxFXRcLynqymmyGxfA+cGC0MaG6H+U9X3bZBES8hsiwBQAAWJntrwsAAAAmoGIAgLUpVUyYfQghBO+FaT8+8EzshRwzbdtGIiLpmeMgS+g905binHPXaSGS/WAjCQAA8JDKF/PMEoJcyrNLzvULcVSpQ4A8u1SlSPYfjrMs13+QRPshBCHn3C5rR07C7pIyodMIAAD0yFTMs9s2xz4qzak3yfn9F88uF5jkDEuW8FItZi+0ORa/e2K7m+ZI/GE1tQkVAwAMUPtiIhLFjK+u437u9McciQi5wlMjotoXE+aiS0lUOHEkUeNq2QQAgJ9oqdge4MqcquimOZasA+nYx1PO9XyxM9B2HF5n9yMRqBgAQE1PxTyz7DLjjx5g8MwiRayeE81zWVzM5ZqVCdpu7tYXQ48SADBAoWJCjuj8ynhE+il3nkIdM+vFxXpuWXoy+mIipVGoGABggCK6zxKqj467VyV0el6FOnl255lcxY7uaYjJC0k6XDuSUJgHAIBRMOoVALA2UDEAwNpAxQAAawMVAwCsDVQMALA2bRXbB4VhmAMAC5LNnnkBLRXz7IoKSIe6Pq2aYpisrm6vuU9PB2Pkw0a09zVaUaSup9VrMlYnN7Zna+4fKNKdyZnltN64Puq21DfWbN5Crxm61FKx8vLTRXOMVaNJng5He04tyc8R2hyRzY7qKq758arksbbVNZAM8ZtRizOK1MRYSz9bn2lzRltKuKvJ9yx2NaJiyX20iZiq8Rp10/7AzHnwrOkbo4Y/nHNS8VPdB0ORfkRfS12mlnW6iEPFQuiqWHX1MyJlKj3y7BwLO61zX01ef8hRaJNHY++XGBq+MumZLM6q4Jn9k0+p2GzFmdyhNLelmrvm/Z4uZTWPsiFV12pilmlChofpzNN0X6pF0Qa4BN3c8ixugsnFMKlYOkNM06NtP2GGyryJV6lr6T4GpjPbXGfU2JZuy9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d13Ii5gnP43B/wr2cro9LemSxOsGDlgzXhagwypL9HtmZja8menWM5TBiamF4Ne1Hs93Qpq3mUzff4bdyiWmxsjOZCjIZO0Y/lVLoJNzbVfkfXpknFzuJom69ZxeyeqcKCvdkcaFrysSbfURSlbmiLfhU5myHYLN330vDFHlfonMBANXtzvvc08+Wk1O4Bq2r5TvuUv9+jnFQhT+/R1GajaMmei5U9tVpkf++Ub4DX+mIhhMdXX74EJqxFUT1OU9a3KK7LYLP7xFrL2dKRIZvJtRWN+UGR1LnPqxDTPbI2G1VLTjItxGg497YDOpI8X4ihkMLXx8XGovvtV9+EgRJVc9Tb7HcI1Tar1/9lY0o5dTZ7Yx2Gkttyt1bIrHukazbmlpwY0DTaniM2ljyt++J/oWIWT3TydzvYtNlE8ncmf7mKGcIj8c0wtzcOm6Y4D5K/L3kI4U1hsc4MpLfNYADgm3jb84vZ4ACAtYGKAQDWBioGAFgbqBgAYG2gYgCAtYGKAQDWBioGAFgbqBgAYG2gYgCAtYGKAQDWBioGAFgbqBgAYG2gYgCAtfkHxExu8Oigo6UAAAAASUVORK5CYII=)
参考链接:http://blog.csdn.net/kimylrong/article/details/17126267 ;
http://www.cnblogs.com/0201zcr/p/4764427.html