zoukankan      html  css  js  c++  java
  • 写了个快速排序

    写了个快速排序,以后用的着的话直接拿来用。:

    package algorithm;

    import java.util.Arrays;
    import java.util.HashSet;
    import java.util.Random;

    public class QuikSort {
    static int array[];// the array

    /**
    * 快速排序
    *
    * @param left
    *            0
    * @param right
    *            length-1
    */
    static void quikSort(int left, int right) {
       if (right - left <= 0)
        return;
       else {

        int p = partition(left, right);
        quikSort(left, p - 1);
        quikSort(p + 1, right);
       }
    }

    /**
    * 找出划分位置
    *
    * @param leftP
    * @param rightP
    * @return
    */
    private static int partition(int leftP, int rightP) {
       int pivot = array[leftP];//枢轴
       int left = leftP;
       int right = rightP;
       while (left < right) {
        while (left < rightP && array[left] < pivot)//大于枢轴
         left++;
        while (right > leftP && array[right] > pivot)//小于枢轴
         right--;
        swap(left, right);//交换
       }
       return left;
    }

    private static void swap(int left, int right) {
       int temp = array[left];
       array[left] = array[right];
       array[right] = temp;
    }

    /**
    * 产生m个1-n之间的随机数,无重复元素,使用的算法是Floyd提出的算法
    *
    * @param m
    * @param n
    * @return
    */
    static int[] randomFloyd(int m, int n) {
       int index = 0;
       Random r = new Random();
       HashSet<Integer> hs = new HashSet<Integer>();
       for (int i = n - m + 1; i <= n; i++) {
        int k = r.nextInt(i);
        if (!hs.contains(k))
         hs.add(k);
        else
         hs.add(i);
        index++;
       }
       array = new int[hs.size()];
       index = 0;
       for (Integer integer : hs) {
        array[index++] = integer;
       }
       return array;

    }

    public static void main(String[] args) {
       array = randomFloyd(200, 1000);//产生0-1000的200个随机数
       System.out.println("初始化数组:"+Arrays.toString(array));
       quikSort(0, array.length - 1);//排序
       System.out.println("排序后"+Arrays.toString(array));
    }
    }
    运行结果:

    初始化数组:[7, 545, 559, 11, 554, 553, 566, 563, 23, 22, 33, 39, 36, 37, 40, 46, 520, 532, 529, 531, 541, 57, 609, 71, 615, 66, 618, 72, 625, 86, 630, 83, 93, 92, 634, 89, 88, 637, 580, 581, 583, 584, 111, 108, 106, 588, 590, 591, 105, 113, 112, 120, 139, 681, 678, 128, 131, 130, 132, 672, 152, 153, 701, 699, 148, 691, 172, 163, 162, 161, 641, 640, 164, 671, 665, 179, 176, 182, 658, 181, 205, 751, 750, 195, 740, 762, 220, 216, 767, 752, 215, 712, 716, 233, 719, 705, 231, 708, 224, 729, 252, 250, 733, 249, 722, 724, 820, 829, 828, 280, 830, 825, 258, 805, 259, 810, 791, 309, 315, 314, 792, 291, 292, 295, 298, 780, 779, 776, 880, 340, 886, 889, 351, 888, 350, 890, 345, 327, 323, 871, 878, 369, 371, 852, 834, 356, 359, 838, 353, 839, 836, 367, 362, 957, 412, 402, 407, 945, 404, 395, 393, 942, 936, 396, 385, 926, 925, 922, 445, 446, 432, 918, 434, 435, 913, 427, 426, 429, 896, 476, 467, 462, 461, 460, 992, 995, 452, 998, 987, 505, 507, 498, 493, 970, 968, 973, 490, 484, 962, 961]


    排序后[7, 11, 22, 23, 33, 36, 37, 39, 40, 46, 57, 66, 71, 72, 83, 86, 88, 89, 92, 93, 105, 106, 108, 111, 112, 113, 120, 128, 130, 131, 132, 139, 148, 152, 153, 161, 162, 163, 164, 172, 176, 179, 181, 182, 195, 205, 215, 216, 220, 224, 231, 233, 249, 250, 252, 258, 259, 280, 291, 292, 295, 298, 309, 314, 315, 323, 327, 340, 345, 350, 351, 353, 356, 359, 362, 367, 369, 371, 385, 393, 395, 396, 402, 404, 407, 412, 426, 427, 429, 432, 434, 435, 445, 446, 452, 460, 461, 462, 467, 476, 484, 490, 493, 498, 505, 507, 520, 529, 531, 532, 541, 545, 553, 554, 559, 563, 566, 580, 581, 583, 584, 588, 590, 591, 609, 615, 618, 625, 630, 634, 637, 640, 641, 658, 665, 671, 672, 678, 681, 691, 699, 701, 705, 708, 712, 716, 719, 722, 724, 729, 733, 740, 750, 751, 752, 762, 767, 776, 779, 780, 791, 792, 805, 810, 820, 825, 828, 829, 830, 834, 836, 838, 839, 852, 871, 878, 880, 886, 888, 889, 890, 896, 913, 918, 922, 925, 926, 936, 942, 945, 957, 961, 962, 968, 970, 973, 987, 992, 995, 998]

  • 相关阅读:
    数组相关操作
    控制结构和函数
    调用函数和方法
    常用类型、算术和操作符重载
    函数式 CSS (FCSS)
    Javascript 封装问题
    认识javascript中的作用域和上下文
    Javascript 继承-原型的陷阱
    解耦你的HTML,CSS和JAVASRIPT
    网格如此简单
  • 原文地址:https://www.cnblogs.com/macula7/p/1960437.html
Copyright © 2011-2022 走看看