Java实例7 - 选择排序 Selection Sort
2011-07-03 08:03
519 查看
/** * 选择排序的思想: * 每次从待排序列中找到最小的元素, * 然后将其放到待排的序列的最左边,直到所有元素有序 * * 选择排序改进了冒泡排序,将交换次数从O(N^2)减少到O(N) * 不过比较次数还是O(N) */ package al; public class SelectSort { public static void main(String[] args) { SelectSort selectSort = new SelectSort(); int[] elements = { 14, 77, 21, 9, 10, 50, 43, 14 }; // sort the array selectSort.sort(elements); // print the sorted array for (int i = 0; i < elements.length; i++) { System.out.print(elements[i]); System.out.print(" "); } } /** * @author * @param array 待排数组 */ public void sort(int[] array) { // min to save the minimum element for each round int min, tmp; for(int i=0; i<array.length; i++) { min = i; // search for the minimum element for(int j=i; j<array.length; j++) { if(array[j] < array[min]) { min = j; } } // swap minimum element tmp = array[i]; array[i] = array[min]; array[min] = tmp; } } }
相关文章推荐
- Java实例7 - 选择排序 Selection Sort
- Java数据结构及算法实例:选择排序 Selection Sort
- Java实现选择排序算法的实例教程
- Java 选择排序 (Selection Sort)
- Java 选择排序、插入排序、希尔算法实例详解
- java排序算法之_选择排序(实例讲解)
- java简单选择排序实例
- JAVA_选择排序、冒泡排序
- java排序高级之选择排序实现方法
- Java数组冒泡,选择,插入三种常用排序法及自带的快速排序法
- JAVA基础实例(三)--排序
- Java代码:数组选择排序 (升序降序)
- 选择,插入,希尔,快速,堆,归并排序六种排序方式的Java 实现和性能对比(付代码)
- java简单选择排序
- java 实现计数排序和桶排序实例代码
- 算法和数据结构-选择排序、插入排序及希尔排序 java实现
- 基本排序_选择排序_Java实现
- java中插入排序,选择排序,冒泡排序
- java数据结构与算法-简单排序-选择排序
- 数组排序-冒泡排序-选择排序-插入排序-希尔排序-快速排序-Java实现