随机快速排序的java代码

来源:互联网 发布:把字符串添加到数组 编辑:程序博客网 时间:2024/06/05 07:57

public class Randomized_QuickSort {

    public static void main(String[] args) {
          Randomized_QuickSort rq = new Randomized_QuickSort();
                  int a[] = { 54, 23, 75, 42, 45, 21, 54, 23, 75, 42, 45, 65, 22, 38, 96,10, 8, 11, 3, 40, 1001, 88,

                  210, 51, 15, 97, 29, 48, 61, 34, 78, 94, 64, 6, 95, 4, 15, 97, 29, 48, 61, 34, 78, 94, 71 };

                  System.out.println("a的长度为:" + a.length);
                  System.out.println("排序前:");
                  int col = 1;
                  for (int j = 0; j < a.length; j++) {
                        // 每行显示10个数据
                        if (j >= col * 15 - 1) {
                                  System.out.print(a[j] + " ");
                                  System.out.println();
                                  col++;
                        } else
                                 System.out.print(a[j] + " ");
                  }

                  int p = 0; // 数组的下标
                  int r = a.length - 1; // 数组的上标
                  rq.RandomizedQuickSort(a, p, r);
                  System.out.println("排序后:");
                  int c = 1;
                  for (int j = 0; j < a.length; j++) {
                          if (j >= c * 15 - 1) {
                                 System.out.print(a[j] + " ");
                                 System.out.println();
                                 c++;
                          } else
                                 System.out.print(a[j] + " ");
                   }
         }

 /*
  * 确定一个基准元素a[p],对子数组a[p:r]进行划分 错误:如果基准元素为1001即为最大的元素的时候,则排序会出现错误,why????
  */
              public int Partitions(int a[], int p, int r) {
                          int i = p;
                          int j = r + 1;
                          int x = a[p]; // x=a[p]为基准元素

                          while (true) {
                           // 从数组的两边开始检查,直到i>=j为止

   /*
    * 这里为什么不使用i++而使用++i的原因为:
    * 1、++i表示先加了之后再做循环,i++表示先循环一次之后再加1
    * 1)、这有个好处,就是防止了和基准元素比较的可能,因为基准元素始终都是在a[0]的位置上.
    * 2)、最后一次循环的时候,数组下标最大为a.length-1,不至于数组下标越界,即出现指针为空的异常 
    * 对于--j也是同样的道理
    */
                                    while (a[++i] < x) ;  // 找到a[i]小于x的数,如果没有,则i=a.length-1
                                    while (a[--j] > x);      // 找到a[j]大于x的数     

                                     if (i >= j) break;
                                    Swap(a, i, j);
                           }
                           a[p] = a[j];
                           a[j] = x;  
                           return j;
                 }

               //把数组a中的元素a[i]和a[j]交换位置

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

                   int RandomizedPartition(int a[], int p, int r) {
                            // 下面的代码是实现在整数M和N之间取得随机整数c(N<=M)
                            int N = p, M = r;
                            int A = M - N;
                            double B = Math.random() * A;
                            int c = (int) B + N;
                            Swap(a, c, p);
                            return Partitions(a, p, r);
                 }

                 void RandomizedQuickSort(int a[], int p, int r) {
                            if (p < r) {
                                     int q = RandomizedPartition(a, p, r);
                                     RandomizedQuickSort(a, p, q - 1);
                                     RandomizedQuickSort(a, q + 1, r);
                         }
               }
}

排序后为:

3 4 6 8 10 11 15 15 21 22 23 23 29 29 34 34 38 40 42 42 45 45 48 48 51 54 54 61 61 64
65 71 75 75 78 78 88 94 94 95 96 97 97 210 1001

 

这是根据王晓东版的计算机算法设计与分析编写的随机快速排序算法,

但是以上还存在一点问题,至今都无法解决:如果当基准元素为数组a【p:r】中的最大元素时,即为a[a.length-1]时,则出现如下异常:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 45

即数组索引越界异常,不知道怎么修改,哎!!!!

初学中~~~~~~~~~~~~~~~~~~~~~~~

还得加油啊!

 

原创粉丝点击