したがって、B の要素を、int コンポーネントが配列インデックスである SortPair 要素の P の新しい配列にコピーする方法がわかりません。例:
B = [ (7,Q), (9,W), (5,K), (0,S), (9,B) ]<br>
だろう :
P = [ {(7,Q),0}, {(9,W),1}, {(5,K),2}, {(0,S),3}, {(9,B),4} ]
次に、派生したコンパレーター sort_cmp で quickSort を使用して P を並べ替えます。
したがって、要素の部分が等しい場合は、配列のインデックスによって比較されます。したがって、同じ相対順序を維持します。
最後に、元の形式を元の B 形式として、P の Elements 部分を B にコピーして戻します。
import MySorts.Algorithms;
import java.util.*;
import sorts.*;
public static void main(String[] args) {
//mainCheckStability(args);
mainMakeStableQuicksort(args);
//mainTimeSorts(args);
}
public static void mainMakeStableQuicksort(String[] args){
class Element{
int int_part;
char chr_part;
Element(int int_part, char chr_part){
this.int_part = int_part; this.chr_part = chr_part;
}
@Override
public String toString() {return "(" + int_part + "," + chr_part +")";}
}
class SortPair{
Element element;
int index;
SortPair(Element j, int i){
this.index = i;
this.element = j;}
public String toString(){
return ("(" + element.toString()+"'"+index+")");
}
}
//Element [] j = new Element[20];
// int i = 0;
final Comparator<Element> cmp = new Comparator<Element>(){
@Override
public int compare(Element lhs, Element rhs){
return lhs.int_part - rhs.int_part;
}
};
Comparator<SortPair> sort_cmp = new Comparator<SortPair>(){
@Override
public int compare(SortPair lhs, SortPair rhs){
int compt = cmp.compare(lhs.element,rhs.element);
if(compt == 0)
return(lhs.index-rhs.index);
else{
return(compt);
}
//return lhs.i - rhs.i;
}
};
Element [] Q = new Element[15];
Random r = new Random();
for (int i = 0; i <Q.length; ++i){
int n = r.nextInt(10);
char c = (char) (((int)'A')+ r.nextInt(26));
Q[i] = new Element(n,c);
}
Element[] A = Arrays.copyOf(Q, Q.length);
Element[] B = Arrays.copyOf(Q, Q.length);
Element[] C = Arrays.copyOf(Q, Q.length);
Element[] D = Arrays.copyOf(Q, Q.length);
Algorithms.setQuicksortCutoff(5);
System.out.println("array: "+ Arrays.toString(Q) + "\n");
Algorithms.mergeSort(A, cmp);
System.out.println("merge,full: " + Arrays.toString(A));
//i commented out the old command and this is where i believe i need to implement the Sort B array code
//I just don't know how to add the int index, when copying B in to the New array P of SortPair, any help would be appreciated i should know this stuff by now
//Algorithms.quickSort(B, cmp);
Element [] P = (Element[])B.clone();
Element.SortPair(P, ){
@Override
public int compare(Element[] e1, Element[] e2){
return
}
}
for(int i =< B.length ){
}
Element [] B =(Element[])P.clone()
System.out.println("quick,full: " + Arrays.toString(P));
System.out.println();
int low = 3, high = Q.length - 3;
System.out.println("array: "+ Arrays.toString(Q) + "\n");
Algorithms.mergeSort(C, low, high, cmp);
System.out.println("merge,"+low+"-"+high+": "+ Arrays.toString(C));
Algorithms.mergeSort(D, low, high, cmp);
System.out.println("merge,"+low+"-"+high+": "+ Arrays.toString(D));
}