Java系列-排序算法之选择排序
2017-05-11 10:22
615 查看
一句话概括思想: 从无序数组中选出当前最小值交换放入数组的第1-n位,最终排序结束则成为有序组。
话不多说,直接上代码:
话不多说,直接上代码:
public static void main(String[] args) { //the original array int[] arrSource = {10, 3, 4, 7, 22, 456}; //sort the array selectedSort(arrSource); //print the elements of array for (int element : arrSource ) { System.out.println(element); } } private static void selectedSort(int[] arrSource) { //ifn, return if (arrSource == null || arrSource.length == 0) { return; } //record the index of Mimimum int minIndex = 0; //sorted array for (int i = 0; i < arrSource.length - 1; i++) { minIndex = i; for (int j = i + 1; j < arrSource.length; j++) { //if arrSource[i] > arrSource[j], record the index of arrSource[j] if (arrSource[minIndex] > arrSource[j]) { minIndex = j; } } //if the mimimum is not the initial value, then swap them if (i != minIndex) { swap(arrSource, i, minIndex); } } } private static void swap(int[] arr, int i, int i1) { int temp = arr[i]; arr[i] = arr[i1]; arr[i1] = temp; }
相关文章推荐
- JavaScript ,Python,java,Go系列算法之【选择排序】篇
- 数据结构算法之排序系列Java、C源码实现(3)--直接选择排序
- 算法系列(二)冒泡排序、选择排序、插入排序和希尔排序(Java实现)
- JavaScript ,Python,java,Go系列算法之选择排序
- 白话经典算法系列之四 直接选择排序及交换二个数据的正确实现
- 算法:排序----Java选择排序
- 一步步学习数据结构和算法之选择排序效率分析及java实现
- 黑马程序员—Java基础学习笔记之排序算法:选择排序&冒泡排序
- 白话经典算法系列之四 直接选择排序及交换二个数据的正确实现
- 用Java写算法之二:选择排序
- 算法:排序----Java选择排序
- 我在北京找工作(二):java实现算法<1> 冒泡排序+直接选择排序
- java 合并排序算法、冒泡排序算法、选择排序算法、插入排序算法、快速排序
- 白话经典算法系列之四 直接选择排序及交换二个数据的正确实现
- java-算法-选择排序、插入排序
- 用JAVA实现排序算法之三:直接选择排序
- 排序选择算法--java--之高效率篇
- 【转载】白话经典算法系列之四 直接选择排序及交换二个数据的正确实现
- 结合舞蹈更易理解的算法--选择排序算法[java代码]
- java 合并排序算法、冒泡排序算法、选择排序算法、插入排序算法、快速排序算法的描述