Java 如何使用两个对象实现快速排序
我正在尝试对两个对象数组进行排序。一个阵列是螺栓[]阵列,另一个是螺母[]阵列。我无法将一个螺栓与另一个螺栓进行比较。我只能把螺栓比作螺母,反之亦然 我认为解决方案是使用一些经过修改的quicksort版本,但我在实现它时遇到了困难。考虑到我编写的代码,Java 如何使用两个对象实现快速排序,java,object,quicksort,Java,Object,Quicksort,我正在尝试对两个对象数组进行排序。一个阵列是螺栓[]阵列,另一个是螺母[]阵列。我无法将一个螺栓与另一个螺栓进行比较。我只能把螺栓比作螺母,反之亦然 我认为解决方案是使用一些经过修改的quicksort版本,但我在实现它时遇到了困难。考虑到我编写的代码,MatchNutsBolt,我不确定我能做些什么来让它工作: int pivot = partition(nut, low, hi, bolt[hi]) 我想不出如何使用,比如说,一个Nut枢轴,然后遍历Bolt[]列表,然后使用匹配的Bolt
MatchNutsBolt
,我不确定我能做些什么来让它工作:
int pivot = partition(nut, low, hi, bolt[hi])
我想不出如何使用,比如说,一个Nut
枢轴,然后遍历Bolt[]
列表,然后使用匹配的Bolt
s枢轴并遍历Nuts[]
列表,从而以平均O(n log n)
运行时间对两个数组进行排序
public class MatchNutsBolts {
public static void main(String[] args) {
// TODO Auto-generated method stub
Random r = new Random(20);
Bolt[] listOfBolts = NutsBolts.makeRandomBolts(r, 7);
Nut[] listOfNuts = NutsBolts.makeMatchingNuts(r, listOfBolts);
int low = 0;
int hi = listOfBolts.length-1;
match(listOfNuts, listOfBolts, low, hi);
boolean x = NutsBolts.correctFit(listOfNuts, listOfBolts);
System.out.print(x+" ");
}
public static void match(Nut[] nut, Bolt[] bolt, int low, int hi)
{
if (low < hi)
{
// Choose last character of bolts array for nuts partition.
int pivot = partition(nut, low, hi, bolt[hi]); //here is where im having a problem
// Now using the partition of nuts choose that for bolts
// partition.
partition(bolt, low, hi, nut[pivot]);
match(nut, bolt, low, pivot-1);
match(nut, bolt, pivot+1, hi);
}
}
private static int partition(Bolt[] bolt, int low, int high, Nut pivot)
{
int i = low;
Bolt temp1, temp2;
for (int j = low; j < high; j++)
{
//int cmp = Nut.compareTo(bolt[j]);
if (bolt[j].getDigit(i) < pivot.getDigit(i)){
temp1 = bolt[i];
bolt[i] = bolt[j];
bolt[j] = temp1;
i++;
} else if(bolt[j].getDigit(i) == pivot.getDigit(i)){
temp1 = bolt[j];
bolt[j] = bolt[high];
bolt[high] = temp1;
j--;
}
}
temp2 = bolt[i];
bolt[i] = bolt[high];
bolt[high] = temp2;
// Return the partition index of an array based on the pivot
// element of other array.
return i;
}
}
class Bolt {
final private static int SIZE = 7;
private int[] notches;
// A Bolt object is basically a seven digit number.
public Bolt(Random r) {
notches = new int[SIZE];
for (int i=0; i<notches.length; i++)
notches[i] = r.nextInt(10);
}
// A standard constructor given the information stored in a Bolt.
public Bolt(int[] digits) {
notches = new int[digits.length];
for (int i=0; i<SIZE; i++)
notches[i] = digits[i];
}
// Needed to access each digit of a Bolt.
public int getDigit(int i) {
return notches[i];
}
// Made this work the opposite of the corresponding comparison. Surprisingly
// it's not a circular definition, just a consistent one.
public int compareTo(Nut n) {
return -n.compareTo(this);
}
// Return a matching Nut. A matching nut is one who's number has each digit
// be its complement. A complement to a digit is its difference from 9.
public Nut getMatchingNut() {
int[] tmp = new int[SIZE];
// We set each digit for the match.
for (int i=0; i<SIZE; i++)
tmp[i] = 9 - notches[i];
// Now create the object and return it.
Nut match = new Nut(tmp);
return match;
}
// For debugging purposes.
/*public void print() {
for (int i=0; i<SIZE; i++)
System.out.print(notches[SIZE-1-i]);
}*/
}
class Nut {
final private static int SIZE = 7;
private int[] notches;
// A Nut object is basically a seven digit number.
public Nut(Random r) {
notches = new int[SIZE];
for (int i=0; i<notches.length; i++)
notches[i] = r.nextInt(10);
}
// A standard constructor given the information stored in a Nut.
public Nut(int[] digits) {
notches = new int[SIZE];
for (int i=0; i<SIZE; i++)
notches[i] = digits[i];
}
// Need this to access a single digit.
public int getDigit(int i) {
return notches[i];
}
// Here we define a compareTo.
public int compareTo(Bolt n) {
int[] tmp = new int[SIZE];
int carry = 0;
boolean low = false;
// Add up the numbers stored in this and n.
for (int i=0; i<SIZE; i++) {
tmp[i] = (carry + notches[i] + n.getDigit(i))%10;
carry = (carry + notches[i] + n.getDigit(i))/10;
if (tmp[i] < 9)
low = true;
}
// Our sum is a bunch of 9s, so we've got a match!
if (tmp[SIZE-1] == 9 && !low)
return 0;
// Our sum is too big, so make this nut too big.
else if (carry > 0)
return 1;
// Opposite case.
else
return -1;
}
// Works just like the mirror method in the Bolt class.
public Bolt getMatchingBolt() {
int[] tmp = new int[SIZE];
for (int i=0; i<7; i++)
tmp[i] = 9 - notches[i];
return new Bolt(tmp);
}
// For debugging purposes.
/*public void print() {
for (int i=0; i<SIZE; i++)
System.out.print(notches[SIZE-1-i]);
}*/
}
class NutsBolts {
// Returns true iff each nut and each corresponding bolt in the two
// respective arrays match eacy other.
public static boolean correctFit(Nut[] nuts, Bolt[] bolts) {
// Try out each corresponding nut and bolt.
for (int i=0; i<nuts.length; i++)
// See if these don't match.
if (nuts[i].compareTo(bolts[i]) != 0)
return false;
// If we get here, they all matched.
return true;
}
// Creates an array of Bolt objects with size number of elements utilizing
// r.
public static Bolt[] makeRandomBolts(Random r, int size) {
// Allocate space for our bolts,
Bolt[] tmp = new Bolt[size];
// Just create each object here and return the array.
for (int i=0; i<size; i++)
tmp[i] = new Bolt(r);
return tmp;
}
// Creates an array of matching nuts to the array of bolts passed in.
// It scrambles the nuts though, so that they are no longer in the correct
// locations.
public static Nut[] makeMatchingNuts(Random r, Bolt[] bolts) {
// Create the array, and add in each matching nut.
Nut[] tmp = new Nut[bolts.length];
for (int i=0; i<bolts.length; i++) {
tmp[i] = bolts[i].getMatchingNut();
}
// Here we mix the array of nuts up.
for (int i=0; i<3*bolts.length; i++) {
// Get two random indexes.
int index1 = r.nextInt(bolts.length);
int index2 = r.nextInt(bolts.length);
// Swap them!
Nut store = tmp[index1];
tmp[index1] = tmp[index2];
tmp[index2] = store;
}
// Return our array of mixed up, but matching nuts.
return tmp;
}
}
公共类匹配螺母螺栓{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
随机r=新随机(20);
螺栓[]螺栓列表=螺母螺栓。制造随机螺栓(r,7);
螺母[]螺母列表=螺母螺栓。匹配螺母(r,螺栓列表);
int低=0;
int hi=螺栓列表。长度-1;
匹配(列表编号、列表编号、低、高);
布尔x=NutsBolts.correctFit(listOfNuts,listOfBolts);
系统输出打印(x+“”);
}
公共静态空隙匹配(螺母[]螺母,螺栓[]螺栓,内部低,内部高)
{
如果(低<高)
{
//为螺母分区选择螺栓数组的最后一个字符。
int pivot=partition(nut,low,hi,bolt[hi]);//这就是我遇到的问题
//现在使用螺母的隔板,选择螺栓的隔板
//分割。
隔板(螺栓、低、高、螺母[枢轴]);
匹配(螺母、螺栓、低、枢轴-1);
匹配(螺母、螺栓、枢轴+1、hi);
}
}
专用静态内部隔板(螺栓[]螺栓,内部低,内部高,螺母枢轴)
{
int i=低;
螺栓temp1、temp2;
对于(int j=低;j<高;j++)
{
//int cmp=螺母。比较(螺栓[j]);
if(螺栓[j].getDigit(i) 对于(int i=0;i说明你的问题。包含螺母和螺栓的排序数组是什么样子的?它将如何排序?你真正想在这里完成什么?你基本上如何对这两个对象数组进行排序,但你不能将螺母与螺母或螺栓与螺栓进行比较,你只能将螺母与螺栓进行比较,然后s的问题通常要求使用快速排序,我已经尝试过实现,但我一直无法正确地进行排序。排序意味着存在“严格排序”。如果您可以严格地对螺栓和螺母进行排序,并且对螺栓和螺母进行排序,为什么您不能对螺栓和螺栓进行排序?还要注意,您的代码中有一些重要的f或者说,这些问题使我们无法在不做一些假设的情况下编译它。