java排序

    排序是一个你想的越久,就会越明白的脑洞,快速排序和堆排序是研究最久的,它开始让我觉得程序寥寥数行,却透彻着开发者精妙的思想。

废话不多说,摆代码:

package teacherwoek;

public class Sort {
    // 选择排序
    public void selectionSort(int[] a) {
        for (int i = 0; i < a.length; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if (a[i] > a[j])
                    swap(a, i, j);
            }
        }
    }

    private void swap(int[] a, int i, int j) {
        int temp;
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

    // 冒泡排序
    public void bubbleSort(int[] a) {
//        int bound = a.length;
//       
//        for (int i=0; i< bound-1; i++) {
//            for (int j = 0; j < bound - 1; j++) {
//                if (a[j] > a[j + 1]) {
//                    swap(a, j, j + 1);
//                }
//            }
//        }
//另一种实现
int n = 0; for (int i = 0; i < a.length - 1; i++) { // 从数组末尾开始 for (int j = a.length - 1; j > i; j--) { n++; // 后面的小 if (a[j] < a[j-1]) { swap(a, j, j-1); } } } System.out.println(n); } // 归并排序 这个还没研究透,从网上拷贝的。。。 /** * 归并排序是稳定排序,它也是一种十分高效的排序,能利用完全二叉树特性的排序一般性能都不会太差。 * java中Arrays.sort()采用了一种名为TimSort的排序算法,就是归并排序的优化版本。 * 从上文的图中可看出,每次合并操作的平均时间复杂度为O(n) * ,而完全二叉树的深度为|log2n|。总的平均时间复杂度为O(nlogn)。而且,归并排序的最好,最坏,平均时间复杂度均为O(nlogn)。 * @param a */ public void mergeSort(int[] a) { int[] temp = new int[a.length]; mergeSort(a,0,a.length-1,temp); } private void mergeSort(int[] a, int i, int j, int[] temp) { if(i<j) { int mid = (i+j)/2; mergeSort(a,i,mid,temp); mergeSort(a,mid+1,j,temp); merge(a,i,mid,j,temp); } } private void merge(int[] a, int i, int mid, int j, int[] temp) { int left = i ; int right = mid+ 1; int t = 0; while(left<=mid && right <= j) { if(a[left] <= a[right]) { temp[t++] = a[left++]; }else { temp[t++] = a[right++]; } } while(left <= mid) { temp[t++] = a[left++]; } while(right<=j) { temp[t++] = a[right++]; } t= 0 ; while(i<=j ) { a[i++] = temp[t++]; } } // 基数 这个也得好好研究,日后完善吧 public void radixSort(int[] a) { } // 快速排序,left为左边界,right为右边界 public void quickSort(int[] a, int left, int right) { int i = left, j = right - 1; int k = a[i]; // 比较的基准值 while (i < j) { while (a[i] < k) { i++; } while (a[j] > k) { j--; } if (i < j) { swap(a, i, j); i++; j--; } } if (i == j) { i++; } if (j > left) { quickSort(a, left, j + 1); } if (i < right) { quickSort(a, i, right); } } // 堆排序 public void HeapSort(int[] a) { int k = a.length, j, temp; // 构造堆 for (int i = a.length / 2 - 1; i >= 0; i--) { j = 2 * i + 1; while (2 * i + 1 < k) { if (j + 1 < k) { if (a[j] < a[j + 1]) j++; } if (a[i] < a[j]) { swap(a, i, j); i = j; } else { break; } } } // 取堆元素 for (int i = k - 1; i > 0; i--) { swap(a, 0, i); int x = 0; while (2 * x + 1 < i) { temp = 2 * x + 1; if (temp + 1 < i) { if (a[temp] < a[temp + 1]) temp++; } if (a[x] < a[temp]) { swap(a, x, temp); x = temp; } else { break; } } } } // 插入排序 public void insertSort(int[] a) { // int i = 0, j = 0, k = 0; // while (i < a.length - 1) { // if (a[i] > a[i + 1]) { // k = a[i + 1]; // j = i; // while (j >= 0 && a[j] > k) { // a[j + 1] = a[j]; // j--; // } // a[j + 1] = k; // } // i++; // } for(int i=0;i<a.length;i++) { int temp = a[i]; int j=i-1; for(;j>0;j--) { if(a[i]<a[j]) { a[j+1] = a[j]; }else { break; } } a[j+1] = temp; } } // 希尔排序 又称之为缩小增量排序 是一种改进版的插入排序 public void shellSort(int[] a) { int x = a.length; for (x = x / 2; x >= 1; x = x / 2) { for (int i = 0; i + x < a.length; i++) { int temp = a[i + x]; int j = i; while (j >= 0 && a[j] > temp) { a[j + x] = a[j]; j -= x; } a[j + x] = temp; } } } public static void main(String[] args) { int[] a = { 1, 24, 5, 3, 9, 8, 7, 4, 10, 19, 12 }; Sort sort = new Sort(); // sort.selectionSort(a); sort.mergeSort(a); // sort.bubbleSort(a); // sort.quickSort(a, 0, a.length); // sort.insertSort(a); // sort.HeapSort(a); // sort.shellSort(a); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }

 

好好生活,天天向上
原文地址:https://www.cnblogs.com/linchongatfirst/p/9527196.html