1. 程式人生 > >java快速排序的遞迴版

java快速排序的遞迴版

       public static void quickSort(int[] array, int start, int end) {

              // 設計一個遞迴出口

             if (start >= end) {

                    return;  

            }

                 int s = start;

                 int e = end;

                 int temp = array[s];

            while ( s < e) {

                     while (s < e && temp < array[e]) {

                             e--;

                      }

                     if (s == e) {

                     break;               

                  }

                   array[e--] = array[s];

                    while (s < e && temp > array[s]) {

                              s++;

                   }

                       if (s == e) {

                               break;

                     }

                    array[s++] = array[e];

           }

                    array[s] = temp;

              quickSort(array, 0, s - 1);

              quickSort(array, s + 1, end);

  }