1.选择排序
- 选择排序
- [1] 80 31 37 10 70 48 60 33 80 選出最小值1
- [1 10] 31 37 80 70 48 60 33 80 選出最小值10
- [1 10 31] 37 80 70 48 60 33 80 選出最小值31
- [1 10 31 33] 80 70 48 60 37 80 ......
- [1 10 31 33 37] 70 48 60 80 80 ......
- [1 10 31 33 37 48] 70 60 80 80 ......
- [1 10 31 33 37 48 60] 70 80 80 ......
- [1 10 31 33 37 48 60 70] 80 80 ......
- [1 10 31 33 37 48 60 70 80] 80 ......
【摘自:http://202.193.64.35/dept7/acm/web/AlgorithmGossip/SelectionInsertionBubble.htm】
package com.bian.dp;
public class SelectSort {
/**
* @param args
*/
//选择排序 时间复杂度:O(n) 空间复杂度:data.length
//100,000 花费时间:35485 ms
public static void sort(int[] data){
for(int i=0;i<data.length;i++){
int lowIndex = i;
for(int j=data.length - 1;j>i;j--){
if(data[j] < data[lowIndex]) lowIndex = j;
}
swap(data,i,lowIndex);
}
}
public static void swap(int[] data,int i,int j){
int temp = data[i];
data[i] = data[j];
data[j] = temp;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] data = new int[100000];
for(int i=0;i<100000;i++){
data[i] = i;
}
long startTime = System.currentTimeMillis();
sort(data);
long endTime = System.currentTimeMillis();
System.out.println("Total time "+(endTime - startTime)+" ms");
}
}