一. 简单选择排序要点
二. 算法分析
- 平均时间复杂度:o(N^2),嵌套双循环
- 最好时间复杂度:o(N^2),每次要找最大最小肯定是要遍历一遍的
- 最坏时间复杂度:o(N^2)
- 空间复杂度:o(1)
- 稳定性:稳定的(在注释中已解释)
三. 代码如下
/**
* 冒泡排序
* @author yinmb
* @Date 2019/5/6
*/
public class Test06 {
public int[] init(){
int[] a={42,14,67,39,32,11};
return a;
}
public void bubbleSort (int[] a){
for (int i = 0; i < a.length; i++) {
for (int j = i; j < a.length; j++) {
if (a[i]>a[j]){
int temp =a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
public static void main(String[] args) {
Test06 test06 = new Test06();
test06.bubbleSort(test06.init());
}
}
网友评论