MapReduce中的排序之 -- 快排

排序是大数据的核心精华.

快排和归并排序更是MapReduce中的精华部分. 

以下是快排部分.

一、百度百科: 

        通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列

二、通俗解释:

        拿到一组数据后, 随机选取一个element, 使其左边元素全部比其小, 右边元素全部比其大. 然后迭代这个逻辑, 直至左数组或右数组长度为一. 

三、Show you code:       

import org.junit.Test;

public class QuickSortTest {

	@Test
	public void test1() {

		// 1方式一, 初始化并带有固定的长度
		int[] a = new int[5];

		// 2 方式二, 初始化并给定值.
		int[] b = { 9, 5, 2, 7, 1 };

		quickSort(b, 0, 4);

	}

	/**
	 * 快速排序
	 *
	 * @param arr
	 * @param L   指向数组第一个元素
	 * @param R   指向数组最后一个元素
	 */
	public static void quickSort(int[] arr, int L, int R) {
		int i = L;
		int j = R;

		// 支点
		int pivot = arr[(L + R) / 2];

		// 左右两端进行扫描,只要两端还没有交替,就一直扫描
		while (i <= j) {

			// 寻找直到比支点大的数
			while (pivot > arr[i])
				i++;

			// 寻找直到比支点小的数
			while (pivot < arr[j])
				j--;

			// 此时已经分别找到了比支点小的数(右边)、比支点大的数(左边),它们进行交换
			if (i <= j) {
				int temp = arr[i];
				arr[i] = arr[j];
				arr[j] = temp;
				i++;
				j--;
			}
		}

		// 上面一个while保证了第一趟排序支点的左边比支点小,支点的右边比支点大了。

		// “左边”再做排序,直到左边剩下一个数(递归出口)
		if (L < j)
			quickSort(arr, L, j);

		// “右边”再做排序,直到右边剩下一个数(递归出口)
		if (i < R)
			quickSort(arr, i, R);
	}

}

四、MapReduce中的快排源码

package org.apache.hadoop.util;

import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;

/**
 * An implementation of the core algorithm of QuickSort.
 */
@InterfaceAudience.Private
@InterfaceStability.Unstable
public final class QuickSort implements IndexedSorter {

  private static final IndexedSorter alt = new HeapSort();

  public QuickSort() { }

  private static void fix(IndexedSortable s, int p, int r) {
    if (s.compare(p, r) > 0) {
      s.swap(p, r);
    }
  }

  /**
   * Deepest recursion before giving up and doing a heapsort.
   * Returns 2 * ceil(log(n)).
   */
  protected static int getMaxDepth(int x) {
    if (x <= 0)
      throw new IllegalArgumentException("Undefined for " + x);
    return (32 - Integer.numberOfLeadingZeros(x - 1)) << 2;
  }

  /**
   * Sort the given range of items using quick sort.
   * {@inheritDoc} If the recursion depth falls below {@link #getMaxDepth},
   * then switch to {@link HeapSort}.
   */
  @Override
  public void sort(IndexedSortable s, int p, int r) {
    sort(s, p, r, null);
  }

  @Override
  public void sort(final IndexedSortable s, int p, int r,
      final Progressable rep) {
    sortInternal(s, p, r, rep, getMaxDepth(r - p));
  }

  private static void sortInternal(final IndexedSortable s, int p, int r,
      final Progressable rep, int depth) {
    if (null != rep) {
      rep.progress();
    }
    while (true) {
    if (r-p < 13) {
      for (int i = p; i < r; ++i) {
        for (int j = i; j > p && s.compare(j-1, j) > 0; --j) {
          s.swap(j, j-1);
        }
      }
      return;
    }
    if (--depth < 0) {
      // give up
      alt.sort(s, p, r, rep);
      return;
    }

    // select, move pivot into first position
    fix(s, (p+r) >>> 1, p);
    fix(s, (p+r) >>> 1, r - 1);
    fix(s, p, r-1);

    // Divide
    int i = p;
    int j = r;
    int ll = p;
    int rr = r;
    int cr;
    while(true) {
      while (++i < j) {
        if ((cr = s.compare(i, p)) > 0) break;
        if (0 == cr && ++ll != i) {
          s.swap(ll, i);
        }
      }
      while (--j > i) {
        if ((cr = s.compare(p, j)) > 0) break;
        if (0 == cr && --rr != j) {
          s.swap(rr, j);
        }
      }
      if (i < j) s.swap(i, j);
      else break;
    }
    j = i;
    // swap pivot- and all eq values- into position
    while (ll >= p) {
      s.swap(ll--, --i);
    }
    while (rr < r) {
      s.swap(rr++, j++);
    }

    // Conquer
    // Recurse on smaller interval first to keep stack shallow
    assert i != j;
    if (i - p < r - j) {
      sortInternal(s, p, i, rep, depth);
      p = j;
    } else {
      sortInternal(s, j, r, rep, depth);
      r = i;
    }
    }
  }

}

引用:

https://juejin.im/post/5ab20ffcf265da239530de24

https://www.cnblogs.com/noKing/archive/2017/11/29/7922397.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值