-1
一般的なSelectionSortを書き込もうとしましたが、sort()
にエラーがあり、findMinIndex(int,int)
またはswap()
にコールすることはできません。main()
でコールできます。この問題を解決するためにコードを変更するにはどうすればよいですか?メソッドJavaを呼び出せませんJava
public class SelectionSort<T extends Comparable<T>>{
T[] result = null;
static int size = 0;
SelectionSort(T[] a){
result = a;
size = a.length;
}
public int findMinIndex(int begin, int end){
T min = result[begin];
int index = begin;
while(begin<=end){
if(result[begin].compareTo(min)<0){
min = result[begin];
index = begin;
}
begin++;
}
return index;
}
public void swap(int i, int j){
T temp = result[i];
result[i]=result[j];
result[j]=temp;
}
private int getSize(){
return size;
}
public void sort(){
int have_sorted_till = -1;
int index = -1;
while(have_sorted_till<size){
index = result.findMinIndex(have_sorted_till+1,size-1);
result.swap(index, have_sorted_till+1);
have_sorted_till++;
}
}
public String toString(){
String s = "";
for(int i=0; i<size; i++){
s = s + result[i]+",";
}
return s;
}
public static void main(String[] args){
Double[] a = new Double[]{2.2,5.7,2.8,9.7,3.9,12.3};
SelectionSort<Double> b = new SelectionSort<Double>(a);
b.swap(0,5);
System.out.print(b.toString());
}
}
"result.findMinIndex"は、 "result"を除いて "findMinIndex"にする必要がありますか? "swap"と同じです。 "result"は配列であり、 "SelectionSort"ではありません。 – mangoDrunk