forked from diwu001/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nuts_Bolts.java
49 lines (45 loc) · 1.76 KB
/
Nuts_Bolts.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*The problem also known as Keys and locks
* Given a set of n nuts of different sizes and n bolts of different sizes.
* There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.
* Constraint: Comparison of a nut to another nut or a bolt to another bolt is not allowed.
* It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.
* e.g. int[] a=[3,4,1,2], int[] b=[2,1,3,4] -> a=[1,2,3,4], b=[1,2,3,4]*/
public class Nuts_Bolts {
//Θ(nlogn) on average
public static void sort(int[] a, int[] b) {
if(a==null||a.length==0||b==null||b.length==0||a.length!=b.length) return;
int m=a.length;
helper(a,b,0,m-1);
for(int i=0;i<m;i++) System.out.print(a[i]+" ");
System.out.println();
for(int i=0;i<m;i++) System.out.print(b[i]+" ");
}
public static void helper(int[] a, int[] b, int low, int high) {
if(low>=high) return;
int pivot = partition(a,low,high,b[high]);
partition(b,low,high,a[pivot]);
helper(a,b,low,pivot-1);
helper(a,b,pivot+1,high);
}
public static int partition(int[] a, int low, int high, int pivot) {
int i = low;
for (int j = low; j < high; j++) {
if (a[j] < pivot){
int temp1 = a[i];
a[i] = a[j];
a[j] = temp1;
i++;
} else if(a[j] == pivot){
int temp1 = a[j];
a[j] = a[high];
a[high] = temp1;
j--;
}
}
int temp1 = a[i];
a[i] = a[high];
a[high] = temp1;
// Return the partition index of an array based on the pivot element of other array.
return i;
}
}