JAVA 数据结构与算法之————选择排序,冒泡排序,插入排序
1.选择排序:
/*
* 选择排序的时间复杂度为O(n2),空间复杂度为O(1)
*
* */
public class SelectSort<E extends Comparable> {
private Swap<E> s = new Swap<>();
/*ascOrDest=“asc" 升序 ascOrDest="desc" 降序*/
public void selectSort(E[] L, String ascOrDesc){
for(int i = 0; i < L.length - 1; i++){
int temp = i;
for(int j = i + 1; j < L.length; j++){
if(ascOrDesc == "asc"){
if(L[j].compareTo(L[temp]) < 0){
temp = j;
}
}else{
if(L[j].compareTo(L[temp]) > 0){
temp = j;
}
}
}
if(temp != i){
s.swap(L, i, temp);
}
}
}
public static void main(String[] args) {
SelectSort<Integer> selectSort