1 | /* |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | |
---|
19 | package org.apache.hadoop.util; |
---|
20 | |
---|
21 | import java.util.Comparator; |
---|
22 | import org.apache.hadoop.io.IntWritable; |
---|
23 | |
---|
24 | /** An implementation of the core algorithm of MergeSort. */ |
---|
25 | public class MergeSort { |
---|
26 | //Reusable IntWritables |
---|
27 | IntWritable I = new IntWritable(0); |
---|
28 | IntWritable J = new IntWritable(0); |
---|
29 | |
---|
30 | //the comparator that the algo should use |
---|
31 | private Comparator<IntWritable> comparator; |
---|
32 | |
---|
33 | public MergeSort(Comparator<IntWritable> comparator) { |
---|
34 | this.comparator = comparator; |
---|
35 | } |
---|
36 | |
---|
37 | public void mergeSort(int src[], int dest[], int low, int high) { |
---|
38 | int length = high - low; |
---|
39 | |
---|
40 | // Insertion sort on smallest arrays |
---|
41 | if (length < 7) { |
---|
42 | for (int i=low; i<high; i++) { |
---|
43 | for (int j=i;j > low; j--) { |
---|
44 | I.set(dest[j-1]); |
---|
45 | J.set(dest[j]); |
---|
46 | if (comparator.compare(I, J)>0) |
---|
47 | swap(dest, j, j-1); |
---|
48 | } |
---|
49 | } |
---|
50 | return; |
---|
51 | } |
---|
52 | |
---|
53 | // Recursively sort halves of dest into src |
---|
54 | int mid = (low + high) >>> 1; |
---|
55 | mergeSort(dest, src, low, mid); |
---|
56 | mergeSort(dest, src, mid, high); |
---|
57 | |
---|
58 | I.set(src[mid-1]); |
---|
59 | J.set(src[mid]); |
---|
60 | // If list is already sorted, just copy from src to dest. This is an |
---|
61 | // optimization that results in faster sorts for nearly ordered lists. |
---|
62 | if (comparator.compare(I, J) <= 0) { |
---|
63 | System.arraycopy(src, low, dest, low, length); |
---|
64 | return; |
---|
65 | } |
---|
66 | |
---|
67 | // Merge sorted halves (now in src) into dest |
---|
68 | for (int i = low, p = low, q = mid; i < high; i++) { |
---|
69 | if (q < high && p < mid) { |
---|
70 | I.set(src[p]); |
---|
71 | J.set(src[q]); |
---|
72 | } |
---|
73 | if (q>=high || p<mid && comparator.compare(I, J) <= 0) |
---|
74 | dest[i] = src[p++]; |
---|
75 | else |
---|
76 | dest[i] = src[q++]; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | private void swap(int x[], int a, int b) { |
---|
81 | int t = x[a]; |
---|
82 | x[a] = x[b]; |
---|
83 | x[b] = t; |
---|
84 | } |
---|
85 | } |
---|