【算法】排序算法

来源:互联网 发布:小学女生内衣淘宝 编辑:程序博客网 时间:2024/05/14 04:46
 现在在使用Java语言,也许用Java实现一些算法也是一件很有趣的事情哦。也算是温故而知新吧。

首先还是实现一些简单的排序算法吧。为了简单起见,只实现了升序排序。在我的机器上用QuickSort排序100万个随机整数花费1.6秒左右

  1. package cn.tenyears.demo;
  2. /**
  3.  * implements some typical sort algorithms. includes selection sort, bubble
  4.  * sort, insertion sort, quicksort.
  5.  * @author taolue
  6.  */
  7. public class Sort {
  8.   private Sort() {
  9.   }
  10.   private static <T> void swap(T[] a, int first, int second) {
  11.     T temp = a[first];
  12.     a[first] = a[second];
  13.     a[second] = temp;
  14.   }
  15.   @SuppressWarnings("unchecked")
  16.   public static void selectionSort(Comparable[] a) {
  17.     int size = a.length;
  18.     for (int index = 0; index < size - 1; index++) {
  19.       int small = index;
  20.       for (int index2 = index + 1; index2 < size; index2++) {
  21.         if (a[index2].compareTo(a[small]) < 0)
  22.           small = index2;
  23.       }
  24.       swap(a, index, small);
  25.     }
  26.   }
  27.   @SuppressWarnings("unchecked")
  28.   public static void bubbleSort(Comparable[] a) {
  29.     int size = a.length;
  30.     for (int index = 0; index < size - 1; index++) {
  31.       for (int index2 = index + 1; index2 < size; index2++) {
  32.         if (a[index].compareTo(a[index2]) > 0) {
  33.           swap(a, index, index2);
  34.         }
  35.       }
  36.     }
  37.   }
  38.   @SuppressWarnings("unchecked")
  39.   public static void insertionSort(Comparable[] a) {
  40.     int size = a.length;
  41.     int index2;
  42.     Comparable temp;
  43.     for (int index = 1; index < size; index++) {
  44.       index2 = index;
  45.       temp = a[index];
  46.       while (index2 > 0 && temp.compareTo(a[index2 - 1]) < 0) {
  47.         swap(a, index2, index2 - 1);
  48.         index2--;
  49.       }
  50.     }
  51.   }
  52.   public static void quickSort(Comparable[] a) {
  53.     quickSortInternal(a, 0, a.length - 1);
  54.   }
  55.   @SuppressWarnings("unchecked")
  56.   private static void quickSortInternal(Comparable[] a, int low, int high) {
  57.     Comparable pivot;
  58.     int scanUp, scanDown;
  59.     int mid;
  60.     if (high - low <= 0)
  61.       return;
  62.     if (high - low <= 1) {
  63.       if (a[high].compareTo(a[low]) < 0)
  64.         swap(a, high, low);
  65.       return;
  66.     }
  67.     mid = (low + high) / 2;
  68.     pivot = a[mid];
  69.     swap(a, mid, low);
  70.     scanUp = low + 1;
  71.     scanDown = high;
  72.     do {
  73.       while (scanUp <= scanDown && a[scanUp].compareTo(pivot) < 0)
  74.         scanUp++;
  75.       while (scanDown >= scanUp && pivot.compareTo(a[scanDown]) <= 0)
  76.         scanDown--;
  77.       if (scanUp < scanDown)
  78.         swap(a, scanUp, scanDown);
  79.     } while (scanUp < scanDown);
  80.     a[low] = a[scanDown];
  81.     a[scanDown] = pivot;
  82.     if (low < scanDown - 1)
  83.       quickSortInternal(a, low, scanDown - 1);
  84.     if (scanDown + 1 < high)
  85.       quickSortInternal(a, scanDown + 1, high);
  86.   }
  87.   public static <T> void printA(T[] a) {
  88.     for (int i = 0; i < a.length; i++) {
  89.       if (i != 0 && i % 20 == 0)
  90.         System.out.println();
  91.       System.out.print(a[i] + " ");
  92.     }
  93.     System.out.println();
  94.   }
  95.   public static void main(String[] args) {
  96.     int size = 1000000;
  97.     Integer[] a = new Integer[size];
  98.     for (int i = 0; i < size; i++)
  99.       a[i] = Integer.valueOf((int) Math.round(Math.random() * size));
  100.     long start = System.currentTimeMillis();
  101.     quickSort(a);
  102.     long end = System.currentTimeMillis();
  103.     System.out.println("Last: " + (end - start) + " ms");
  104.   }
  105. }
原创粉丝点击