selection sort(选择排序)
来源:互联网 发布:php程序用什么软件 编辑:程序博客网 时间:2024/05/01 11:40
转载自:http://java2novice.com/java-sorting-algorithms/selection-sort/
The selection sort is a combination of searching and sorting. During each pass, the unsorted element with the smallest (or largest) value is moved to its proper position in the array. The number of times the sort passes through the array is one less than the number of items in the array. In the selection sort, the inner loop finds the next smallest (or largest) value and the outer loop places that value into its proper location.
Selection sort is not difficult to analyze compared to other sorting algorithms since none of the loops depend on the data in the array. Selecting the lowest element requires scanning all n elements (this takesn − 1 comparisons) and then swapping it into the first position. Finding the next lowest element requires scanning the remaining n − 1 elements and so on, for (n − 1) + (n − 2) + ... + 2 + 1 = n(n − 1) / 2 ∈ Θ(n2) comparisons. Each of these scans requires one swap for n − 1 elements.
package com.java2novice.algos;public class MySelectionSort {public static int[] doSelectionSort(int[] arr){ for (int i = 0; i < arr.length - 1; i++) { int index = i; for (int j = i + 1; j < arr.length; j++) if (arr[j] < arr[index]) index = j; int smallerNumber = arr[index]; arr[index] = arr[i]; arr[i] = smallerNumber; } return arr;}public static void main(String a[]){int[] arr1 = {10,34,2,56,7,67,88,42};int[] arr2 = doSelectionSort(arr1);for(int i:arr2){System.out.print(i);System.out.print(", ");}}}
Output:
2, 7, 10, 34, 42, 56, 67, 88,
- 选择排序(selection sort)
- 选择排序(Selection Sort)
- selection sort 选择排序
- 选择排序 Selection Sort
- 选择排序(Selection Sort)
- 选择排序(Selection Sort)
- 选择排序 (Selection Sort)
- 选择排序(Selection sort)
- 选择排序 (Selection sort)
- 选择排序Selection sort
- Selection Sort(选择排序)
- 选择排序(Selection Sort)
- Selection Sort选择排序
- selection sort 选择排序
- selection sort(选择排序)
- Selection sort 选择排序
- 选择排序Selection sort
- 选择排序(Selection Sort)
- response = httpClient.execute(httpGet)不返回response
- USCAO1.2 双重回文数
- hello,myblog
- centos 6.5安装NodeJS
- delegate和event的区别
- selection sort(选择排序)
- java之访问日志
- 数据库工具类实现
- springmvc前端控制器的配置
- MySQL索引背后的数据结构及算法原理
- 2017技术准备
- Linux系统SPI驱动学习笔记(二)
- 异步编程(AsyncCallback委托,IAsyncResult接口,BeginInvoke方法,EndInvoke方法的使用小结)
- 字母,数字,下划线或者数字的正则表达式