熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

各種排序算法的Java實現

2013-11-23 19:00:31  來源: Java核心技術 

  package comsxsexestudyalgorithm;

  

  public class SortTest {

  

      private static final int ARRAY_SEED = ;

      private static final int ARRAY_LENGTH = ;

      private static int[] array = new int[ARRAY_LENGTH];

  

      private static void initArray() {

  

          for (int i = ; i < ARRAY_LENGTH; i++) {

              array[i] = (int) (Mathrandom() * ARRAY_SEED);

          }

      }

  

      private static void printArray(boolean before) {

  

          String s = null;

          if (before)

              s = before :;

          else

              s = after :;

          Systemoutprint(s);

          for (int i = ; i < ARRAY_LENGTH; i++) {

              Systemoutprint(array[i] + );

          }

          Systemoutprintln( );

      }

  

      /**

       * @param args

       */

      public static void main(String[] args) {

  

          initArray();

          // printArray(true);

          long startTime = SystemcurrentTimeMillis();

          quickSort( ARRAY_LENGTH );

          long endTime = SystemcurrentTimeMillis();

          // printArray(false);

          Systemoutprintln(quick sort length : + ARRAY_LENGTH + took + (endTime startTime) + ms);

  

          initArray();

          // printArray(true);

          startTime = SystemcurrentTimeMillis();

          insertSort();

          endTime = SystemcurrentTimeMillis();

          // printArray(false);

          Systemoutprintln(insert sort length : + ARRAY_LENGTH + took + (endTime startTime) + ms);

  

          initArray();

          // printArray(true);

          startTime = SystemcurrentTimeMillis();

          selectSort();

          endTime = SystemcurrentTimeMillis();

          // printArray(false);

          Systemoutprintln(select sort length : + ARRAY_LENGTH + took + (endTime startTime) + ms);

  

          initArray();

          // printArray(true);

          startTime = SystemcurrentTimeMillis();

          bubbleSort();

          endTime = SystemcurrentTimeMillis();

          // printArray(false);

          Systemoutprintln(bubble sort length : + ARRAY_LENGTH + took + (endTime startTime) + ms);

  

          initArray();

          // printArray(true);

          startTime = SystemcurrentTimeMillis();

          binarySearchSort();

          endTime = SystemcurrentTimeMillis();

          // printArray(false);

          Systemoutprintln(binarySearchSort sort length : + ARRAY_LENGTH + took + (endTime startTime) + ms);

      }

  

      private static void quickSort(int left int right) {

  

          int key = array[left];

          int tmp;

          int start = left;

          int end = right;

  

          while (left < right) {

  

              // 首先從右向左找 找到一個小於key的值 進行交換 然後准備從左向右找

              if (array[right] >= key) {

                  right;

                  continue;

              }

              // 交換

              tmp = array[right];

              array[right] = array[left];

              array[left] = tmp;

  

              // 從左向右找 找到一個大於key的值進行交換 然後循環

              if (array[left] <= key) {

                  left++;

                  continue;

              }

              // 交換

              tmp = array[right];

              array[right] = array[left];

              array[left] = tmp;

          }

          // 以left right為新數組的起點和終點 遞歸調用

          if (start < left)

              quickSort(start left);

          if (right < end)

              quickSort(right + end);

  

          // Systemoutprintln(quick sort over);

      }

  

      private static void binarySearchSort() {

  

          int i = middle = tmp;

  

          for (; i < ARRAY_LENGTH; i++) {

  

              int low = ;

              int high = i ;

  

              tmp = array[i];

  

              while (low <= high) {

                  middle = (low + high) / ;

                  if (tmp > array[middle])

                      low = middle + ;

                  else

                      high = middle ;

              }

  

              for (int k = i; k > middle; k)

                  array[k] = array[k ];

              array[low] = tmp;

  

          }

      }

  

      private static void insertSort() {

  

          int i = j tmp;

  

          for (; i < ARRAY_LENGTH; i++) {

              tmp = array[i];

              j = i;

              while (j > && array[j ] > tmp) {

                  array[j] = array[j ];

                  j;

              }

              array[j] = tmp;

          }

      }

  

      private static void selectSort() {

  

          int i = minValue tmp;

          int j = i + ;

  

          for (; i < ARRAY_LENGTH ; i++) {

              minValue = array[i];

              for (j = i + ; j < ARRAY_LENGTH; j++) {

                  if (array[j] < minValue) {

                      minValue = array[j];

                      tmp = array[j];

                      array[j] = array[i];

                      array[i] = tmp;

                  }

              }

          }

      }

  

      private static void bubbleSort() {

  

          int i = ;

          int j tmp;

  

          for (; i < ARRAY_LENGTH; i++) {

              for (j = i + ; j < ARRAY_LENGTH; j++) {

                  if (array[j] < array[i]) {

                      tmp = array[j];

                      array[j] = array[i];

                      array[i] = tmp;

                  }

              }

          }

      }

  

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26125.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.