using: public static <E> void bubbleSort(E[ ] list, Comparator<? super E> compar
ID: 3532848 • Letter: U
Question
using:
public static <E> void bubbleSort(E[ ] list, Comparator<? super E> comparator)
I have a working method for the Comparable bubble sort with a test array but I just don't understand how to use Comparator. Do I need to have a separate list of objects for the Comparator?
I was trying to use:
//A test method
public static void main(String[] args){
Integer[] list = {2, 3, 4, 5, 6, 1, -2, 6, 14, 12};
bubbleSort(list);
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
}
is this an incorrect assumption?
Explanation / Answer
publicclassbubbleSort{
publicstaticvoidmain(Stringa[]){
inti;
intarray[]={12,9,4,99,120,1,3,10};
System.out.println("ValuesBeforethesort: ");
for(i=0;i<array.length;i++)
System.out.print(array[i]+"");
System.out.println();
bubble_srt(array,array.length);
System.out.print("Valuesafterthesort: ");
for(i=0;i<array.length;i++)
System.out.print(array[i]+"");
System.out.println();
System.out.println("PAUSE");
}
publicstaticvoidbubble_srt(inta[],intn){
inti,j,t=0;
for(i=0;i<n;i++){
for(j=1;j<(n-i);j++){
if(a[j-1]>a[j]){
t=a[j-1];
a[j-1]=a[j];
a[j]=t;
}
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.