zoukankan      html  css  js  c++  java
  • 简单排序(冒泡排序,插入排序,选择排序)

                                    简单排序(冒泡排序,插入排序,选择排序)

    这些算法思想是比较简单的,执行速度也相对慢一些,不过,在某些情况下比那些复杂算法实际上还要好一些,比如,对于小规模的文件以及基本有序的文件,插入算法能比快速排序算法更为有效。


    如何排序:

        1   比较两个数据项

        2    交换两个数据想,或者复制其中的一项


    冒泡排序:





    代码:

    package SimpleSort.bubbleSort;
    
    public class ArrayBub {
        private long[] a;
        private int nElems;
    
        public ArrayBub(int max) {
            a = new long[max];
            nElems = 0;
        }
    
        public void insert(long value) {
            a[nElems] = value;
            nElems++;
        }
    
        public void display() {
            for (int j = 0; j < nElems; j++) {
                System.out.print(a[j] + " ");
            }
            System.out.println("");
        }
    
        public void bubbleSort() {
            int out, in;
            for (out = nElems - 1; out > 1; out--) {
                for (in = 0; in < out; in++) {
                    if (a[in] > a[in + 1]) {
                        swap(in, in + 1);
                    }
                }
            }
        }
    
        public void swap(int dex1, int dex2) {
            long temp = a[dex1];
            a[dex1] = a[dex2];
            a[dex2] = temp;
        }
    
        public static void main(String[] args) {
            int maxSize = 100;
            ArrayBub arr = new ArrayBub(maxSize);
    
            arr.insert(77);
            arr.insert(99);
            arr.insert(44);
            arr.insert(55);
            arr.insert(22);
            arr.insert(88);
            arr.insert(11);
            arr.insert(00);
            arr.insert(68);
            arr.insert(33);
    
            arr.display();
            arr.bubbleSort();
            arr.display();
        }
    }
    



    第一层for循环 out计数,从数组的最后开始,每确定一个个最大一个数字out--减少比较项。知道排序到第一个下标为0的。

    时间街边O(N2).

    交换比较是永远是相邻的数据项。


    选择排序:

    选择排序改进了冒泡排序,将必要的交换次数congO( N2)减少到O(N),但是比较次数仍保持为O(N2)

    代码:

    package SimpleSort.selectSort;
    
    public class ArraySel {
    
        private long[] a;
        private int nElems;
    
        public ArraySel(int max) {
            a = new long[max];
            nElems = 0;
        }
    
        public void insert(long value) {
            a[nElems] = value;
            nElems++;
        }
    
        public void display() {
            for (int j = 0; j < nElems; j++) {
                System.out.print(a[j] + " ");
            }
            System.out.println("");
        }
    
        public void selectSort() {
            int out, in, min;
            for (out = 0; out < nElems - 1; out++) {
                min = out;
                for (in = out + 1; in < nElems; in++) {
                    if (a[in] < a[min]) {
                        min = in;
                    }
                }
                swap(out, min);
            }
        }
    
        public void swap(int dex1, int dex2) {
            long temp = a[dex1];
            a[dex1] = a[dex2];
            a[dex2] = temp;
        }
    
        public static void main(String[] args) {
            int maxSize = 100;
            ArraySel arr = new ArraySel(maxSize);
    
            arr.insert(77);
            arr.insert(99);
            arr.insert(44);
            arr.insert(55);
            arr.insert(22);
            arr.insert(88);
            arr.insert(11);
            arr.insert(00);
            arr.insert(68);
            arr.insert(33);
    
            arr.display();
            arr.selectSort();
            arr.display();
        }
    }


    用一个临时变量min存储最小的那个值,如果碰到比min还夏小的,那么就将那个最小的值赋给min,然后min再进行比较,完成后将最小值放在下标为0的位置上,然后继续比较,out一样是计数,但是在最开始。与冒泡排序相比,减少了移动次数(复制),比较次数还是没有改变。


    插入排序

    比冒泡(快一倍)和选择排序(快一点)都要快,经常被用在比较复杂的排序算法的最后阶段,例如快速排序

    代码:

    package SimpleSort.insertSort;
    
    import SimpleSort.bubbleSort.ArrayBub;
    
    public class ArrayIns {
        private long[] a;
        private int nElems;
    
        public ArrayIns(int max) {
            a = new long[max];
            nElems = 0;
        }
    
        public void insert(long value) {
            a[nElems] = value;
            nElems++;
        }
    
        public void display() {
            for (int j = 0; j < nElems; j++) {
                System.out.print(a[j] + " ");
            }
            System.out.println("");
        }
    
        public void insertionSort() {
            int out, in;
            for (out = 1; out < nElems; out++) {
                long temp = a[out];
                in = out;
                while (in > 0 && a[in - 1] >= temp) {
                    a[in] = a[in - 1];
                    --in;
                }
                a[in] = temp;
            }
        }
    
        public static void main(String[] args) {
            int maxSize = 100;
            ArrayIns arr = new ArrayIns(maxSize);
    
            arr.insert(77);
            arr.insert(99);
            arr.insert(44);
            arr.insert(55);
            arr.insert(22);
            arr.insert(88);
            arr.insert(11);
            arr.insert(00);
            arr.insert(68);
            arr.insert(33);
    
            arr.display();
            arr.insertionSort();
            arr.display();
        }
    }
    


  • 相关阅读:
    volcanol的工控博客
    volcanol的工控博客
    volcanol的工控博客
    volcanol的工控博客
    volcanol的工控博客
    volcanol的工控博客
    volcanol的工控博客
    volcanol的工控博客
    volcanol的工控博客
    Oracle分析函数-排序排列(rank、dense_rank、row_number、ntile)
  • 原文地址:https://www.cnblogs.com/meiLinYa/p/9302985.html
Copyright © 2011-2022 走看看