JAVA 数据结构与算法之————选择排序,冒泡排序,插入排序

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 = new SelectSort<>();
        Integer[] data = {2, 6, 1, 8, 2};
        selectSort.selectSort(data, "desc");

        System.out.println(Arrays.toString(data));

    }
}

swap方法:
功能:数组中两个位置的值

public void swap(E[] data, int i, int j){
        E temp = data[i];
        data[i] = data[j];
        data[j] = temp;
    }

2,冒泡排序

/*
* 时间复杂度为O(n2)
* 空间复杂度为O(1)
* ascOrDest=“asc" 升序  ascOrDest="desc" 降序
* */
public void bubbleSort(E[] L, String ascOrDesc){

        for(int i = 1; i < L.length; i++){
            for(int j = 0; j < L.length - 1; j++){
                if(ascOrDesc == "asc"){
                    if(L[j].compareTo(L[j + 1]) > 0){
                        s.swap(L, j, j + 1);
                    }
                }else{
                    if(L[j].compareTo(L[j + 1]) < 0){
                        s.swap(L, j, j + 1);
                    }
                }
            }
        }
     }

改进冒泡排序:

/*
    * 改进冒泡排序,用一个flag记录再一轮排序中元素是否交换过,如果没有交换则说明后面的元素已经有序,不需要再交换了。
    * */
    public void  optimizeBubbleSort(E[] L, String ascOrDesc){

        boolean flag = true;
        for(int i = 1; i < L.length && flag == true; i++){
            flag = false;
            for(int j = 0; j < L.length - 1; j++){
                if(ascOrDesc == "asc"){
                    if(L[j].compareTo(L[j + 1]) > 0){
                        s.swap(L, j, j + 1);
                        flag = true;
                    }
                }else{
                    if(L[j].compareTo(L[j + 1]) < 0){
                        s.swap(L, j, j + 1);
                        flag = true;
                    }
                }
            }
        }
    }

插入排序:

import java.util.Arrays;
/*
* 插入排序的时间复杂度为O(n2),空间复杂度为O(1)
* */

public class InsertSort<E extends Comparable> {
    private Swap<E> s = new Swap<>();
    /*ascOrDesc=“asc“ 升序 ascOrDesc=”desc" 降序*/
    public void insertSort(E[] L, String ascOrDesc){

        for(int i = 1; i < L.length; i++){

            if(ascOrDesc == "asc"){
                if(L[i].compareTo(L[i - 1]) < 0){
                    E temp = L[i];
                    int flag = i;
                    /*找到第一个比L[i]小的位置插入*/
                    for(int j = i - 1; j >= 0; j--){
                        if(L[j].compareTo(temp) > 0){
                            
                            L[j + 1] = L[j];
                            flag = j;
                        }else{
                            break;
                        }
                    }
                    L[flag] = temp;
                }
            }else{
                if(L[i].compareTo(L[i - 1]) > 0){
                    E temp = L[i];
                    int flag = i;
                    /*找到第一个比L[i]大的位置插入*/
                    for(int j = i - 1; j >= 0; j--){
                        if(L[j].compareTo(temp) < 0){
                            L[j + 1] = L[j];
                            flag = j;
                        }else{
                            break;
                        }
                    }
                    L[flag] = temp;
                }
            }

        }

    }

    public static void main(String[] args) {
        InsertSort<Integer> insertSort = new InsertSort<>();
        Integer[] data = {3, 2, 6, 1, 0, 4};
        insertSort.insertSort(data, "desc");
        System.out.println(Arrays.toString(data));
    }
}

你可能感兴趣的:(JAVA,数据结构与算法,JAVA,冒泡排序)