public class MergeSort
{
public static void sort(Comparable[] a)
{
Comparable[] aux = new Comparable[a.length];
sort(a, aux, 0, a.length);
}
private static void sort(Comparable[] a, Comparable[] aux, int lo, int hi)
{
if (hi - lo <= 1) return;
int mid = lo + (hi+lo)/2;
sort(a, aux, lo, hi);
sort(a, aux, mid, hi);
int i = lo, j = mid;
for (int k = lo; k < hi; k++)
{
if (i == mid) aux[k] = a[j++];
else if (j == hi) aux[k] = a[i++];
else if (a[j].compareTo(a[i]) > 0) aux[k] = a[j++];
else aux[k] = a[i++];
}
for (int k = lo; k < hi; k++)
{
a[k] = aux[k];
}
}
public static void main(String[] args)
{
//Read strings from standard input
String[] a = StdIn.readAllStrings();
sort(a);
for (int i = 0; i < a.length; i++)
{
StdOut.print(a[i] + " ");
}
StdOut.println();
}
}
I get this message when trying to compile:
MergeSort.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked
for details.
When I compile with -Xlint
I get these errors:
MergeSort.java:7: warning: [rawtypes] found raw type: Comparable
public static void sort(Comparable[] a)
^
missing type arguments for generic class Comparable<T>
where T is a type-variable:
T extends Object declared in interface Comparable
MergeSort.java:9: warning: [rawtypes] found raw type: Comparable
Comparable[] aux = new Comparable[a.length];
^
missing type arguments for generic class Comparable<T>
where T is a type-variable:
T extends Object declared in interface Comparable
MergeSort.java:9: warning: [rawtypes] found raw type: Comparable
Comparable[] aux = new Comparable[a.length];
^
missing type arguments for generic class Comparable<T>
where T is a type-variable:
T extends Object declared in interface Comparable
MergeSort.java:14: warning: [rawtypes] found raw type: Comparable
private static void sort(Comparable[] a, Comparable[] aux, int lo, int hi)
^
missing type arguments for generic class Comparable<T>
where T is a type-variable:
T extends Object declared in interface Comparable
MergeSort.java:14: warning: [rawtypes] found raw type: Comparable
private static void sort(Comparable[] a, Comparable[] aux, int lo, int hi)
^
missing type arguments for generic class Comparable<T>
where T is a type-variable:
T extends Object declared in interface Comparable
MergeSort.java:29: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type Comparable
else if (a[j].compareTo(a[i]) > 0) aux[k] = a[j++];
^
where T is a type-variable:
T extends Object declared in interface Comparable
6 warnings