簡單排序算法

今天無聊的寫了幾行簡單排序算法,能夠參考一下。。。算法

簡單介紹一下簡單排序算法:spa

1.冒泡排序:0~N-1一次比較,若是左邊的>右邊那麼Swap。而後依次向右移動一次繼續進行比較。3d

      第一趟最多比較N-1,第二趟最多比較N-2以此類推最後最多比較1次結束。code

      對於10個數據就是9+8+7+6+5+4+3+2+1=45次。Big O=N的2次方。orm

2.選擇排序:選擇一個最小的數字,與最左端的交換位置。blog

      第一趟選擇最小的放到索引爲0的位置,第二趟放到索引爲1的位置以此類推直到最後一個。排序

      交換次數Big O=N,比較次數Big O=N的2次方。索引

3.插入排序:通常用於局部有序的狀況。選擇一個位置(1),一次向左比較是否大於選擇的數字而且知足當前索引大於0,get

      在比較時知足條件那麼向右移動補空洞。而後把選擇的數字插入合適的位置。具體以下:io

 

如下是main方法

     MyArrary array = new MyArrary(10);
        array.unorderedInsert(10);
        array.unorderedInsert(9);
        array.unorderedInsert(8);
        array.unorderedInsert(7);
        array.unorderedInsert(6);
        array.display();
        System.out.println("-------Bubble Sort");
        array.bubbleSort();
        array.display();
        System.out.println("-------Ordered Insert");
        int value = 0;
        while (!array.isPull()) {
            array.orderedInsert(++value);
        }
        array.display();
        System.out.println("-------Delete By Index 4");
        System.out.println("Delete:" + array.deleteByIndex(4));
        array.display();
        System.out.println("-------Delete By Value 1");
        System.out.println("Delete:" + array.deleteByValue(1));
        array.display();
        System.out.println("-------Unordered Insert value 1");
        array.unorderedInsert(1);
        array.display();
        System.out.println("-------Insertion Sort");
        array.insertionSort();
        array.display();
        System.out.println("-------Unordered Insert value 5");
        array.unorderedInsert(5);
        array.display();
        System.out.println("-------Selection Sort");
        array.selectionSort();
        array.display();
        System.out.println("-------Find Value 3 By Binary");
        System.out.println("Find Index: "+array.findByBinary(3));
        System.out.println("-------Find Value 3 By Normal");
        System.out.println("Find Index: "+array.findByBinary(3));
        System.out.println("-------Delete ALL");
        while (!array.isEmpty()) {
            array.deleteByIndex(0);
        }
        System.out.println("Is Empty: " + array.isEmpty());

如下是MyArrary 類

public class MyArrary {
    private long[] arry;
    private int elems;

    public MyArrary(int size) {
        arry = new long[size];
        elems = 0;
    }

    public void unorderedInsert(long value) {
        arry[elems++] = value;
    }

    public void orderedInsert(long value) {
        int i = 0;
        for (; i < elems; i++)
            if (arry[i] > value)
                break;
        for (int j = elems; j > i; j--)
            arry[j] = arry[j - 1];
        arry[i] = value;
        elems++;
    }

    public boolean deleteByIndex(int index) {
        if (index < 0 || index == elems)
            return false;
        for (int i = index; i < elems - 1; i++) {
            arry[i] = arry[i + 1];
        }
        elems--;
        return true;
    }

    public boolean deleteByValue(long value) {
        int index = findByBinary(value);
        if (index == -1)
            return false;
        for (int i = index; i < elems - 1; i++) {
            arry[i] = arry[i + 1];
        }
        elems--;
        return true;
    }

    public long getElementByIndex(int index) {
        return arry[index];
    }

    public int findByNormal(long value) {
        int i = 0;
        for (; i < elems; i++)
            if (arry[i] == value)
                break;
        if (i == elems)
            return -1;
        return i;
    }

    public int findByBinary(long value) {
        int upper = elems;
        int lower = 0;
        int cur;
        while (true) {
            cur = (upper + lower) / 2;
            if (arry[cur] == value)
                return cur;
            else if (lower > upper)
                return -1;
            else {
                if (arry[cur] < value)
                    lower = cur + 1;
                else
                    upper = cur - 1;
            }
        }
    }

    public void bubbleSort() {
        for (int i = 0; i < elems - 1; i++)
            for (int j = 0; j < elems - i - 1; j++)
                if (arry[j] > arry[j + 1]) {
                    swap(j, j + 1);
                }
    }

    public void selectionSort() {
        int min;
        for (int i = 0; i < elems - 1; i++) {
            min = i;
            for (int j = i + 1; j < elems; j++)
                if (arry[min] > arry[j])
                    min = j;
            swap(i, min);
        }
    }

    public void insertionSort() {
        long temp;
        int swapIndex;
        for (int i = 1; i < elems; i++) {
            temp = arry[i];
            for (swapIndex = i; swapIndex > 0 && arry[swapIndex - 1] > temp; swapIndex--)
                arry[swapIndex] = arry[swapIndex - 1];
            arry[swapIndex] = temp;
        }
    }

    public void swap(int from, int to) {
        long temp = arry[from];
        arry[from] = arry[to];
        arry[to] = temp;
    }

    public void display() {
        for (int i = 0; i < elems; i++) {
            System.out.print(arry[i] + " ");
        }
        System.out.println();
    }

    public boolean isEmpty() {
        return elems == 0;
    }

    public boolean isPull() {
        return elems == arry.length;
    }
}

如下是運行結果:

相關文章
相關標籤/搜索