zoukankan      html  css  js  c++  java
  • 排序算法 java实现2

    继续排序算法

    4.冒泡排序

    从第一个开始,跟后面一个数比较,如果比后面大就交换位置,这样没完成一轮,就可以把最大的选出来

        public static <T extends Comparable<T>> T[] genericBubbleSort(T[] a) {
            int n = a.length;
            for (int i = 0; i < n - 1; i++) {
                for (int j = 0; j < n - i - 1; j++) {
                    if (a[j].compareTo(a[j + 1]) > 0) {
                        T temp = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = temp;
                    }
                }
            }
            return a;
        }

    改进上面的冒泡排序

    方案一:设置一标志性变量pos,用于记录每趟排序中最后一次进行交换的位置。由于pos位置之后的记录均已交换到位,

    故在进行下一趟排序时只要扫描到pos位置即可

        public static <T extends Comparable<T>> T[] genericBubbleSortGai(T[] a) {
            int n = a.length;
            int i = n - 1;
            while (i > 0) {
                int pos = 0;
                for (int j = 0; j < i; j++) {
                    if (a[j].compareTo(a[j + 1]) > 0) {
                        pos = j;
                        T temp = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = temp;
                    }
                }
                i = pos;
            }
            return a;
        }

    改进方案二:

    两边同时进行  先找最大的  然后找最小的

        public static <T extends Comparable<T>> T[] genericBubbleSortGai2(T[] a) {
            int n = a.length;
            int low = 0, high = n - 1;
            int j;
            T tmp;
            while (low < high) {
                for (j = low; j < high; j++) {
                    if (a[j].compareTo(a[j + 1]) > 0) {
                        tmp = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = tmp;
                    }
                }
                high--;
                for (j = high; j > low; j--) {
                    if (a[j].compareTo(a[j - 1]) < 0) {
                        tmp = a[j];
                        a[j] = a[j - 1];
                        a[j - 1] = tmp;
                    }
                }
                low++;
            }
            return a;
        }

    改进方案三:

    设置一个标志  如果有一趟没有发生交换  则排序完成

        public static <T extends Comparable<T>> T[] genericbubblesortgai3(T[] a) {
            int n = a.length;
            boolean flag = true;
            for (int i = 0; i < n - 1; i++) {
                if (!flag) {
                    return a;
                }
                flag = false;
                for (int j = 0; j < n - i - 1; j++) {
                    if (a[j].compareTo(a[j + 1]) > 0) {
                        flag = true;
                        T temp = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = temp;
                    }
                }
    
            }
            return a;
        }

    5.快速排序

    <快速排序> 基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,
    * 然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。快速排序是一种不稳定的排序算法

        public static <T extends Comparable<T>> T[] QuickSortStart(T[] list) {
            quickSort(list, 0, list.length - 1);
            return list;
        }
    
        private static <T extends Comparable<T>> void quickSort(T[] list,
                int first, int last) {
            if (last > first) {
                int povitIndex = partition(list, first, last);
                quickSort(list, first, povitIndex - 1);
                quickSort(list, povitIndex + 1, last);
            }
        }
    
        private static <T extends Comparable<T>> int partition(T[] list, int first,
                int last) {
            /*
             * 把数组分为两组,将比povit小的数放在它前面,比povit大的数放在它后面
             */
            T povit = list[first];
            int low = first + 1;
            int high = last;
    
            while (high > low) {
                while (high > low && list[low].compareTo(povit) <= 0)
                    low++;
                while (low <= high && list[high].compareTo(povit) > 0)
                    high--;
                if (high > low) {
                    T temp = list[high];
                    list[high] = list[low];
                    list[low] = temp;
                }
            }
            while (high > first && list[high].compareTo(povit) >= 0)
                high--;
            if (povit.compareTo(list[high]) > 0) {
                list[first] = list[high];
                list[high] = povit;
                return high;
            }
            return first;
        }

    6.归并排序

    将一个序列一直对半拆分,知道不能拆分,然后开始归并,归并采用插入排序

        public static <T extends Comparable<T>> T[] mergesort(T[] a) {
            T[] temp = a.clone();
            a = msort(a, temp, 0, a.length);
            return a;
        }
    
        public static <T extends Comparable<T>> T[] msort(T[] a, T[] temp,
                int first, int last) {
            if (first + 1 < last) {
                int mid = (first + last) / 2;
                msort(a, temp, first, mid);
                msort(a, temp, mid, last);
    
                int index1 = first;
                int index2 = mid;
                int index3 = first;
                while (index1 < mid && index2 < last) {
                    if (a[index1].compareTo(a[index2]) < 0) {
                        temp[index3] = a[index1];
                        index1++;
                    } else {
                        temp[index3] = a[index2];
                        index2++;
                    }
                    index3++;
                }
                while (index1 < mid) {
                    temp[index3++] = a[index1++];
                }
                while (index2 < last) {
                    temp[index3++] = a[index2++];
                }
                for (int i = first; i < last; i++)
                    a[i] = temp[i];
    
            }
            return a;
        }

    感觉就是把代码复制上去了,说的不是很清楚,,,还有一个堆排序没有写

    最后对上面的排序算法做了下测试,生成10000个int数,进行排序,算时间

    1.归并排序  50~60 ms之间

    2.简单选择排序 135~155 ms之间

    3.选择排序 220 ms    //可能是我数据没选好,10000个数,生成的随机数也是0~10000

    4.快速排序  30ms  左右

    5.冒泡排序 改进方案二快点  280ms

    6.插入排序  70ms左右

    然后还是10000个随机数,范围换成0~100000,结果基本每种排序的时间更短

    所以排序的时间长短是和数据本身有关系的

  • 相关阅读:
    ASP.NET 错误
    linux下使用蓝牙设备【转】
    AIDL Android中的远程接口 [转]
    Handler理解
    Hid Report Descriptor
    Android kernel x86 编译方法
    Android Init Language
    DBUS 资源
    Analysing Bluetooth Keyboard Traffic with hcidump
    DBUS基础知识
  • 原文地址:https://www.cnblogs.com/luolei/p/4681232.html
Copyright © 2011-2022 走看看