现在的位置: 首页 > 综合 > 正文

算法—–快速排序

2013年12月05日 ⁄ 综合 ⁄ 共 1002字 ⁄ 字号 评论关闭
  1. package com.eshore.sweetop.sort;
  2. import java.util.Arrays;
  3. public class Quick {
  4.     private static int partition(int[] a, int p, int r) {
  5.         int x = a[r];
  6.         int i = p - 1;
  7.         for (int j = p; j < r; j++) {
  8.             if (a[j] < x) {
  9.                 i++;
  10.                 if (i != j) {
  11.                     a[i] ^= a[j];
  12.                     a[j] ^= a[i];
  13.                     a[i] ^= a[j];
  14.                 }
  15.             }
  16.         }
  17.         if (i + 1 != r) {
  18.             a[i + 1] ^= a[r];
  19.             a[r] ^= a[i + 1];
  20.             a[i + 1] ^= a[r];
  21.         }
  22.         return i + 1;
  23.     }
  24.     public static void sort2(int[] a, int p, int r) {
  25.         if (p < r) {
  26.             int q = partition(a, p, r);
  27.             sort2(a, p, q - 1);
  28.             sort2(a, q + 1, r);
  29.         }
  30.     }
  31.     public static void sort(int[] a) {
  32.         sort2(a, 0, a.length - 1);
  33.     }
  34.     public static void main(String[] args) {
  35.         int[] a = { 34111368926 };
  36.         sort(a);
  37.         System.out.println(Arrays.toString(a));
  38.     }
  39. }

抱歉!评论已关闭.